GitHub Actions
在 CI 中运行 zn-gate:审计智能体配置,并扫描提示中的注入和泄露的密钥。
zn-gate Action 会在每次推送或拉取请求时检查仓库。它只在 GitHub 运行器上运行,不需要账号和 API 密钥,也不会向任何地方发送数据。它有两项检查:
- Scan(
mode: scan,默认)在提示、工具定义和配置文件中查找提示词注入陷阱和泄露的密钥。详情:扫描。 - Audit(
mode: audit)读取你的编程智能体被配置为信任的内容(.claude/、.mcp.json、.codex/、.cursor/、钩子、AGENTS.md/CLAUDE.md、插件和技能),并报告结果,不会运行仓库中的任何内容。详情:审计。 mode: both两者都运行。
最小工作流
下面的工作流在每次推送和拉取请求时运行扫描。发现项会显示为注释,并使该步骤失败:
name: zn-gate
on: [push, pull_request]
permissions:
contents: read
jobs:
zn:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: usezn/zn@v1.17.0
完整工作流
下面的工作流运行两项检查,生成 SARIF 报告并上传到 GitHub 代码扫描。中等及以上严重程度的发现项会使该步骤失败:
name: zn-gate
on:
pull_request:
permissions:
contents: read
jobs:
zn:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write # only for the SARIF upload
actions: read # private repositories only
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: usezn/zn@v1.17.0
id: zn
with:
mode: both
path: .
format: sarif
severity-threshold: medium
- uses: github/codeql-action/upload-sarif@v4
if: ${{ !cancelled() && steps.zn.outputs.sarif-file != '' }}
with:
sarif_file: ${{ steps.zn.outputs.sarif-file }}
输入
| 输入 | 默认值 | 作用 |
|---|---|---|
mode |
scan | scan、audit 或 both。 |
version |
1.17.0 |
要安装的 zn-gate 的确切版本。不接受版本范围和 latest。 |
path |
. |
要检查的路径,相对于工作区。audit 需要目录;scan 也接受单个文件。 |
fail-on-threat |
true |
有发现项时使步骤失败。false 只报告。必须是 true 或 false。 |
severity-threshold |
high |
仅 audit:当 fail-on-threat 为 true 时,使步骤失败的最低严重程度(critical、high、medium、low)。scan 没有严重程度:任何发现项都会失败。 |
fail-on |
空 | 仅 audit:使步骤失败的最低严重程度(low、medium、high、critical、none)。设置后,它会覆盖 fail-on-threat 和 severity-threshold。 |
agents |
空(全部) | 仅 audit:要审计的智能体列表,以逗号分隔。 |
format |
github |
github(行内注释)、text、json 或 sarif。 |
sarif-file |
空 | 使用 format: sarif 时:文件的写入位置,位于工作区或运行器临时目录内。为空时,在运行器临时目录下的新目录中写入 zn-gate.sarif。 |
package-sha256 |
空 | version 对应的 PyPI 上 zn-gate wheel 的 SHA-256,由 scan 使用。 |
npm-integrity |
空 | version 对应的 zn-gate npm 压缩包的 sha512-... 完整性值,由 audit 使用。 |
版本
除非你设置 version,usezn/zn@v1.17.0 会安装 zn-gate 1.17.0。Audit 需要 zn-gate 1.6.0 或更高版本:version 更旧时,mode: audit 会失败,mode: both 只运行扫描并输出警告。像示例那样把 Action 固定到确切的发布标签,这样每次运行的都是同一份代码。发布列表见发布页面。
输出与退出码
Action 会设置三个输出,可在后续步骤中读取:
sarif-file:SARIF 文件的路径(仅format: sarif);如果没有写入文件则为空。scan-exit-code:0干净,1有发现项,2出错;扫描未运行时为空。audit-exit-code:0干净,1有达到或超过阈值的发现项,2出错;审计未运行时为空。
使用 fail-on-threat: false 时,检查只报告而不失败,所以有发现项时退出码为 0;出错时仍为 2。
代码扫描(SARIF)
使用 format: sarif 并加上上传步骤后,每个发现项都是 Security > Code scanning 下的一条告警,包含规则、文件和行号,任何密钥值都会被隐藏。扫描和审计是两次独立的运行(类别为 zn-gate/scan/ 和 zn-gate/audit/),因此可以按工具筛选。代码扫描需要公开仓库,或在私有仓库上启用 GitHub Code Security;否则请使用 format: github,并省略 security-events 权限。在来自 fork 的拉取请求上,GitHub 可能不允许上传;检查仍会运行,也仍会设置作业状态。
权限
Action 只需要 contents: read。仅在上传 SARIF 时添加 security-events: write,在私有仓库中还要添加 actions: read。在 actions/checkout 上使用 persist-credentials: false,避免令牌留在工作区中,并在 pull_request 而不是 pull_request_target 上运行工作流。
只报告,不让构建失败
如果想在不阻止合并的情况下查看发现项,例如在清理现有仓库时,请设置 fail-on-threat: false:步骤保持绿色,发现项仍会显示为注释。若想保持检查严格但不中断作业,请使用 continue-on-error: true 并读取退出码输出(1 表示有发现项,2 表示出错,所以忽略之前先检查是否为 2):
- uses: usezn/zn@v1.17.0
id: zn
continue-on-error: true
with:
mode: both
- name: Report the result
if: always()
run: echo "scan=${{ steps.zn.outputs.scan-exit-code }} audit=${{ steps.zn.outputs.audit-exit-code }}"
仓库干净之后,把 fail-on-threat 改回 true(并移除 continue-on-error),让新的发现项阻止合并。
验证软件包
要让运行拒绝任何其他文件,请设置 npm-integrity(参见 npm view zn-gate@<version> dist.integrity)和 package-sha256。如果下载的文件哈希不同,安装会报错并停止:
- uses: usezn/zn@v1.17.0
with:
mode: audit
npm-integrity: 'sha512-<integrity of the zn-gate npm tarball>'
package-sha256: '<sha-256 of the zn-gate wheel on PyPI>'
徽章
使用 GitHub 为你自己的工作流生成的状态徽章来表明检查在运行(替换 <owner>、<repo> 和文件名 zn.yml)。Action 本身不会添加徽章:
[](https://github.com/<owner>/<repo>/actions/workflows/zn.yml)
在本机运行同样的检查
同样的检查可以在本地运行:
npx -y zn-gate scan .
npx -y zn-gate audit .