P PasteCode
检查清单

审查AI生成的API路由清单

一份关于AI编码助手编写的REST和RPC API路由的人工审查清单——包括认证、输入验证、速率限制和错误处理。

CursorClaude CodeCodexWindsurf Next.jsTypeScriptPostgreSQL
.md .json 更新于 2026年6月8日

API路由是您应用程序的攻击面。AI生成的路由只处理了快乐路径,却跳过了认证检查,忘记了输入验证,并向匿名用户返回堆栈跟踪。

正确性

[ ] Route only accepts the HTTP methods it handles — return 405 for others
[ ] Request body is parsed with a schema validator (Zod, Valibot) before any use
[ ] Path parameters are validated and typed — not passed raw to a database query
[ ] Query string parameters are validated — undefined values do not cause runtime errors
[ ] Response has the correct Content-Type header (application/json for JSON responses)
[ ] Pagination parameters have upper bounds — page size is capped server-side
[ ] Empty results return 200 with an empty array, not 404 (unless the resource itself is missing)
[ ] Created resources return 201, not 200
[ ] All code paths return a response — no async handler that falls through without sending
[ ] Async errors are caught — unhandled promise rejections crash Node.js
[ ] File upload handlers validate MIME type and file size before processing
[ ] Date and time inputs are validated as ISO 8601 and parsed with a safe library

安全性

[ ] Every protected route checks the session or token before any other logic runs
[ ] Authentication check is the first statement in the handler — not after logging or parsing
[ ] Authorization is checked — authenticated user can only access their own resources
[ ] Tenant or user ID used in DB queries comes from the session, not the request body or URL
[ ] IDOR (Insecure Direct Object Reference) is prevented — resource IDs are scoped to the user
[ ] Rate limiting is applied to mutation endpoints and authentication-adjacent routes
[ ] Mass assignment is prevented — only explicitly listed fields from the request are written to the DB
[ ] Error responses do not include stack traces, database errors, or internal paths
[ ] Sensitive fields (passwords, tokens, secrets) are excluded from response serialization
[ ] CORS is configured explicitly — Access-Control-Allow-Origin is not set to * for credentialed endpoints
[ ] Content-Type is validated on POST/PUT/PATCH — reject requests without application/json where expected
[ ] SQL and NoSQL injection is prevented — all queries use parameterized statements
[ ] Path traversal is prevented — file paths constructed from user input are resolved and validated
[ ] Uploaded files are stored outside the web root and served via a signed URL, not a direct path

性能

[ ] Database queries inside the handler use indexes — check EXPLAIN for slow paths
[ ] N+1 queries in list endpoints are batched or replaced with a JOIN
[ ] Expensive operations are offloaded to a background queue — respond 202 Accepted
[ ] Response payloads contain only the fields the client needs — no full DB row serialization
[ ] Streaming is used for large responses — do not buffer gigabytes in memory
[ ] Caching headers (Cache-Control, ETag) are set on read-heavy public endpoints
[ ] Connection pooling is configured for the database client — no new connection per request

部署

[ ] API routes are not publicly accessible before authentication middleware is active
[ ] Environment-specific base URLs are configured — no hardcoded localhost URLs
[ ] Health check endpoint (/health or /api/health) returns 200 and is excluded from auth
[ ] API versioning strategy is consistent — /api/v1/... if versioning is used at all
[ ] Rate limit configuration uses a distributed store (Redis) in multi-instance deployments
[ ] Request logging redacts sensitive fields — no passwords or tokens in logs
[ ] Timeout is set on upstream fetch calls — no route that hangs indefinitely on a slow service

AI特有风险

[ ] AI has not put business logic before the auth check — always authenticate first
[ ] AI has not used parseInt() on user input without NaN handling — use Zod's z.coerce.number()
[ ] AI has not returned the full error object to the client — err.message can leak internals
[ ] AI has not used req.body without confirming body parsing middleware is active
[ ] AI has not skipped the OPTIONS handler on CORS-enabled routes
[ ] AI has not used a synchronous bcrypt or crypto call on the hot request path
[ ] AI has not generated a catch block that swallows errors silently — always log and respond
[ ] API keys or secrets are not accepted in query params — they appear in access logs

修复提示

Fix Prompt
Review this API route against the checklist above. Move the authentication check
to the top, add Zod validation for all inputs, scope database queries to the
authenticated user's ID, remove internal error details from responses, and add
rate limiting. Return the corrected route handler only, with comments on each
security fix applied.