路由处理器变更缺陷审查
审查路由改动的响应路径、异步异常及中间件顺序,输出可复现缺陷、严重程度与补测建议。
完整提示词
<context>
Express bugs are mostly control-flow bugs. In Express 4, a rejected promise from an `async` handler is not passed to the error middleware: the unhandled rejection crashes the process on Node 15 and later (or, if an `unhandledRejection` listener swallows it, the request hangs until the client times out), unless the handler catches and calls `next(err)` or a wrapper does it. Express 5 forwards rejected promises, so the version decides the finding. Responding twice is the other classic: a branch that calls `res.status(400).json(...)` without `return` falls through to a second `res.json`, which throws "Cannot set headers after they are sent", and calling `next()` after responding does the same downstream. Middleware runs in registration order: body parsers registered after a router leave `req.body` undefined, an auth middleware added below a route does not protect it, and the error handler must take four arguments and be registered last. Request input is less typed than it looks: `req.query.id` can be an array (`?id=1&id=2`) and, with Express 4's default `extended` query parser, an object (`?id[$ne]=1`), which turns into NoSQL injection when passed straight to MongoDB and into type confusion elsewhere; `parseInt` returns `NaN` silently. `req.ip` and `X-Forwarded-For` are only trustworthy with the right `trust proxy` setting. Module-level variables are shared by all concurrent requests.
</context>
<task>
<diff>
{{diff}}
</diff>
{{#context}}
<context_given>
{{context}}
</context_given>
{{/context}}
1. If you can read the repository, open each changed handler in full, the app setup file that registers middleware and routers, and the error middleware, before judging any hunk. If you only have the diff, say which conclusions depend on code you could not see.
2. Trace every path through each changed handler and confirm it ends in exactly one response or one `next(err)`: missing `return` after a response, responses inside callbacks plus another after them, `next()` called after responding, and paths that end without responding at all.
3. Check async error flow for the stated Express version: async handlers or middleware without try/catch or a wrapper on Express 4, promises not awaited (floating), errors thrown inside callbacks or event handlers that no middleware can catch, and stream `pipe` without error handling.
4. Check middleware order and registration: parsers, auth, rate limiting and CORS registered before the routes they must cover, routers mounted at the intended path, and the four-argument error handler registered last.
5. Check request input: types of `req.query`, `req.params` and `req.body` assumed rather than validated, objects or arrays passed into database queries, `parseInt` or `Number` results used without a `NaN` check, missing size limits on bodies or uploads, and `req.ip` used for security decisions without `trust proxy` configured.
6. Check shared state and responses: module-level mutable variables written per request, error responses that send stack traces or internal messages, and status codes left at the default 200 for failures.
7. For each finding, write the concrete input, request or sequence of events that triggers it and what goes wrong. Drop any finding you cannot trigger from the code you have read.
8. Rate each finding: high (hanging requests or crashes from unhandled rejections, auth bypass from middleware order, injection through query objects), medium (double-response errors, wrong status codes clients rely on), low.
9. List the tests that would fail on the current code and pass once it is fixed.
</task>
<constraints>
- State the Express version you assumed; if it is unknown, say which findings apply only to Express 4.
- Use the validation library and error-wrapper pattern the codebase already has; do not introduce a new framework.
- At most 10 findings, most severe first. No style, naming or formatting comments unless they cause a defect.
- Stay read-only: do not edit files, push, or run commands that change state. Running read-only checks (type checker, linter, tests) is fine and worth reporting.
{{> guardrails/investigate-before-answering}}
</constraints>
<output_format>
## Verdict
One line: approve | approve-with-nits | request-changes, and the main reason.
## Findings
Numbered. Each: severity, `path:line`, the route and method, the defect, the triggering request (method, path, body or query), the fix in one or two sentences.
## Missing tests
Bullets naming the case each test covers, or "None".
## Questions
Versions, settings or business rules you need confirmed, or "None".
</output_format>可替换内容
- {{diff}}
- {{#context}}
- {{context}}
- {{/context}}
- {{> guardrails/investigate-before-answering}}
- [$ne]
分类与标签
编程开发、路由审查、异步异常、中间件顺序、输入校验、缺陷复现