secure-code-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSecure Code Review
安全代码审查
Review code for vulnerabilities systematically, not line-by-line.
系统性地审查代码中的漏洞,而非逐行检查。
1. Orient
1. 定位
- What does this code do? Identify: entry points, trust boundaries, data stores, privileged operations
- Read the tests — what invariants do they reveal?
- Check the diff's blast radius: auth logic? parsing? file handling? crypto?
- 这段代码的功能是什么?明确:入口点、信任边界、数据存储、特权操作
- 阅读测试用例——它们揭示了哪些不变量?
- 检查代码变更的影响范围:是否涉及认证逻辑?解析?文件处理?加密?
2. Trace Untrusted Data
2. 跟踪不可信数据
Follow each input from entry point to sink:
| Sink Class | What to Verify |
|---|---|
| SQL/NoSQL | Parameterized; no string-built queries; identifiers whitelisted |
| Command exec | No user data in shell strings; argv-array APIs; no shell=True |
| HTML/rendering | Contextual auto-escaping; raw/unsafe HTML flags justified |
| File paths | Basename/allowlist; canonicalize + prefix check; no user paths in includes |
| Deserialization | Typed formats (JSON) over object serializers; validation post-parse |
| Redirects | Relative-only or allowlisted targets |
| Eval/dynamic code | Justified and input-free, or rejected |
追踪每个输入从入口点到接收端的路径:
| 接收端类别 | 验证要点 |
|---|---|
| SQL/NoSQL | 使用参数化查询;禁止拼接字符串构建查询;标识符需在白名单内 |
| 命令执行 | 命令字符串中不包含用户数据;使用参数数组API;禁止设置shell=True |
| HTML/渲染 | 按上下文自动转义;合理使用原始/不安全HTML标记 |
| 文件路径 | 使用文件名/白名单;规范化路径并检查前缀;引入文件时不使用用户提供的路径 |
| 反序列化 | 优先使用类型化格式(如JSON)而非对象序列化器;解析后进行验证 |
| 重定向 | 仅允许相对路径或白名单内的目标地址 |
| 求值/动态代码 | 仅在合理且无用户输入的情况下使用,否则拒绝 |
3. Audit Auth and Access Control
3. 审计认证与访问控制
- Every endpoint enforces authz server-side; role checks at the resource, not the controller only
- Object-level checks (IDOR): does the query filter by the caller's tenant/user ID?
- Session management: rotation, invalidation, secure cookie flags
- Password reset flows: token entropy, expiry, single-use, no account enumeration
- 每个端点都在服务端强制执行授权;在资源层面而非仅控制器层面进行角色检查
- 对象级检查(IDOR):查询是否按调用者的租户/用户ID进行过滤?
- 会话管理:会话轮换、失效机制、安全Cookie标记
- 密码重置流程:令牌熵值、过期时间、单次使用、禁止账户枚举
4. Audit Secrets and Config
4. 审计密钥与配置
- No hardcoded credentials/keys/API tokens; no secrets in logs or error messages
- Crypto: approved algorithms, library primitives (not hand-rolled), correct modes, random from CSPRNG
- 禁止硬编码凭证/密钥/API令牌;密钥不出现于日志或错误信息中
- 加密:使用经批准的算法、库原语(而非自行实现)、正确的模式,采用CSPRNG生成随机数
5. Race and State
5. 竞争条件与状态
- TOCTOU on file checks, check-then-use on quotas/credits
- Concurrency on mutable shared state; missing transactions on multi-step writes
- 文件检查时的TOCTOU问题,配额/积分检查后使用的问题
- 可变共享状态的并发问题;多步骤写入时缺失事务处理
Communication
沟通
Report findings with severity, the specific code path, an exploit sketch, and a suggested fix. Distinguish "must fix" from "harden later."
报告发现时需包含严重程度、具体代码路径、漏洞利用示意图及修复建议。区分"必须修复"与"后续强化"的问题。