审查代码变更的安全性
如需完整文档索引,请参阅 llms.txt。文档页面的 Markdown 版本可通过在页面后追加
.md来获取 URL。
运行安全变更审查,以在一个由 Git 支持的变更集中发现回归。 Codex 会审查每个发生变更的类源代码文件及其直接支撑代码。 它不会将审查扩展为完整的代码库审计。
若要扫描整个代码库而不是特定变更,请参阅 运行安全 扫描。
运行手动审查
Section titled “运行手动审查”在桌面应用中,打开 安全,选择 扫描,然后选择 + 扫描。 选择代码库,然后选择 变更。审查未提交的变更、一个 单独提交,或一个基准修订和头部修订。 深度扫描 不适用于 变更扫描。
你也可以在对话中要求 Codex 审查未提交的变更:
Use $codex-security:security-diff-scan to review my current uncommitted changes for security regressions.对于提交或分支范围,请在需要时指定两个修订:
Use $codex-security:security-diff-scan to review the changes from origin/main to HEAD for security regressions. Focus on authentication, authorization, input handling, filesystem access, network requests, and secrets.当拉取请求的基准修订和头部修订在本地检出中可用时, 你也可以指定该拉取请求。
在设置中确认变更
Section titled “在设置中确认变更”- 选择 变更。
- 确认已检出的代码库、当前分支和最新提交。
- 在 要审查的变更下,选择:
Uncommitted changes用于当前工作树。- 用于单提交审查的最新提交。
- 用于分支或拉取请求范围的基准修订和头部修订。
- 确认摘要描述的是你打算审查的变更。
- 选择 开始扫描。
Codex 不会检出其他分支,也不会切换所选工作树。如果 请求的修订在本地不可用,请在审查前获取它,或 提供本地可用的基准修订和头部修订。
处理发现的问题
Section titled “处理发现的问题”审查结果后, 修复并验证一个已接受的 发现项 或 导出并跟踪 发现项。
在 CI/CD 中自动执行审查
Section titled “在 CI/CD 中自动执行审查”如果你有权访问测试版独立 CLI,请参阅 运行 Codex 中的 Security
CI 以获取结构化 JSON、严重性策略和 SARIF
上传。继续阅读本节,以通过
调用已安装的插件技能 codex exec。
当运行器可以无交互地调用 $codex-security:security-diff-scan 中的 CI 时,在
Codex CLI 中运行。首先,安装 CLI 且不要暴露扫描
凭据:
npm install --global @openai/codex在 Codex 中安装 Security 插件 CLI:
codex plugin add codex-security@openai-curatedThe install command uses the public Codex CLI plugin marketplace, which can offer a different version from the hosted desktop-app catalog. Check the plugin changelog before you depend on a specific plugin version or feature in CI.
接下来,从你的 OpenAI API 密钥 CI 存储中提供一个
CODEX_SECURITY_API_KEY。仅在扫描期间暴露该凭据:
CODEX_API_KEY="$CODEX_SECURITY_API_KEY" codex exec \ --sandbox workspace-write \ "Use \$codex-security:security-diff-scan to review changes from $BASE_REVISION to $HEAD_REVISION for security regressions. Do not modify the checkout."可写沙箱允许扫描创建临时工件。提示词 仍要求 Codex 保持源代码检出不变。
扫描会将输出写入
$TMPDIR/codex-security-scans/<repository>/<scan-id>/:
| 文件 | 内容 |
|---|---|
report.md |
完整扫描目录的主要可读入口点。 |
findings/<slug>/ |
每个可报告发现项对应一份详细漏洞报告,并在可用时包含支持性的概念验证文件。 |
hardening/ |
当扫描存在可报告发现项时,提供结构加固组合和支持性提案或图表。 |
findings.json |
包含稳定标识符、严重性、置信度、源位置和修复措施的发现项。可供已批准的内部安全工作流或下游工具使用。 |
scan-manifest.json |
密封的扫描回执,包含已审查目标、修订和工件哈希。 |
coverage.json |
已审查和推迟的表面、排除项以及覆盖完整性。 |
该 findings.json 架构
定义了完整结构。该架构包含以下字段:
| 字段 | 类型 | 描述 |
|---|---|---|
documentType |
字符串 | 将文档标识为 codex-security.findings。 |
schemaVersion |
字符串 | 标识发现项架构版本。 |
scanId |
字符串 | 标识生成发现项的扫描。 |
findings |
数组 | 包含零个或多个发现项对象。 |
findings[].findingId |
字符串 | 从发现项指纹派生的稳定发现项标识符。 |
findings[].occurrenceId |
字符串 | 标识某次特定扫描中该发现项的此次出现。 |
findings[].ruleId |
字符串 | 标识漏洞家族。 |
findings[].identity |
对象 | 包含语义锚点和可选的同级实例标识符。 |
findings[].fingerprints |
对象 | 包含指纹算法和主指纹。 |
findings[].title |
字符串 | 提供简短的发现项标题。 |
findings[].summary |
字符串 | 概述漏洞及其影响。 |
findings[].severity |
对象 | 包含严重性级别和可选评分详情。 |
findings[].confidence |
对象 | 包含置信度级别和理由。 |
findings[].taxonomy |
对象 | 包含漏洞类别和 CWE 标识符。 |
findings[].locations |
数组 | 列出受影响文件、行号和位置角色。 |
findings[].remediation |
字符串 | 描述建议的修复。 |
findings[].provenance |
对象 | 标识发现项的来源。 |
例如,此命令会为每个问题输出一行以制表符分隔的记录:
jq -r ' .findings[] | [.findingId, .severity.level, .confidence.level, .locations[0].path, .locations[0].startLine, .title] | @tsv' findings.json这些示例假定使用可信的 Linux 运行器,并带有 Node.js 和 npm、Git、Python
3、 jq以及提供商的命令行工具。 npm 全局包前缀
必须可写。
选择适用于你的 CI 提供商的示例:
扫描结果可能包含敏感的漏洞详情。请将工件 保持私密,并仅在审查受众、内容和 所需批准后发布发现项。
name: Codex Security review
on: pull_request:
jobs: security-review: if: github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest permissions: contents: read steps: - uses: actions/checkout@v5 with: ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 persist-credentials: false
- name: Install Codex Security env: CODEX_HOME: ${{ runner.temp }}/codex-home run: | npm install --global @openai/codex codex plugin add codex-security@openai-curated
- name: Review code changes env: CODEX_SECURITY_API_KEY: ${{ secrets.CODEX_SECURITY_API_KEY }} CODEX_HOME: ${{ runner.temp }}/codex-home TMPDIR: ${{ runner.temp }}/codex-security BASE_SHA: ${{ github.event.pull_request.base.sha }} HEAD_REVISION: ${{ github.event.pull_request.head.sha }} run: | BASE_REVISION="$(git merge-base "$BASE_SHA" "$HEAD_REVISION")" CODEX_API_KEY="$CODEX_SECURITY_API_KEY" codex exec \ --sandbox workspace-write \ "Use \$codex-security:security-diff-scan to review changes from $BASE_REVISION to $HEAD_REVISION for security regressions. Do not modify the checkout."
- uses: actions/upload-artifact@v4 if: always() with: name: codex-security-review path: ${{ runner.temp }}/codex-security/codex-security-scans创建一个已掩码的 CODEX_SECURITY_API_KEY CI/CD 变量,并在分享发现项前私下审查扫描
工件。
codex-security-review: rules: - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID == $CI_PROJECT_ID' variables: GIT_DEPTH: "0" script: - | codex_security_api_key="$CODEX_SECURITY_API_KEY" unset CODEX_SECURITY_API_KEY export CODEX_HOME="/tmp/codex-home-$CI_JOB_ID" export TMPDIR="/tmp/codex-security-$CI_JOB_ID" export BASE_REVISION="$CI_MERGE_REQUEST_DIFF_BASE_SHA" export HEAD_REVISION="${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA:-$CI_COMMIT_SHA}" npm install --global @openai/codex codex plugin add codex-security@openai-curated CODEX_API_KEY="$codex_security_api_key" codex exec \ --sandbox workspace-write \ "Use \$codex-security:security-diff-scan to review changes from $BASE_REVISION to $HEAD_REVISION for security regressions. Do not modify the checkout." after_script: - | unset CODEX_SECURITY_API_KEY scan_root="/tmp/codex-security-$CI_JOB_ID/codex-security-scans" if [ -d "$scan_root" ]; then tar -czf codex-security-artifacts.tar.gz -C "$scan_root" . fi artifacts: when: always paths: - codex-security-artifacts.tar.gztrigger: none
pool: vmImage: ubuntu-latest
steps: - checkout: self fetchDepth: 0
- bash: | set -euo pipefail export CODEX_HOME="$AGENT_TEMPDIRECTORY/codex-home" npm install --global @openai/codex codex plugin add codex-security@openai-curated displayName: Install Codex Security
- bash: | set -euo pipefail export CODEX_HOME="$AGENT_TEMPDIRECTORY/codex-home" export TMPDIR="$AGENT_TEMPDIRECTORY/codex-security" export HEAD_REVISION="$SYSTEM_PULLREQUEST_SOURCECOMMITID" export BASE_REVISION="$(git merge-base HEAD^1 "$HEAD_REVISION")" CODEX_API_KEY="$CODEX_SECURITY_API_KEY" codex exec \ --sandbox workspace-write \ "Use \$codex-security:security-diff-scan to review changes from $BASE_REVISION to $HEAD_REVISION for security regressions. Do not modify the checkout." displayName: Review code changes condition: and(succeeded(), ne(variables['System.PullRequest.IsFork'], 'True')) env: CODEX_SECURITY_API_KEY: $(CODEX_SECURITY_API_KEY)
- publish: $(Agent.TempDirectory)/codex-security/codex-security-scans artifact: codex-security-review condition: always()对于 Azure Repos,请配置一个 生成验证 分支策略,以在拉取请求上运行 流水线。
pipeline { agent { label 'linux' } stages { stage('Codex Security review') { when { allOf { changeRequest() expression { !env.CHANGE_FORK?.trim() } } } steps { sh '''#!/usr/bin/env bash set -euo pipefail export CODEX_HOME="/tmp/codex-home-$BUILD_TAG" export TMPDIR="/tmp/codex-security-$BUILD_TAG" mkdir -p "$TMPDIR" git fetch --no-tags origin "$CHANGE_TARGET" target="$(git rev-parse FETCH_HEAD)" git fetch --no-tags origin "$CHANGE_BRANCH" git rev-parse FETCH_HEAD > "$TMPDIR/head" git merge-base "$target" "$(cat "$TMPDIR/head")" > "$TMPDIR/base" npm install --global @openai/codex codex plugin add codex-security@openai-curated ''' withCredentials([string(credentialsId: 'codex-security-api-key', variable: 'CODEX_SECURITY_API_KEY')]) { sh '''#!/usr/bin/env bash set +x set -euo pipefail export CODEX_HOME="/tmp/codex-home-$BUILD_TAG" export TMPDIR="/tmp/codex-security-$BUILD_TAG" export HEAD_REVISION="$(cat "$TMPDIR/head")" export BASE_REVISION="$(cat "$TMPDIR/base")" CODEX_API_KEY="$CODEX_SECURITY_API_KEY" codex exec \ --sandbox workspace-write \ "Use \$codex-security:security-diff-scan to review changes from $BASE_REVISION to $HEAD_REVISION for security regressions. Do not modify the checkout." ''' } } post { always { sh '''#!/usr/bin/env bash set -euo pipefail scan_root="/tmp/codex-security-$BUILD_TAG/codex-security-scans" if [ -d "$scan_root" ]; then tar -czf codex-security-artifacts.tar.gz -C "$scan_root" . fi ''' archiveArtifacts artifacts: 'codex-security-artifacts.tar.gz', allowEmptyArchive: true } } } }}这些示例会跳过来自复刻的拉取请求。仅从
受保护的流水线定义运行带凭据的作业,并且仅面向受信任、可使用扫描
凭据的贡献者。归档 codex-security-scans 以将结构化发现项、
清单、覆盖工件、 report.md及其链接的 findings/ 和
hardening/ 输出保存在一起。先从建议性结果开始,并在将该作业设为必需检查前审查覆盖范围
和运行时间。
关于 API密钥处理和沙箱控制,请参阅 非交互式
模式。如果你的组织允许使用 Codex
GitHub Action,它可以在运行时安装 CLI ,但
你仍必须先安装插件,并将该 action 的 codex-home
输入指向同一个 CODEX_HOME。