返回提示词目录第 1292 页

审计并修复服务端操作安全漏洞

检查服务端操作与路由的鉴权、输入校验及数据泄露问题,提交最小修复与测试结果。

完整提示词

<context>
In the Next.js App Router every exported server action (`"use server"`) is a public POST endpoint: its action id ships in the client bundle of any page that uses it, and anyone can call it with arguments of their choosing, whatever the UI shows. Hiding a button or checking the role in a layout protects nothing: layouts do not re-run on every action call or client navigation, and middleware is easy to bypass through matcher gaps and must never be the only check (a 2025 advisory showed the `x-middleware-subrequest` header skipping middleware entirely in unpatched versions). Arguments to actions arrive as untrusted, serialised input; TypeScript types are not validation. The second classic leak is data, not code: a server component that passes a whole database row to a client component ships every field to the browser, and a module imported from a client component is bundled for the browser along with any `process.env` value it reads that has the `NEXT_PUBLIC_` prefix. Closures in inline server actions are encrypted, but the values they capture still should not be secrets the action trusts blindly.
</context>

<task>
Audit and fix {{scope}}.
{{#auth}}Auth model: {{auth}}{{/auth}}

1. Read `package.json` for the Next.js version and flag it if it is below a release that fixed a known middleware or action vulnerability; do not upgrade without saying so.
2. List every server action (files and inline functions with `"use server"`), every `route.ts` handler and every `middleware.ts` matcher. For each, note who should be allowed to call it.
3. For each action and handler, check inside the function itself: the session is read and verified, the user is authorised for this specific object (ownership or tenant check on the id passed in, not just "logged in"), and input is parsed with a schema (Zod or the project's existing validator) that rejects unknown fields.
4. Find data leaks to the client: props passed from server to `"use client"` components that carry whole records, tokens or internal fields; server-only modules (database clients, secret readers) importable from client code; `NEXT_PUBLIC_` variables that hold secrets.
5. Check middleware: matcher coverage, whether any route relies on middleware alone, and redirects built from user input (open redirect).
6. Fix with the smallest change that follows existing patterns: a shared `requireUser`/`requireRole` helper called at the top of each action, a DTO or `select` that returns only needed fields, `import "server-only"` in server modules, and schema validation. Return generic errors, not stack traces.
7. Add tests that call each fixed action or handler directly as an unauthenticated user and as a user from another tenant, and assert denial. Run the tests, type check and lint.
</task>

<constraints>
- Treat every exported action as reachable by anyone; never accept "the UI does not show it" as a control.
- Do not move auth into middleware only, and do not remove existing checks.
- Do not change the auth provider, session strategy or database schema; propose those separately.
- Never print secret values you find; refer to them by variable name and file.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Attack surface
Table: file, action or route, intended caller, auth check present (yes/no), object-level check (yes/no), validation (yes/no).
## Findings
Ranked list: severity, file:line, what an attacker can do, fix.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test and what it proves.
## Verification
Commands run and their actual results.
## Residual risk
What remains, such as framework version, rate limiting or CSRF assumptions, and what would close it.
</output_format>

可替换内容

  • {{scope}}
  • {{#auth}}
  • {{auth}}
  • {{/auth}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、接口安全审计、服务端鉴权、多租户隔离、数据泄露检查

前往 AI 图片创作