Loading...
Loading...
Compare original and translation side by side
| Boundary | What to validate |
|---|---|
| HTTP requests | Body, query params, headers, path params — type, length, format, range |
| File uploads | Type, size, content (not just extension), filename sanitization |
| Database reads | Assume data could be corrupted — validate on read if used in security decisions |
| Environment variables | Type, format, required vs optional — fail fast on invalid config |
| External API responses | Schema validation — don't trust upstream services blindly |
| User-generated content | Sanitize for XSS, SQL injection, path traversal, command injection |
| 边界 | 校验内容 |
|---|---|
| HTTP 请求 | 请求体、查询参数、请求头、路径参数 — 校验类型、长度、格式、取值范围 |
| 文件上传 | 文件类型、大小、实际内容(不单单校验扩展名)、文件名 sanitization |
| 数据库读取 | 假设数据可能已损坏,若用于安全决策则读取时必须校验 |
| 环境变量 | 校验类型、格式、必填/可选属性,配置非法时快速失败 |
| 外部 API 响应 | 做 schema 校验,不要盲目信任上游服务 |
| 用户生成内容 | 做XSS、SQL注入、路径遍历、命令注入防护的 sanitization 处理 |
| Principle | Requirement |
|---|---|
| Authentication | Every endpoint must know who is calling. No anonymous access unless explicitly designed. |
| Authorization | Every operation must check if the caller is ALLOWED to do this. Auth ≠ authz. |
| Least privilege | Grant the minimum permissions needed. No admin-by-default. |
| Session management | Secure tokens (HttpOnly, Secure, SameSite), reasonable TTL, revocation support. |
| Password handling | bcrypt/scrypt/argon2 only. Never MD5/SHA for passwords. Never store plaintext. |
| API keys | Scoped, rotatable, revocable. Never embedded in client-side code. |
| 原则 | 要求 |
|---|---|
| 身份认证(Authentication) | 每个接口必须识别调用方身份,除非明确设计为匿名访问 |
| 权限授权(Authorization) | 每个操作都必须校验调用方是否拥有执行权限,身份认证≠权限授权 |
| 最小权限原则 | 授予操作所需的最低权限,默认不分配管理员权限 |
| 会话管理 | 使用安全令牌(开启HttpOnly、Secure、SameSite属性)、合理的有效期、支持吊销机制 |
| 密码处理 | 仅使用bcrypt/scrypt/argon2算法加密,禁止用MD5/SHA处理密码,永远不存储明文密码 |
| API 密钥 | 限定作用范围、支持轮换、支持吊销,永远不要嵌入到客户端代码中 |
.env.gitignore.gitignore.envLayer 1: Network — firewalls, VPN, TLS everywhere
Layer 2: Application — input validation, output encoding
Layer 3: Authentication — verify identity at every boundary
Layer 4: Authorization — check permissions for every operation
Layer 5: Data — encryption at rest and in transit
Layer 6: Monitoring — log security events, alert on anomaliesLayer 1: Network — firewalls, VPN, TLS everywhere
Layer 2: Application — input validation, output encoding
Layer 3: Authentication — verify identity at every boundary
Layer 4: Authorization — check permissions for every operation
Layer 5: Data — encryption at rest and in transit
Layer 6: Monitoring — log security events, alert on anomalies| Practice | Frequency |
|---|---|
| Audit dependencies for known CVEs | Every build (CI) |
| Pin dependency versions | Always (lockfile) |
| Review new dependencies before adding | Every time |
| Remove unused dependencies | Every release |
| Prefer well-maintained dependencies | >1000 stars, active maintainer, no critical CVEs |
| 实践 | 执行频率 |
|---|---|
| 扫描依赖的已知CVE漏洞 | 每次构建(CI流程中) |
| 锁定依赖版本 | 始终(用锁文件) |
| 新增依赖前做评审 | 每次新增时 |
| 移除未使用的依赖 | 每次发版时 |
| 优先选择维护良好的依赖 | Star数>1000、维护者活跃、无严重CVE漏洞的依赖 |
| Attack | Prevention |
|---|---|
| SQL Injection | Parameterized queries ONLY. Never string concatenation. |
| XSS | Context-aware output encoding. CSP headers. |
| Command Injection | Never pass user input to shell commands. Use libraries. |
| Path Traversal | Validate and canonicalize file paths. Never use raw user input. |
| SSRF | Allowlist outbound URLs. Block internal network ranges. |
| Deserialization | Never deserialize untrusted data with native serializers. Use JSON. |
| CSRF | Anti-CSRF tokens for state-changing operations. SameSite cookies. |
| 攻击类型 | 防护手段 |
|---|---|
| SQL 注入 | 仅使用参数化查询,永远不要拼接SQL字符串 |
| XSS | 上下文感知的输出编码,配置CSP请求头 |
| 命令注入 | 永远不要把用户输入传给shell命令,使用封装好的库 |
| 路径遍历 | 校验并规范化文件路径,永远不要直接使用原始用户输入 |
| SSRF | 对外访问URL用允许名单限制,屏蔽内部网络地址段 |
| 反序列化漏洞 | 永远不要用原生序列化器反序列化不可信数据,使用JSON |
| CSRF | 状态变更操作添加CSRF令牌,使用SameSite属性的Cookie |
| # | Risk | Key Defense |
|---|---|---|
| A01 | Broken Access Control | Authz checks on every operation, deny by default |
| A02 | Cryptographic Failures | TLS everywhere, strong algorithms, no plaintext secrets |
| A03 | Injection | Parameterized queries, input validation |
| A04 | Insecure Design | Threat modeling during planning, not after |
| A05 | Security Misconfiguration | Secure defaults, no default credentials |
| A06 | Vulnerable Components | Dependency scanning, pinned versions |
| A07 | Auth Failures | MFA, rate limiting, secure session management |
| A08 | Data Integrity Failures | Signed updates, CI/CD security, input validation |
| A09 | Logging Failures | Log security events, don't log secrets, monitor |
| A10 | SSRF | Allowlist outbound URLs, block internal ranges |
| 序号 | 风险 | 核心防御手段 |
|---|---|---|
| A01 | 访问控制失效 | 每次操作都做权限校验,默认拒绝 |
| A02 | 加密失效 | 全链路开启TLS,使用强加密算法,不存储明文密钥 |
| A03 | 注入漏洞 | 使用参数化查询,做输入校验 |
| A04 | 不安全设计 | 规划阶段做威胁建模,不要事后补 |
| A05 | 安全配置错误 | 安全默认配置,不使用默认凭证 |
| A06 | 存在漏洞的组件 | 依赖漏洞扫描,锁定依赖版本 |
| A07 | 身份认证失效 | 开启MFA,接口限流,安全的会话管理 |
| A08 | 数据完整性失效 | 签名更新,CI/CD安全防护,输入校验 |
| A09 | 日志与监控失效 | 记录安全事件,不打印密钥,做异常监控 |
| A10 | SSRF | 对外访问URL用允许名单限制,屏蔽内部地址段 |
| Pattern | Problem | Fix |
|---|---|---|
| "Security through obscurity" | Hiding ≠ protecting | Proper access controls |
| Trusting client-side validation | Trivially bypassed | Server-side validation always |
Broad CORS ( | Any origin can access API | Explicit origin allowlist |
| Admin-by-default | Over-privileged users | Least privilege, role-based access |
| Logging everything | Secrets in logs | Structured logging with redaction |
| Rolling your own crypto | Guaranteed vulnerabilities | Use vetted libraries (libsodium, etc.) |
| 反模式 | 问题 | 修复方案 |
|---|---|---|
| “模糊式安全” | 隐藏不等于保护 | 做完善的访问控制 |
| 只依赖客户端校验 | 可以轻松绕过 | 永远要做服务端校验 |
宽松CORS配置( | 任何来源都可以访问API | 明确配置允许的来源名单 |
| 默认管理员权限 | 用户权限过大 | 最小权限原则,基于角色的权限控制 |
| 不加区分打印所有日志 | 日志中泄露密钥 | 结构化日志+自动脱敏 |
| 自己实现加密算法 | 必然存在漏洞 | 使用经过验证的加密库(如libsodium等) |
| Excuse | Reality |
|---|---|
| "It's just an internal tool" | Internal tools get compromised. Insider threats are real. |
| "We'll add security later" | Later never comes. Vulnerabilities ship. |
| "Nobody would try that" | They will. And they have automated tools. |
| "It's behind a firewall" | Firewalls get bypassed. Defense in depth. |
| "We don't have sensitive data" | User emails, passwords, and usage patterns are sensitive. |
| "This is just a prototype" | Prototypes become production. Secure from day one. |
| 借口 | 现实 |
|---|---|
| “这只是个内部工具” | 内部工具也会被攻陷,内部威胁是真实存在的 |
| “我们后面再加安全” | 后面永远不会来,漏洞会直接上线 |
| “没人会这么攻击的” | 他们会的,而且还有自动化攻击工具 |
| “它在防火墙后面” | 防火墙可以被绕过,要做深度防御 |
| “我们没有敏感数据” | 用户邮箱、密码、使用行为都是敏感数据 |
| “这只是个原型” | 原型最终会变成生产代码,从第一天就做安全 |