> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 筛选

> 使用 where 缩小 Attack Surface Query 的结果范围：条件、嵌套的 and/or 分组、not、各字段类型的运算符、关系测试和时间窗口

`where` 决定查询返回哪些行。它读起来就像你口头描述的句子：*端口为 80 或 443、且页面标题包含 admin 的服务* 写作

```json theme={null}
{
  "logic": "and",
  "items": [
    { "field": "port", "op": "in", "value": ["80", "443"] },
    { "field": "http.title", "op": "contains", "value": "admin" }
  ]
}
```

筛选条件是由四种项构成的树。你可以自由组合它们：

> [card] 条件
>
> `{ "field", "op", "value" }` 检测一个字段：`port in [80, 443]`。

> [card] 分组
>
> `{ "logic": "and" | "or", "items": [...] }` 像括号一样组合多个项。

> [card] 非
>
> `{ "not": <item> }` 保留该项 *不* 匹配的行。

> [card] 关系测试
>
> `{ "relation": "services", … }` 将关联行列表作为一个整体进行判断：*一个都没有*、*全部*、*至少五个*。

筛选始终在你的工作区内运行：任何 `or` 和 `not` 的组合都无法越出工作区。

## 条件

**field** `string` *required*

资源的字段（`port`）、关联行的字段（`http.title`、`target.value`），或列表的字段（`technologies.name`，参见 [列表上的条件](#conditions-on-a-list)）。

**op** `string` *required*

运算符。字段接受哪些运算符取决于其类型：参见 [运算符](#operators)。

**value** `string | string[]`

**始终为字符串**，即使是数字和布尔值（`"443"`、`"true"`）。列表运算符（`in`、`notIn`、`inSubnet`、`notInSubnet`、`containsAll`、`containsAny`）使用数组，`between` 使用 `[from, to]` 对，`isNull` / `isNotNull` 则不需要任何值。

与字段不匹配的值（例如端口为 `"abc"`、严重级别为 `"urgent"`、IP 为 `"fe80::1%eth0"`）会在任何操作运行之前被拒绝，并给出错误值的路径。

## 分组、嵌套和 not

`where` 的顶层始终是一个分组。分组内可以包含条件和其他分组，最多嵌套 **四层**，因此可以写出任意布尔表达式。

*不在开发主机上的管理后台或登录页面：*

```json theme={null}
{
  "logic": "and",
  "items": [
    { "logic": "or", "items": [
      { "field": "http.title", "op": "contains", "value": "admin" },
      { "field": "http.title", "op": "contains", "value": "login" }
    ] },
    { "not": { "logic": "or", "items": [
      { "field": "host.value", "op": "startsWith", "value": "dev-" },
      { "field": "host.value", "op": "endsWith", "value": ".internal" }
    ] } }
  ]
}
```

* `not` 可以包裹任意项：条件、分组或关系测试。
* 一个筛选最多包含 **50 个条件**（AI 智能体和 MCP 为 20 个）。每个关系测试计为一个条件。

> [!TIP]
>
> 在支持嵌套之前编写的筛选，在内层分组中使用 `"conditions"` 而不是 `"items"`（`{ "logic": "or", "conditions": [...] }`）。这种写法仍然有效。

## 运算符

| 字段类型 | 运算符 | 值 | 示例 |
| - | - | - | - |
| text | `eq`, `neq`, `contains`, `notContains`, `startsWith`, `endsWith`, `in`, `notIn` | 任意字符串；`contains`、`startsWith` 和 `endsWith` 不区分大小写 | `value`, `url`, `http.title`, `name` |
| int | `eq`, `neq`, `in`, `notIn`, `gt`, `gte`, `lt`, `lte`, `between` | `"443"`；`between` 使用 `["1", "1024"]` | `port`, `http.statusCode`, `daysToExpiry` |
| number | `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `between` | `"7.5"` | `cvssScore`, `epssScore` |
| bool | `eq`, `neq` | `"true"` or `"false"` | `isOpen`, `isNew`, `isPrimary`, `wildcard` |
| enum | `eq`, `neq`, `in`, `notIn` | 列出的值之一 | `kind`, `severity`, `detectionStatus`, `type` |
| id | `eq`, `neq`, `in`, `notIn` | 来自之前结果的 `id` | `id`, `host.id` |
| ip | `eq`, `neq`, `in`, `notIn`, `inSubnet`, `notInSubnet` | `"10.0.0.1"`；子网运算符使用 CIDR | `ip` |
| time | `gt`, `gte`, `lt`, `lte`, `between` | [时间表达式](#time-expressions)；`between` 使用 `["now-7d", "now"]` | `firstSeen`, `lastSeen`, `notAfter` |
| 字符串列表 | `eq`, `neq`, `contains`, `in`, `notIn`, `containsAll`, `containsAny` | 见下文 | `cve`, `cwe`, `tags`, `ports` |

* **`between`** 包含两端：`port between ["1", "1024"]` 匹配 1 和 1024。
* **字符串列表**（`cve`、`tags`…）：当列表包含该元素时，`eq` / `in` 匹配；当列表与你的数组至少有一个共同元素时，`containsAny` 匹配；当列表包含你数组中的每个元素时，`containsAll` 匹配。

> [!TIP]
>
> 在大型文本字段（`value`、`url`、`http.title`、发现的 `name`）上使用 `contains`、`startsWith` 和 `endsWith` 的开销比 `eq` 或 `in` 更大。如果有确切的值，请优先使用。

### 空值

有些字段可能为空：没有 HTTP 响应的服务没有 `http.title`，发现可能没有 `cvssScore`，从未被扫描发现的主机没有 `lastSeen`。所有此类字段都接受两个额外的运算符，它们不需要 `value`：

```json theme={null}
{ "field": "http.title", "op": "isNull" }
{ "field": "cvssScore", "op": "isNotNull" }
```

空值永远不匹配肯定运算符，而 **始终匹配** 否定运算符（`neq`、`notIn`、`notContains`、`notInSubnet`）：没有标题的服务不在 `contains "admin"` 的结果中，但在 `notContains "admin"` 的结果中。要排除这些行，请在同一字段上添加 `isNotNull`。

## 列表上的条件

针对 **列表** 字段的条件（`services` 上的 `technologies.name`、`hosts` 上的 `vulnerabilities.severity`）会检查关联行：

* 肯定运算符表示 **"至少有一个匹配的关联行"**。
* 否定运算符（`neq`、`notIn`、`notContains`）表示 **"没有任何匹配的关联行"**。

```json Hosts with a critical finding theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "field": "vulnerabilities.severity", "op": "eq", "value": "critical" } ] } }
```

```json Services running nginx theme={null}
{ "from": "services", "select": ["host.value", "port"],
  "where": { "logic": "and", "items": [
    { "field": "technologies.name", "op": "eq", "value": "nginx" } ] } }
