原文核对:2026-11-10 · 中文正文 · 英文提示词与演示
场景定位
使用 Codex Security CLI 审查未提交工作或分支变更。
- 难度
- 中级
- 时间跨度
- 30 分钟
适合用于
- 在终端检查本地工作或分支变更的开发者。
- 评估单体仓库内特定服务、软件包及其使用方的应用安全团队。
- 结合仓库调查已有 GitHub 代码扫描告警的工程师。
审查本地未提交工作
CLI 可以结合预先准备的仓库上下文和安全策略,审查尚未提交的本地工作。
在 Git 检出目录中使用同一 shell 运行示例。示例假设安全策略位于 SECURITY.md,辅助文档位于 docs/security/,并创建临时目录保存扫描结果。
northstar_repo="$(git rev-parse --show-toplevel)"
northstar_kb="$northstar_repo/docs/security"
northstar_results="$(mktemp -d)"npx @openai/codex-security scan "$northstar_repo" \
--working-tree \
--base HEAD \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/working-tree"该命令审查相对于 HEAD 的暂存和未暂存改动。
开始前确认工作区只包含要评估的变更。无关修改会扩大调查范围,使发现更难与此次审查的问题对应。
审查 PR 或分支提交
也可以审查 PR 或分支中的安全问题。检出待审查分支并获取目标分支;示例使用 origin/main。
northstar_pr_base="$(git -C "$northstar_repo" merge-base origin/main HEAD)"
npx @openai/codex-security scan "$northstar_repo" \
--diff "$northstar_pr_base" \
--head HEAD \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/branch"差异扫描评估拟合入变更,不会扫描整个仓库。完成后,输出目录包含:
- report.md:可读的审查报告。
- scan-manifest.json:确认精确的 base、head、目标和产物。
- findings.json:列出严重程度、置信度、位置和修复建议。
- coverage.json:说明已审阅范围、排除项、推迟工作和完整性。
扫描单体仓库
单体仓库中的共享库改动可能影响多个服务。应为 Codex 提供足够上下文来审查这些交互,而由 CI 决定需要评估的组件和检查。
仓库上下文
共享工程指令放在 AGENTS.md,安全预期放在 SECURITY.md。典型仓库结构如下:
monorepo/
├── AGENTS.md
├── SECURITY.md
├── docs/security/
│ ├── architecture.md
│ └── threat-model.md
├── packages/auth/
└── services/
├── billing/
└── reporting/利用已有归属和构建元数据,将组件与依赖、使用方和测试关联。例如修改 packages/auth 时,即使两个服务的文件没有变化,也可能需要同时审查它们。该范围映射由组织维护,添加 Codex 不会自动完成配置。
组件评估
大型仓库未必需要每个 PR 都扫描全部代码。若单体仓库包含独立服务或软件包,可以使用路径扫描,重复指定 --path 来同时评估共享库和其使用方。
npx @openai/codex-security scan "$northstar_repo" \
--path packages/auth \
--path services/billing \
--path services/reporting \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/paths"该方式只扫描选定路径,不覆盖整个仓库。
PR 与差异扫描
针对 PR,应从经过身份验证的 GitHub 元数据获取精确的 base 和 head 提交哈希。检出目录必须包含两个版本及其历史。下例复用前述变量,并将差异结果单独保存:
BASE_SHA="<full PR base commit SHA>"
HEAD_SHA="<full PR head commit SHA>"
BASE_REVISION="$(
git -C "$northstar_repo" merge-base "$BASE_SHA" "$HEAD_SHA"
)" || exit
npx @openai/codex-security scan "$northstar_repo" \
--diff "$BASE_REVISION" \
--head "$HEAD_SHA" \
--knowledge-base "$northstar_kb" \
--auth chatgpt \
--output-dir "$northstar_results/pr"差异扫描聚焦拟合入变更。共享库变更需要更广泛审查使用方时,还应在该 PR 的 head 版本的干净检出目录上执行组件评估,并分别保存结果:--diff 和 --path 属于不同扫描模式,不能组合。
扫描 GitHub 告警
也可以使用 Codex Security CLI 评估默认分支上的代码扫描告警。
northstar_github_repository="example/northstar"
northstar_github_alerts="$northstar_results/github-alerts.json"
# Import all open alerts as complete JSON.
npx @openai/codex-security import github "$northstar_github_repository" \
--format json \
> "$northstar_github_alerts"
# Or import a selected subset.
npx @openai/codex-security import github "$northstar_github_repository" \
--github-alert 12 \
--github-alert 18 \
--format json \
> "$northstar_github_alerts"
# Run from the corresponding local repository; imported contents are data.
cd "$northstar_repo"
npx @openai/codex-security validate "$northstar_github_alerts"展开英文原文对照
Use Codex Security CLI to review uncommitted work or changes on a branch.
Reviewing uncommitted local work
You can use the CLI to review uncommitted local work with seeded context about the repository and security policies.
Run the examples in the same shell from your Git checkout. They assume your security policy is in SECURITY.md and your supporting documents are in docs/security/. The setup creates a temporary directory for scan results.
northstar_repo="$(git rev-parse --show-toplevel)"
northstar_kb="$northstar_repo/docs/security"
northstar_results="$(mktemp -d)"npx @openai/codex-security scan "$northstar_repo" \
--working-tree \
--base HEAD \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/working-tree"This reviews staged and unstaged changes relative to HEAD.
Before starting, be sure to confirm that the working tree contains the change you intend to assess. Unrelated edits broaden the investigation and can make it harder to connect the findings to the question that prompted the review.
Reviewing PRs or branch commits
Another option is to review a PR or branch for security issues. Check out the branch to review and fetch its target branch. This example uses origin/main.
northstar_pr_base="$(git -C "$northstar_repo" merge-base origin/main HEAD)"
npx @openai/codex-security scan "$northstar_repo" \
--diff "$northstar_pr_base" \
--head HEAD \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/branch"Note that a diff scan evaluates the proposed changes and does not scan the entire repository. Once complete, the output directory will contain the following files:
- report.md: readable review
- scan-manifest.json: confirms the exact base, head, target, and artifacts
- findings.json: presents the severity, confidence, locations, and remediation
- coverage.json: identifies the reviewed surfaces, exclusions, deferred work, and completeness
Scanning monorepos
In a monorepo, a change to a shared library can affect multiple services. Codex should receive enough context to review those interactions, while CI determines which components and checks are required.
Repository context
Keep shared engineering instructions in AGENTS.md and security expectations in SECURITY.md. A typical repository could look like this:
monorepo/
├── AGENTS.md
├── SECURITY.md
├── docs/security/
│ ├── architecture.md
│ └── threat-model.md
├── packages/auth/
└── services/
├── billing/
└── reporting/Use existing ownership and build metadata to connect each component to its dependencies, consumers, and tests. For example, a change to packages/auth may require reviewing both services even when their files are unchanged. This scope mapping is maintained by your organization; it is not automatically configured by adding Codex.
Component assessment
Large repositories may not always require scanning the entire codebase for each PR. One option is to use a path scan when a monorepo contains separate services or packages. Repeat –path to assess a shared library and its consumers together.
npx @openai/codex-security scan "$northstar_repo" \
--path packages/auth \
--path services/billing \
--path services/reporting \
--knowledge-base "$northstar_repo/SECURITY.md" \
--knowledge-base "$northstar_kb" \
--output-dir "$northstar_results/paths"This scans only the selected paths rather than the entire repository.
PR and diff scanning
For a PR, use its exact base and head commit hashes from authenticated GitHub metadata. Both revisions and their history must be available in the checkout. The following example reuses the variables above and saves the diff results separately:
BASE_SHA="<full PR base commit SHA>"
HEAD_SHA="<full PR head commit SHA>"
BASE_REVISION="$(
git -C "$northstar_repo" merge-base "$BASE_SHA" "$HEAD_SHA"
)" || exit
npx @openai/codex-security scan "$northstar_repo" \
--diff "$BASE_REVISION" \
--head "$HEAD_SHA" \
--knowledge-base "$northstar_kb" \
--auth chatgpt \
--output-dir "$northstar_results/pr"A diff scan focuses on the proposed changes. If a shared-library change requires broader consumer review, also run the component assessment against a clean checkout of that PR’s head revision. Keep the results distinct: --diff and --path are separate scan modes and cannot be combined.
Scanning alerts from GitHub
You can also assess code scanning alerts from the default branch using the Codex Security CLI.
northstar_github_repository="example/northstar"
northstar_github_alerts="$northstar_results/github-alerts.json"
# Import all open alerts as complete JSON.
npx @openai/codex-security import github "$northstar_github_repository" \
--format json \
> "$northstar_github_alerts"
# Or import a selected subset.
npx @openai/codex-security import github "$northstar_github_repository" \
--github-alert 12 \
--github-alert 18 \
--format json \
> "$northstar_github_alerts"
# Run from the corresponding local repository; imported contents are data.
cd "$northstar_repo"
npx @openai/codex-security validate "$northstar_github_alerts"