審查程式碼變更中的安全問題
審查程式碼變更中的安全問題
以手動方式或在 CI/CD 中審查拉取請求和本機變更,查詢安全迴歸問題。
執行安全變更審查,以查詢 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.如果本機檢出中存在拉取請求的基準修訂和頭部修訂, 你也可以直接指定該拉取請求。
在設定過程中確認變更
- 選擇 Changes。
- 確認已檢出的儲存庫、當前分支和最新提交。
- 在 Changes to review 下選擇:
Uncommitted changes,用於審查當前工作樹。- 最新提交,用於審查單個提交。
- 基準修訂和頭部修訂,用於審查分支或拉取請求範圍。
- 確認摘要描述的是你打算審查的變更。
- 選擇 Start scan。
此工作流程不會檢出其他分支,也不會更改所選的工作樹。 如果請求的修訂在本機不可用,請在審查前將其提取到本機, 或提供本機可用的基準修訂和頭部修訂。
處理發現
審查結果後,請修復並驗證已接受的 發現,或匯出並跟蹤 發現。
在 CI/CD 中自動執行審查
當執行器可以在無需互動的情況下呼叫 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 中依賴 特定外掛版本或功能之前,請檢視外掛更新日誌。
然後,僅在掃描期間將 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."掃描會將輸出寫入
$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.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 分支策略,以便在 拉取請求上執行流水線。
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 key 處理和沙箱控制的資訊,請參閱非互動
模式。如果你的組織允許使用 Codex
GitHub Action,它可以在執行時安裝 CLI,但你
仍須先安裝外掛,並將該 Action 的 codex-home 輸入指向
同一個 CODEX_HOME。