```

```json Hosts without port 443 theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "field": "services.port", "op": "neq", "value": "443" } ] } }
```

这是简便写法。当你需要对 **同一个** 关联行施加多个条件，或者需要"至少一个"以外的判断时，请使用关系测试。

## 关系测试

关系测试针对整个关联行列表提出判断。

```json theme={null}
{ "relation": "services", "quantifier": "none", "where": { "logic": "and", "items": [
  { "field": "port", "op": "eq", "value": "443" } ] } }
```

**relation** `string` *required*

资源的某个列表（`hosts` 上的 `services`），或通过关联行到达的列表（`services` 上的 `host.vulnerabilities`）。

**quantifier** `"some" | "none" | "every"` (default: some)

* `some`：至少有一个关联行匹配 `where`。不带 `where` 时：该行至少有一个关联行。
* `none`：没有关联行匹配 `where`。不带 `where` 时：该行没有任何关联行。
* `every`：每个关联行都匹配 `where`（此时 `where` 必填）。没有关联行的行也会通过。

**count** `object`

`{ "op": "eq" | "neq" | "gt" | "gte" | "lt" | "lte", "value": "<n>" }`：将关联行的 **数量**（如果提供了 `where`，则为匹配 `where` 的行）与一个数字比较。`quantifier` 和 `count` 只能二选一。

**where** `object`

针对关联行的筛选。其字段是关联资源自身的字段：`port`，而不是 `services.port`。其中不能再包含其他列表。

与列表上的条件不同，同一个关系测试中的所有条件都作用于 **同一个关联行**：

```json HTTP but no HTTPS theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "relation": "services", "where": { "logic": "and", "items": [
      { "field": "port", "op": "in", "value": ["80", "8080"] } ] } },
    { "relation": "services", "quantifier": "none", "where": { "logic": "and", "items": [
      { "field": "port", "op": "eq", "value": "443" } ] } } ] } }
