为开发团队提供安全检查表、代码审查视角和策略模板。
开发新 API 端点前需要验证的项目。
在 API 代码审查期间要检查的安全问题。
| 检查项目 | 重要性 | 说明 |
|---|---|---|
| 身份验证中间件应用于所有端点 | 需要 | 确保除公共应用程序接口外,不会缺少任何身份验证检查 |
| 存在对象级授权检查 | 需要 | 验证请求的资源是否属于请求用户 |
| 管理功能具有角色验证功能 | 需要 | 确保普通用户无法调用管理员 API |
| 在 JWT 验证过程中指定算法 | 需要 | 预防算法:无攻击 |
| 令牌过期已正确配置 | 推荐 | 访问令牌:15 分钟内 |
| 检查项目 | 重要性 | 说明 |
|---|---|---|
| 请求正文已进行模式验证 | 需要 | 类型、长度和格式的验证 |
| SQL 查询参数化 | 需要 | 确保不使用字符串连接创建查询 |
| 存在路径参数格式验证 | 需要 | 检查预期格式,如 UUID |
| 分页参数有上限 | 推荐 | 防止 limit=999999 等过度请求 |
| 上传文件有类型和大小限制 | 推荐 | 验证内容类型和文件大小 |
| 检查项目 | 重要性 | 说明 |
|---|---|---|
| 回复不包含不必要的字段 | 需要 | 防止泄漏密码哈希值、内部 ID 等。 |
| 错误响应不包含堆栈跟踪 | 需要 | 不要在生产中暴露内部信息 |
| 配置了安全标头 | 推荐 | X-Content-Type-Options、HSTS 等。 |
| 内容类型已正确设置 | 推荐 | 明确指定应用程序/json |
| 检查项目 | 重要性 | 说明 |
|---|---|---|
| 应用速率限制 | 需要 | 尤其是身份验证和支付终端 |
| 记录适当的日志 | 推荐 | 可追溯:谁在何时做了什么 |
| 秘密不是硬编码 | 需要 | 使用环境变量或秘密管理器 |
| CORS 设置适当 | 推荐 | 避免使用通配符 |
| 依赖项没有已知漏洞 | 推荐 | 检查 npm 审核/Snyk 的结果 |
用于内部 API 开发的基本政策模板。根据您的项目进行定制。
认证方法 - 面向用户的应用程序接口:OAuth 2.0(授权码 + PKCE) - 服务器到服务器 API:OAuth 2.0(客户端凭证)或 mTLS - 外部集成 API:API 密钥 + HMAC 签名 令牌管理 - 访问令牌过期:15 分钟 - 刷新令牌有效期:7 天(需要轮换) - 令牌存储:仅 HttpOnly + 安全 + SameSite Cookie 密码政策 - 至少 12 个字符,至少一个大写字母、一个小写字母、一个数字和一个符号 - 使用 bcrypt 进行散列(成本系数 12 或更高) - 密码列表攻击保护(与 Have I Been Pwned API 集成)
版本管理 - 通过 URL 路径进行版本管理:/api/v1/resources - 旧版本至少 6 个月的支持期 - 通过 Deprecation 标头通知版本更新 速率限制(默认值) - 一般 API:100 个请求/15 分钟 - 认证 API:5 个请求/15 分钟 - 公共 API:30 个请求/分钟 - 始终返回 RateLimit-* 标头 响应 - 内容类型:application/json(固定格式) - 错误响应采用 RFC 7807(问题详情)格式 - 在生产中从不返回堆栈跟踪 - 分页:默认 20 项,最多 100 项
必填日志字段 - 时间戳(ISO 8601,UTC) - 请求 ID(UUID,用于追踪) - 用户 ID / API 客户端 ID - HTTP 方法 + 端点 - 状态代码 - 源 IP - 响应时间 禁止日志字段(敏感数据) - 密码/令牌/API 密钥 - 信用卡号 - 完整显示 PII(需要屏蔽) 监控警报 - 认证失败:每分钟 5 次以上时发出警报 - 403 错误:每分钟 10 次以上时发出警报 - 500 错误:发生 1 次立即发出警报 - 超过速率限制:模式分析
通过监控警报、用户报告或外部通知检测事件。通过初步分流确定严重程度。
0-30 分钟防止破坏升级。使 API 密钥失效,暂时中止受影响的端点,并确定影响范围。
30 分钟 - 2 小时修复漏洞、应用补丁并恢复服务。通知受影响的用户。
2-24 小时进行事后分析。进行根本原因分析,制定防止再次发生的措施,并将结果记录在案。
1-5 个工作日Additional checklist items for systems integrating AI models and LLM-powered features.
Related: LLM06: Excessive Agency, LLM02: Insecure Output Handling
Related: ASI01: Excessive Agency, ASI03: Insecure Tool/Function Calling, ASI09: Inadequate Sandboxing
Related: LLM03: Training Data Poisoning, ML02: Data Poisoning Attack, ML06: AI Supply Chain Attacks
Security check items specific to code that integrates LLMs, AI agents, and ML models.
| 检查项目 | 重要性 | 说明 |
|---|---|---|
| System prompts are not exposed in client-side code or API responses | 需要 | Prompt leakage enables targeted prompt injection attacks |
| LLM outputs are validated before rendering (HTML/JS/SQL) | 需要 | LLM-generated content may contain XSS payloads or injection vectors |
| User input and system instructions are clearly separated in prompts | 需要 | Prevents direct prompt injection by maintaining instruction-data boundary |
| Tool/function call permissions are scoped per user role | 需要 | Prevent privilege escalation through agent tool access |
| Token count limits are enforced per request and per session | 推荐 | Prevents cost explosion and denial-of-wallet attacks |
| RAG retrieval results are sanitized before insertion into prompts | 需要 | Retrieved documents may contain indirect prompt injection payloads |
| Agent actions and tool calls are logged with full audit trail | 推荐 | Essential for incident investigation and compliance in AI systems |
Policy templates for organizations deploying AI models and LLM-powered applications.
■ Model Access Tiers - Tier 1 (Restricted): GPT-4 class / fine-tuned models — requires team lead approval - Tier 2 (Standard): GPT-3.5 class / embeddings — available to all developers - Tier 3 (Open): Open-source models (local inference) — no approval required ■ API Key Management for LLM Providers - One API key per service/environment (never share across projects) - Monthly spend alerts at 50%, 80%, 100% of budget - Hard spending caps enforced at the provider level - Key rotation: every 90 days or immediately upon team member departure ■ Data Sent to External Models - NEVER send: PII, credentials, internal IP, source code, customer data - ALLOWED with review: anonymized logs, public documentation, synthetic data - All prompts to external APIs must be logged (excluding PII)
■ Training Data Requirements - All training data must have documented provenance and licensing - PII must be removed or anonymized before use in training/fine-tuning - Data poisoning checks: validate data integrity with hash verification - Retain training data snapshots for reproducibility and audit ■ RAG (Retrieval-Augmented Generation) Data - Document ingestion pipeline must sanitize content (strip scripts, injections) - Access control on vector store must mirror source document permissions - Embedding models must be versioned and pinned ■ Model Output Data - LLM outputs must not be trusted as authoritative — always verify facts - Generated code must pass the same security review as human-written code - Outputs containing PII must be flagged and redacted before storage
■ Severity Levels for AI-Specific Incidents - P1 (Critical): Prompt injection leading to data exfiltration or unauthorized actions - P1 (Critical): Model serving compromised or returning manipulated outputs - P2 (High): Training data poisoning detected, jailbreak bypass discovered - P2 (High): Agent performing unintended actions outside approved scope - P3 (Medium): Model drift causing degraded accuracy below threshold - P4 (Low): Cost overrun due to excessive token usage ■ Response Procedures - P1: Immediately disable affected model endpoint, notify security team - P2: Quarantine affected model version, roll back to last known good - P3: Trigger retraining pipeline, increase monitoring frequency - P4: Adjust rate limits and budget caps, review usage patterns ■ Post-Incident Requirements - Root cause analysis within 48 hours for P1/P2 - Update prompt injection test suite with new attack vectors - Review and update guardrails configuration
应用程序接口设计中使用的与安全相关的 HTTP 状态代码。
| 代码 | 意义 | 使用案例 |
|---|---|---|
| 400 | Bad Request | 输入验证错误 |
| 401 | Unauthorized | 需要验证或令牌无效 |
| 403 | Forbidden | 已验证但权限不足 |
| 404 | Not Found | 资源不存在(也可用于替代 403) |
| 405 | Method Not Allowed | 不允许使用 HTTP 方法 |
| 413 | Payload Too Large | 超出请求机身尺寸 |
| 422 | Unprocessable Entity | 语法正确,但语义错误 |
| 429 | Too Many Requests | 超出速率限制 |
| 500 | Internal Server Error | 内部服务器错误(隐藏详情) |