> ## 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.

# 已保存查询与报告

> 保存 Attack Surface Query 批次，将大型导出作为后台报告任务运行，并将已保存查询渲染为 PDF 报告

## 已保存查询

已保存查询是存储在工作区中的具名批次，团队可以重复运行相同的问题（每周暴露面审查、合规导出），而无需到处复制 JSON。

| 方法与路径 | 权限 | 用途 |
| - | - | - |
| `GET /api/attack-surface/saved-queries` | `report.read` | 按名称列出工作区的已保存查询 |
| `POST /api/attack-surface/saved-queries` | `report.write` | 保存一个批次 |
| `GET /api/attack-surface/saved-queries/:id` | `report.read` | 读取一个 |
| `PATCH /api/attack-surface/saved-queries/:id` | `report.write` | 修改其名称、描述或批次 |
| `DELETE /api/attack-surface/saved-queries/:id` | `report.write` | 删除 |
| `POST /api/attack-surface/saved-queries/:id/run` | `report.read` | 运行并返回结果，可选传入 [参数](#params) |

### 保存批次

```json theme={null}
{
  "name": "Weekly exposure review",
  "description": "New hosts, risky ports and open critical findings of the last 7 days",
  "spec": {
    "window": { "since": "now-7d" },
    "queries": {
      "newHosts": { "from": "hosts", "select": ["value", "kind", "target.value"], "limit": 200 },
      "riskyPorts": { "from": "services", "select": ["host.value", "port"], "limit": 200,
        "where": { "logic": "and", "items": [{ "field": "port", "op": "in", "value": ["21", "23", "445", "3389", "6379"] }] } },
      "critical": { "from": "vulnerabilities", "select": ["name", "host.value", "firstSeen"],
        "where": { "logic": "and", "items": [
          { "field": "isOpen", "op": "eq", "value": "true" },
          { "field": "severity", "op": "eq", "value": "critical" } ] } }
    }
  }
}
```

**name** `string` *required*

1 到 120 个字符，在工作区内唯一。

**description** `string`

最多 2 000 个字符。

**spec** `object` *required*

一个批次：与 `POST /api/attack-surface/query` 的请求体相同，外加可选的 `params` 声明（请参阅 [参数](#params)）。

批次在保存时会根据目录**以及你的权限**进行校验：你无法保存针对你无权读取的资源的查询。

### 运行已保存查询

已保存查询始终**以运行它的成员身份、按其当时的权限**运行，而不是作者的权限。没有 `vulnerability.read` 权限的成员运行上述批次会得到 `PERMISSION_DENIED`。每次运行时都会重新校验批次，因此已保存查询会随你角色的任何变化而变化。

相对时间（`now-7d`）在查询运行时解析，因此已保存的每周审查始终覆盖最近七天。

## 参数

同一个问题常常需要针对不同对象提出：*这个目标暴露了什么？*、*哪些主机开放了这个端口？*。与其为每个目标保存一份副本，不如用**参数**保存一次查询，在运行时填入值。

### 声明参数

在 spec 中添加 `params` 映射，然后在需要填值的位置放置 `{ "$param": "<name>" }`：

```json theme={null}
{
  "name": "Exposure of one target",
  "spec": {
    "params": {
      "target": { "type": "string", "default": "example.com", "description": "Target to review" },
      "ports": { "type": "list", "default": ["21", "23", "445", "3389"] },
      "since": { "type": "string", "default": "now-7d" },
      "rows": { "type": "number", "default": 100 }
    },
    "window": { "since": { "$param": "since" } },
    "queries": {
      "exposed": {
        "from": "services",
        "select": ["host.value", "port", "http.title"],
        "where": { "logic": "and", "items": [
          { "field": "target.value", "op": "eq", "value": { "$param": "target" } },
          { "field": "port", "op": "in", "value": { "$param": "ports" } }
        ] },
        "limit": { "$param": "rows" }
      }
    }
  }
}
```

| `type` | 接受 | 典型用途 |
| - | - | - |
| `string` | 字符串，1 到 255 个字符 | 筛选值、window 边界（`now-7d`） |
| `number` | 数字 | `limit`，或数值型筛选值（以数字字符串形式发送） |
| `list` | 1 到 100 个字符串 | `in`、`notIn`、`containsAny`… 的值 |

* 标记只能代表**筛选值**、**`limit`** 或 **window 边界**（`since`、`until`），不能用于其他位置：绝不能代表字段、运算符或资源。参数可以改变*你要查找的内容*，但不能改变*查询读取的内容*。
* 每个参数都需要 `default`。保存时会用默认值校验 spec，因此已保存查询始终可以直接运行。
* 一个已保存查询最多包含 10 个参数，且每个声明的参数都必须被使用。

### 带值运行

在运行调用的请求体中发送参数值。缺失的参数使用默认值；完全不带请求体则全部使用默认值运行。

```http theme={null}
POST /api/attack-surface/saved-queries/6f1d0c3a-…/run
Content-Type: application/json
X-Workspace-Id: <workspace id>

{ "params": { "target": "acme.io", "ports": ["3389"] } }
```

系统会先填入参数值，然后像其他批次一样进行校验：不是数字的端口或过长的目标值会被拒绝，并指出问题所在的路径。查询未声明的参数同样会报错。

已保存查询的 [PDF 报告](#pdf-reports) 使用默认值。

## 报告模式

对于超出交互式查询允许范围的导出，请使用 `"options": { "mode": "report" }` 发送批次。它会作为后台任务排队，而不是在请求中运行：

```json theme={null}
{
  "options": { "mode": "report" },
  "queries": {
    "allUrls": { "from": "urls", "select": ["url", "service.host.value", "firstSeen"], "limit": 20000, "window": null }
  }
}
```

照常使用 `nextCursor` 对导出进行分页：每一页都是一个新任务。

调用会立即返回：

```json theme={null}
{ "jobId": "0b6f2a4e-…", "status": "queued" }
```

轮询 `GET /api/attack-surface/jobs/:jobId` 直到任务结束：

| `status` | 含义 |
| - | - |
| `queued` | 等待 Worker；每个 API 实例一次只运行一个报告任务 |
| `running` | 批次正在运行 |
| `completed` | `result` 包含批次结果，结构与交互式响应相同 |
| `failed` | `error` 包含 `{ code, message }` |

* 任务状态及其结果在最后一次更新后保留**一小时**，之后 `GET …/jobs/:jobId` 返回 `NOT_FOUND`。
* 批次在排队**之前**就会进行校验和成本估算，因此错误会立即报告，而不是在任务中报告。
* 任务以**你排队时的权限**运行。失败的任务不会重试。
* 报告模式会提高限制：每个查询 50 000 行，每条语句 120 秒，每个批次 10 分钟，响应 16 MB，成本预算 60 000。请参阅 [分页、限制与成本](https://docs.oasm.io/zh/attack-surface-query/paging-and-limits.md#limits)。
* 当运维人员设置了 `ASQ_READ_REPLICA_URL` 时，报告任务会从该只读副本读取，使重型导出不占用主数据库。
* 报告模式仅通过 HTTP API 提供，AI 代理和 MCP 客户端无法使用（`MODE_CONFLICT`）。

## PDF 报告

已保存查询可以渲染为 PDF，并与工作区的其他 [报告](https://docs.oasm.io/zh/reports.md) 一起存储：

```http theme={null}
POST /api/reports/generate/query
Content-Type: application/json
X-Workspace-Id: <workspace id>

{ "savedQueryId": "6f1d0c3a-…" }
```

* 需要 `report.write`。批次以你当前的权限运行，其 [参数](#params) 使用默认值。
* 批次中的所有查询读取**同一个一致的数据库快照**，因此各表格中的数字彼此一致。
* 批次在交互式限制下运行（每个查询 1 000 行，成本预算 1 000）；其自身的 `options.mode` 会被忽略。
* 每个查询生成一张表格，以查询名称命名。关联字段成为列（`host.value`）；关联行列表显示为每个子项的第一个值，以逗号分隔；ID 会被省略。
* 一张表格最多 **500 行**，一个单元格最多 300 个字符。被截断的表格或查询还有更多页的表格会被标记为已截断。
* 失败的查询会在其表格位置显示错误；其他表格仍会正常渲染。


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