```

```json Only web ports open theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "relation": "services", "quantifier": "every", "where": { "logic": "and", "items": [
      { "field": "port", "op": "in", "value": ["80", "443"] } ] } } ] } }
```

```json Ten ports or more theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "relation": "services", "count": { "op": "gte", "value": "10" } } ] } }
```

```json A finding both critical and open theme={null}
{ "from": "hosts", "select": ["value"],
  "where": { "logic": "and", "items": [
    { "relation": "vulnerabilities", "where": { "logic": "and", "items": [
      { "field": "severity", "op": "eq", "value": "critical" },
      { "field": "isOpen", "op": "eq", "value": "true" } ] } } ] } }
```

> [!NOTE]
>
> 关系测试会读取关联资源，因此需要该资源的权限：针对 `vulnerabilities` 的测试需要 `vulnerability.read`，即使查询只返回主机。

筛选选择的是父级行；它不会改变 `include` 返回哪些子级。若只想获取匹配的子级，请将相同的条件放入 include 自身的 `where` 中（参见 [组合查询](https://docs.oasm.io/zh/attack-surface-query/composing-queries.md#pattern-6-filter-the-included-children)）。

## 时间表达式

时间值为以下之一：

| 形式 | 含义 |
| - | - |
| `now` | 批量查询的求值时间（`meta.now`）；同一批量查询中的所有查询使用同一个值 |
| `now-30m`, `now-24h`, `now-7d`, `now-2w` | `now` 之前的若干分钟、小时、天或周（1 到 3650 个单位） |
| `now+30d` | `now` 之后，**仅用于筛选**：例如 30 天内到期的证书 |
| `2026-11-01T00:00:00Z`, `2026-11-01T07:00:00+07:00` | 带偏移量的 ISO 8601；不带偏移量的时间会被拒绝 |

```json theme={null}
{ "from": "tls", "select": ["host", "notAfter"],
  "where": { "logic": "and", "items": [ { "field": "notAfter", "op": "between", "value": ["now", "now+30d"] } ] } }
```

## 时间窗口

**窗口** 保留时间字段落在 `[since, until)` 区间内的行。这是表达 *最近* 的简便方式：

```json theme={null}
{ "window": { "since": "now-7d" }, "queries": { "newHosts": { "from": "hosts" } } }
```

* 设置在批量查询上的窗口，适用于所有未设置自身窗口的查询。设置在单个查询上的窗口会覆盖批量查询的窗口。
* 查询上的 `"window": null` 读取全部时间范围，即使批量查询设置了窗口。
* 完全不设置窗口表示全部时间范围。
* 窗口默认作用于资源的 `firstSeen`；`"windowField": "lastSeen"` 则使用另一个时间字段（*本周被扫描发现的主机*）。
* `until` 默认为 `now`，且不能超过未来一天。
* `rollups` 没有时间字段：当批量查询设置了窗口时，请为其设置 `"window": null`。

> [!NOTE]
>
> `firstSeen` 是 OASM 首次记录该行的时间，`lastSeen` 是扫描最后一次报告它的时间。*本周新增* 是作用于 `firstSeen` 的窗口；*本周仍然存活* 是作用于 `lastSeen` 的窗口。`isNew` 字段又有所不同：表示该行是由其工作流在该目标上最近一次完成的运行发现的（即控制台中的"新"徽标）。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.