审查代码变更中的安全问题
以手动方式或在 CI/CD 中审查PR和本地变更,查找安全回归问题。
运行安全变更审查,以查找 Git 支持的单个变更集中的回归问题。 Codex 会审查每个发生变更的源代码类文件及其直接相关的支持代码。 它不会将审查范围扩大为完整的仓库审计。
要扫描整个仓库而非特定变更,请参阅运行安全 扫描。
运行手动审查
在桌面应用中,打开 Security,选择 Scans,然后选择 + Scan。 选择仓库,然后选择 Changes。审查未提交的变更、 单个提交,或基准修订和头部修订。变更扫描不支持 Deep scan。
你也可以在对话中要求 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.如果本地检出中存在PR的基准修订和头部修订, 你也可以直接指定该PR。
在设置过程中确认变更
- 选择 Changes。
- 确认已检出的仓库、当前分支和最新提交。
- 在 Changes to review 下选择:
Uncommitted changes,用于审查当前工作树。- 最新提交,用于审查单个提交。
- 基准修订和头部修订,用于审查分支或PR范围。
- 确认摘要描述的是你打算审查的变更。
- 选择 Start scan。
Codex 不会检出其他分支,也不会切换所选工作树。如果 请求的修订版本在本地不可用,请在审查前获取该版本,或 提供本地可用的基准和目标修订版本。
处理发现
查看结果后,修复并验证已接受的 发现,或导出并跟踪 发现。
在 CI/CD 中自动执行审查
有关独立 CLI 的结构化 JSON、严重性策略和 SARIF 上传,请参阅在 CI 中运行 Codex Security
。继续阅读本节,了解如何通过
codex exec 调用已安装的插件技能。
当运行器可以在无需交互的情况下调用 Codex CLI 时,在 CI 中运行 $codex-security:security-diff-scan。
首先,在不暴露扫描凭据的情况下安装 CLI:
npm install --global @openai/codex在 CLI 中安装 Codex Security 插件:
codex plugin add codex-security@openai-curated安装命令使用公开的 Codex CLI 插件市场。有关特定版本的要求,请参阅插件更新日志。
接下来,将 CI 密钥存储中的 OpenAI API key 提供为
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 |
已审查及暂缓审查的范围、排除项和覆盖完整性。 |
有关字段定义,请参阅发现模式。使用 findingId 标识发现,使用 occurrenceId 标识该发现
在本次扫描中的具体实例。
例如,以下命令会为每项发现输出一行以制表符分隔的数据:
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.gz trigger: 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,请配置 Build validation 分支策略,以便在 PR上运行流水线。
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
}
}
}
}
} 这些示例会跳过来自复刻仓库的PR。只有使用受保护的
流水线定义,并且贡献者可信、可以接触扫描
凭据时,才运行需要凭据的作业。归档 codex-security-scans,将结构化发现、
清单、覆盖范围和 report.md 保存在一起,并同时保留所有按要求生成的
findings/ 或 hardening/ 输出。先从建议性结果开始,并在将该作业
设为必需检查前审查覆盖范围和运行时间。
有关 API key 处理和沙箱控制的信息,请参阅非交互
模式。如果你的组织允许使用 Codex
GitHub Action,它可以在运行时安装 CLI,但你
仍须先安装插件,并将该 Action 的 codex-home 输入指向
同一个 CODEX_HOME。