differential-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<!-- Source: Trail of Bits | License: CC-BY-SA-4.0 | Adapted: 2026-02-09 -->
<!-- Agent: security-architect | Task: #4 | Session: 2026-02-09 -->
<!-- 来源:Trail of Bits | 许可证:CC-BY-SA-4.0 | 改编日期:2026-02-09 -->
<!-- Agent: security-architect | 任务:#4 | 会话:2026-02-09 -->
Differential Review
差异审查
Security Notice
安全声明
AUTHORIZED USE ONLY: These skills are for DEFENSIVE security analysis and authorized research:
- Pull request security review for owned repositories
- Pre-merge security validation in CI/CD pipelines
- Security regression detection in code changes
- Compliance validation of code modifications
- Educational purposes in controlled environments
NEVER use for:
- Reviewing code you are not authorized to access
- Exploiting discovered vulnerabilities without disclosure
- Circumventing code review processes
- Any illegal activities
仅限授权使用:这些技能仅用于防御性安全分析与授权研究:
- 自有代码仓库的拉取请求安全审查
- CI/CD流水线中的合并前安全验证
- 代码变更中的安全退化检测
- 代码修改的合规性验证
- 受控环境中的教育用途
严禁用于:
- 审查未经授权访问的代码
- 利用已发现的漏洞且不披露
- 规避代码审查流程
- 任何非法活动
Step 1: Obtain the Diff
步骤1:获取代码差异
Git Diff Methods
Git差异获取方法
bash
undefinedbash
undefinedReview staged changes
查看暂存区变更
git diff --cached
git diff --cached
Review specific commit
查看指定提交的变更
git diff HEAD~1..HEAD
git diff HEAD~1..HEAD
Review pull request (GitHub)
查看拉取请求差异(GitHub)
gh pr diff <PR-NUMBER>
gh pr diff <PR-NUMBER>
Review specific files
查看指定文件的变更
git diff --cached -- src/auth/ src/api/
git diff --cached -- src/auth/ src/api/
Review with context (10 lines)
带上下文查看差异(10行)
git diff -U10 HEAD~1..HEAD
git diff -U10 HEAD~1..HEAD
Show only changed file names
仅显示变更的文件名
git diff --name-only HEAD~1..HEAD
git diff --name-only HEAD~1..HEAD
Show stats (insertions/deletions per file)
显示变更统计(每个文件的新增/删除行数)
git diff --stat HEAD~1..HEAD
undefinedgit diff --stat HEAD~1..HEAD
undefinedClassify Changed Files
对变更文件分类
Prioritize review by security sensitivity:
| Priority | File Patterns | Reason |
|---|---|---|
| P0 | | Direct security code |
| P0 | | Configuration and secrets |
| P0 | | Security controls |
| P1 | | Attack surface |
| P1 | | Dependency changes |
| P1 | | Infrastructure config |
| P2 | | Data access layer |
| P2 | | Shared utility code |
| P3 | | Tests and documentation |
根据安全敏感度优先级进行审查:
| 优先级 | 文件模式 | 原因 |
|---|---|---|
| P0 | | 直接涉及安全的代码 |
| P0 | | 配置与密钥信息 |
| P0 | | 安全控制逻辑 |
| P1 | | 攻击面相关代码 |
| P1 | | 依赖变更 |
| P1 | | 基础设施配置 |
| P2 | | 数据访问层代码 |
| P2 | | 共享工具类代码 |
| P3 | | 测试与文档 |
Step 2: Security-Focused Diff Analysis
步骤2:聚焦安全的差异分析
Analysis Framework
分析框架
For each changed file, evaluate these security dimensions:
针对每个变更文件,从以下安全维度进行评估:
2.1 Input Validation Changes
2.1 输入验证变更
CHECK: Did the change modify input validation?
- Added validation: POSITIVE (verify correctness)
- Removed validation: CRITICAL (likely regression)
- Changed validation: INVESTIGATE (may weaken security)
- No validation on new input: WARNING (missing validation)Red Flags:
- Removing or weakening regex patterns
- Commenting out validation middleware
- Changing mode to
strictloose - Adding type or disabling type checks
any - Removing length limits or range checks
检查:变更是否修改了输入验证逻辑?
- 新增验证:积极项(验证正确性)
- 移除验证:严重问题(可能导致退化)
- 修改验证:需调查(可能弱化安全性)
- 新输入未做验证:警告(缺失验证)危险信号:
- 移除或弱化正则表达式规则
- 注释掉验证中间件
- 将模式改为
strictloose - 添加类型或禁用类型检查
any - 移除长度限制或范围校验
2.2 Authentication/Authorization Changes
2.2 身份验证/授权变更
CHECK: Did the change affect auth?
- New endpoint without auth middleware: CRITICAL
- Removed auth check: CRITICAL
- Changed permission levels: INVESTIGATE
- Modified token handling: INVESTIGATE
- Added new auth bypass: CRITICALRed Flags:
- Routes added without authentication middleware
- checks removed or weakened
isAdmin - Token expiry extended significantly
- Session management changes
- CORS policy relaxation
检查:变更是否影响认证逻辑?
- 新增端点未添加认证中间件:严重问题
- 移除认证检查:严重问题
- 修改权限级别:需调查
- 修改令牌处理逻辑:需调查
- 新增认证绕过机制:严重问题危险信号:
- 新增路由未配置认证中间件
- 检查被移除或弱化
isAdmin - 令牌过期时间大幅延长
- 会话管理逻辑变更
- CORS策略放宽
2.3 Data Flow Changes
2.3 数据流变更
CHECK: Did the change introduce new data flows?
- User input to database: CHECK for injection
- User input to HTML: CHECK for XSS
- User input to file system: CHECK for path traversal
- User input to command execution: CHECK for command injection
- User input to redirect: CHECK for open redirect检查:变更是否引入了新的数据流?
- 用户输入到数据库:检查注入风险
- 用户输入到HTML:检查XSS风险
- 用户输入到文件系统:检查路径遍历风险
- 用户输入到命令执行:检查命令注入风险
- 用户输入到重定向:检查开放重定向风险2.4 Cryptographic Changes
2.4 加密逻辑变更
CHECK: Did the change affect cryptography?
- Algorithm downgrade: CRITICAL (e.g., SHA-256 to MD5)
- Key size reduction: CRITICAL
- Removed encryption: CRITICAL
- Changed to ECB mode: CRITICAL
- Hardcoded key/IV: CRITICAL检查:变更是否影响加密逻辑?
- 算法降级:严重问题(如SHA-256改为MD5)
- 密钥长度缩短:严重问题
- 移除加密:严重问题
- 改为ECB模式:严重问题
- 硬编码密钥/IV:严重问题2.5 Error Handling Changes
2.5 错误处理变更
CHECK: Did the change affect error handling?
- Removed try/catch: WARNING
- Added stack trace in response: CRITICAL (info disclosure)
- Changed error to success: CRITICAL (fail-open)
- Swallowed exceptions: WARNING检查:变更是否影响错误处理逻辑?
- 移除try/catch:警告
- 响应中返回堆栈跟踪:严重问题(信息泄露)
- 将错误改为成功:严重问题(故障开放)
- 吞掉异常:警告2.6 Dependency Changes
2.6 依赖变更
CHECK: Did dependencies change?
- New dependency: CHECK for known CVEs
- Version downgrade: INVESTIGATE
- Removed security dependency: CRITICAL
- Changed to fork/alternative: INVESTIGATEbash
undefined检查:依赖是否发生变更?
- 新增依赖:检查已知CVE漏洞
- 版本降级:需调查
- 移除安全相关依赖:严重问题
- 替换为分支/替代库:需调查bash
undefinedCheck new dependencies for known vulnerabilities
检查新依赖的已知漏洞
npm audit
pip audit
go list -m -json all | nancy sleuth
undefinednpm audit
pip audit
go list -m -json all | nancy sleuth
undefinedStep 3: Inline Security Comments
步骤3:嵌入式安全注释
Comment Format
注释格式
For each finding, provide a structured inline comment:
markdown
**SECURITY [SEVERITY]**: [Brief description]
**Location**: `file.js:42` (in diff hunk)
**Category**: [OWASP/CWE category]
**Impact**: [What could go wrong]
**Remediation**: [How to fix]
```diff
- // Current (vulnerable)
- db.query("SELECT * FROM users WHERE id = " + userId);
+ // Suggested (safe)
+ db.query("SELECT * FROM users WHERE id = $1", [userId]);
```undefined针对每个发现的问题,提供结构化的嵌入式注释:
markdown
**安全 [严重级别]**:[简要描述]
**位置**:`file.js:42`(差异块中)
**分类**:[OWASP/CWE分类]
**影响**:[可能引发的风险]
**修复建议**:[修复方案]
```diff
- // 当前(存在漏洞)
- db.query("SELECT * FROM users WHERE id = " + userId);
+ // 建议(安全写法)
+ db.query("SELECT * FROM users WHERE id = $1", [userId]);
```Severity Levels for Diff Findings
差异问题的严重级别
| Severity | Criteria | Action |
|---|---|---|
| CRITICAL | Exploitable vulnerability introduced | Block merge |
| HIGH | Security regression or missing control | Block merge |
| MEDIUM | Weak pattern that could lead to vulnerability | Request changes |
| LOW | Style issue with security implications | Suggest improvement |
| INFO | Security observation, no immediate risk | Note for awareness |
| 严重级别 | 判定标准 | 处理动作 |
|---|---|---|
| CRITICAL(严重) | 引入可被利用的漏洞 | 阻止合并 |
| HIGH(高) | 安全退化或缺失控制逻辑 | 阻止合并 |
| MEDIUM(中) | 可能导致漏洞的弱模式 | 要求修改 |
| LOW(低) | 涉及安全的代码风格问题 | 建议改进 |
| INFO(信息) | 安全相关观察,无即时风险 | 备注提醒 |
Step 4: Differential Security Report
步骤4:差异安全报告
Report Template
报告模板
markdown
undefinedmarkdown
undefinedDifferential Security Review
差异安全审查报告
PR/Commit: [reference]
Author: [author]
Reviewer: security-architect
Date: YYYY-MM-DD
Files Changed: X | Additions: +Y | Deletions: -Z
PR/提交记录:[引用标识]
作者:[作者]
审查者:security-architect
日期:YYYY-MM-DD
变更文件数:X | 新增行数:+Y | 删除行数:-Z
Security Impact Summary
安全影响摘要
| Category | Before | After | Change |
|---|---|---|---|
| Input validation | X checks | Y checks | +/-N |
| Auth-protected routes | X routes | Y routes | +/-N |
| SQL parameterization | X% | Y% | +/-N% |
| Secrets exposure | X | Y | +/-N |
| 分类 | 变更前 | 变更后 | 变化 |
|---|---|---|---|
| 输入验证 | X项检查 | Y项检查 | +/-N |
| 认证保护路由 | X个路由 | Y个路由 | +/-N |
| SQL参数化比例 | X% | Y% | +/-N% |
| 密钥暴露风险 | X | Y | +/-N |
Findings
发现的问题
CRITICAL
严重级别
- [Finding with full details and remediation]
- [问题详情及修复建议]
HIGH
高风险
- [Finding with full details and remediation]
- [问题详情及修复建议]
MEDIUM
中风险
- [Finding with full details and remediation]
- [问题详情及修复建议]
Verdict
评审结论
- APPROVE: No security issues found
- APPROVE WITH CONDITIONS: Minor issues, fix before deploy
- REQUEST CHANGES: Security issues must be addressed
- BLOCK: Critical vulnerability introduced
undefined- 批准:未发现安全问题
- 有条件批准:存在次要问题,部署前修复
- 要求修改:必须解决安全问题
- 阻止合并:引入严重漏洞
undefinedStep 5: Automated Diff Scanning
步骤5:自动化差异扫描
Semgrep Diff Mode
Semgrep差异模式
bash
undefinedbash
undefinedScan only changed files
仅扫描变更的文件
semgrep scan --config=p/security-audit --baseline-commit=main
semgrep scan --config=p/security-audit --baseline-commit=main
Scan diff between branches
扫描分支间的差异
semgrep scan --config=p/security-audit --baseline-commit=origin/main
semgrep scan --config=p/security-audit --baseline-commit=origin/main
Output as SARIF for CI integration
输出为SARIF格式用于CI集成
semgrep scan --config=p/security-audit --baseline-commit=main --sarif --output=diff-results.sarif
undefinedsemgrep scan --config=p/security-audit --baseline-commit=main --sarif --output=diff-results.sarif
undefinedCustom Diff Security Checks
自定义差异安全检查
bash
undefinedbash
undefinedCheck for secrets in diff
检查差异中的密钥信息
git diff --cached | grep -iE "(password|secret|api.?key|token|credential)\s*[=:]"
git diff --cached | grep -iE "(password|secret|api.?key|token|credential)\s*[=:]"
Check for dangerous function additions
检查新增的危险函数
git diff --cached | grep -E "^+" | grep -iE "(eval|exec|system|innerHTML|dangerouslySetInnerHTML)"
git diff --cached | grep -E "^+" | grep -iE "(eval|exec|system|innerHTML|dangerouslySetInnerHTML)"
Check for removed security middleware
检查被移除的安全中间件
git diff --cached | grep -E "^-" | grep -iE "(authenticate|authorize|validate|sanitize|escape)"
git diff --cached | grep -E "^-" | grep -iE "(authenticate|authorize|validate|sanitize|escape)"
Check for new TODO/FIXME security items
检查新增的安全相关TODO/FIXME项
git diff --cached | grep -E "^+" | grep -iE "(TODO|FIXME|HACK|XXX).*(security|auth|vuln)"
undefinedgit diff --cached | grep -E "^+" | grep -iE "(TODO|FIXME|HACK|XXX).*(security|auth|vuln)"
undefinedGitHub Actions Integration
GitHub Actions集成
yaml
name: Security Diff Review
on: [pull_request]
jobs:
security-diff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Semgrep diff scan
uses: returntocorp/semgrep-action@v1
with:
config: p/security-audit
- name: Check for secrets
run: |
git diff origin/main..HEAD | grep -iE "(password|secret|api.?key|token)\s*[=:]" && exit 1 || exit 0yaml
name: Security Diff Review
on: [pull_request]
jobs:
security-diff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Semgrep diff scan
uses: returntocorp/semgrep-action@v1
with:
config: p/security-audit
- name: Check for secrets
run: |
git diff origin/main..HEAD | grep -iE "(password|secret|api.?key|token)\s*[=:]" && exit 1 || exit 0Common Security Regressions in Diffs
差异中常见的安全退化模式
| Pattern | What Changed | Risk |
|---|---|---|
Removed | Security headers removed | Header injection, clickjacking |
Changed | Cookie policy weakened | CSRF attacks |
| Removed rate limiting middleware | Rate limit removed | Brute force, DoS |
Added | CORS wildcard | Cross-origin attacks |
Removed | CSRF protection removed | CSRF attacks |
Changed | Cookie accessible to JS | XSS token theft |
| 模式 | 变更内容 | 风险 |
|---|---|---|
移除 | 安全头被移除 | 头注入、点击劫持 |
将 | Cookie策略弱化 | CSRF攻击 |
| 移除速率限制中间件 | 速率限制被移除 | 暴力破解、拒绝服务 |
新增 | CORS配置为通配符 | 跨源攻击 |
移除 | CSRF保护被移除 | CSRF攻击 |
将 | Cookie可被JS访问 | XSS令牌窃取 |
Related Skills
相关技能
- - Full codebase static analysis
static-analysis - - Pattern-based vulnerability discovery
variant-analysis - - Custom detection rules
semgrep-rule-creator - - Hardcoded credentials detection
insecure-defaults - - STRIDE threat modeling
security-architect
- - 全代码库静态分析
static-analysis - - 基于模式的漏洞发现
variant-analysis - - 自定义检测规则
semgrep-rule-creator - - 硬编码凭证检测
insecure-defaults - - STRIDE威胁建模
security-architect
Agent Integration
Agent集成
- code-reviewer (primary): Security-augmented code review
- security-architect (primary): Security assessment of changes
- penetration-tester (secondary): Verify exploitability of findings
- developer (secondary): Security-aware development guidance
- code-reviewer(主要):增强安全能力的代码审查
- security-architect(主要):变更的安全评估
- penetration-tester(次要):验证发现问题的可利用性
- developer(次要):安全意识开发指导
Memory Protocol (MANDATORY)
内存协议(强制要求)
Before starting:
Read
.claude/context/memory/learnings.mdAfter completing:
- New pattern ->
.claude/context/memory/learnings.md - Issue found ->
.claude/context/memory/issues.md - Decision made ->
.claude/context/memory/decisions.md
ASSUME INTERRUPTION: If it's not in memory, it didn't happen.
开始前:
阅读
.claude/context/memory/learnings.md完成后:
- 新发现的模式 ->
.claude/context/memory/learnings.md - 发现的问题 ->
.claude/context/memory/issues.md - 做出的决策 ->
.claude/context/memory/decisions.md
假设可能中断:未记录在内存中的内容视为未发生。