審查程式碼變更中的安全問題
以手動方式或在 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。