javascript-typescript-nextjs-web-server-security

This document is designed as a security spec that supports:

Next.js (TypeScript/JavaScript) Web Security Spec (Next.js 16.1.x, Node.js 20.9+)

This document is designed as a security spec that supports:

  1. Secure-by-default code generation for new Next.js backend code (Route Handlers, API Routes, Server Actions, Proxy/Middleware).
  2. Security review / vulnerability hunting in existing Next.js repos (passive “notice issues while working” and active “scan the repo and report findings”).

It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).

Target scope: Next.js 16.1.x (latest line shown in the App Router docs) ([Next.js][1]), running on Node.js 20.9+ (per Next.js system requirements). ([Next.js][2])


0) Safety, boundaries, and anti-abuse constraints (MUST FOLLOW)


1) Operating modes

1.1 Generation mode (default)

When asked to write new Next.js code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in a Next.js repo (even if the user did not ask for a security scan):

1.3 Active audit mode (explicit scan request)

When the user asks to “scan”, “audit”, or “hunt for vulns”:

Recommended audit order:

  1. Deployment entrypoints and environment (Dockerfiles, package.json scripts, hosting config).
  2. Next.js config (next.config.*), Proxy/Middleware, routing patterns.
  3. Authentication, sessions, cookies.
  4. CSRF protections and state-changing endpoints (Server Actions, Route Handlers, API Routes).
  5. XSS (React + CSP) and unsafe HTML rendering.
  6. Cache/data-leak hazards (static rendering + caching + “use cache”).
  7. File handling (uploads/downloads) and path traversal.
  8. Injection classes (SQL/ORM misuse, command execution, unsafe deserialization).
  9. Outbound requests (SSRF).
  10. Redirect handling (open redirects).
  11. CORS and security headers.

2) Definitions and review guidance

2.1 Untrusted input (treat as attacker-controlled unless proven otherwise)

In Next.js backends, untrusted input includes:

App Router:

Pages Router:

Plus:

2.2 State-changing request

A request is state-changing if it can create/update/delete data, change auth/session state, trigger side effects (purchase, email send, webhook send), or initiate privileged actions.

Special note for Next.js:

2.3 Required audit finding format

For each issue found, output:


3) Secure baseline: minimum production configuration (MUST in production)

This is the smallest “production baseline” that prevents common Next.js backend misconfigurations.

3.1 Run Next.js in production mode (MUST)

3.2 Put a reverse proxy / edge layer in front when self-hosting (MUST for public internet)

3.3 Baseline header/cookie posture (SHOULD)

IMPORTANT NOTE: Only set Secure in production environment. When running in a local dev environment over HTTP, do not set Secure property on cookies. You should do this conditionally based on if the app is running in production mode. You should also include a property like SESSION_COOKIE_SECURE which can be used to disable Secure cookies when testing over HTTP.

3.4 Clear separation between server-only and client code (MUST)


4) Rules (generation + audit)

Each rule contains: required practice, insecure patterns, detection hints, and remediation.

NEXT-DEPLOY-001: Do not run next dev in production; ensure production mode behavior

Severity: High (if production)

NOTE: If they are deploying to a specific Next.js hosting provider, they do not need to worry about this.

Required:

Insecure patterns:

Detection hints:

Fix:

Note:


NEXT-SUPPLY-001: Stay on supported Next.js releases; patch quickly for security advisories

Severity: High (Critical if known-vulnerable version)

Required:

Insecure patterns:

Detection hints:

IMPORTANT: Any versions older than these minor versions are vulnerable to "react2shell" vulnerability (https://nextjs.org/blog/CVE-2025-66478): 15.0.5 15.1.9 15.2.6 15.3.6 15.4.8 15.5.7 16.0.7

Fix:


NEXT-SECRETS-001: Secrets MUST NOT be committed or exposed to the browser

Severity: High (Critical if secret is client-exposed)

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-SECRETS-002: Avoid server-only → client bundling mistakes (server/client boundary is a security boundary)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-AUTH-001: Authentication/authorization MUST be enforced server-side for every protected action

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-AUTH-002: Proxy/Middleware-based auth MUST NOT create route coverage gaps

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


NEXT-CSRF-001: Cookie-authenticated state-changing endpoints MUST be CSRF-protected

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


NEXT-SESS-001: Session cookies MUST use secure attributes in production

Severity: Medium

Required (production, HTTPS):

Insecure patterns:

Detection hints:

Fix:


NEXT-SESS-002: Sessions MUST be bounded and resistant to fixation/replay

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-INPUT-001: Runtime input validation is mandatory (TypeScript is not validation)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-HEADERS-001: Essential security headers MUST be set (in app or at the edge)

Severity: Low

Required (typical web app):

Insecure patterns:

Detection hints:

Fix:


NEXT-CSP-001: Use a CSP to reduce XSS impact; prefer nonces for scripts

Severity: Medium

NOTE: It is most important to set the CSP's script-src. All other directives are not as important and can generally be excluded for the ease of development.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


NEXT-XSS-001: Prevent reflected/stored XSS in React/Next rendering

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-ACTION-001: Server Actions MUST be treated like public endpoints

Severity: High (Critical for privileged actions)

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-ACTION-002: Do not accidentally leak secrets through Server Action closure/binding patterns

Severity: Medium (High if important secrets are exposed)

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-CACHE-001: Prevent data leaks via static rendering and shared caching

Severity: High (Critical if cross-user data leak)

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-FILES-001: User uploads MUST be validated, stored safely, and served safely

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-PATH-001: Prevent path traversal and unsafe file access

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-SSRF-001: Outbound requests using user-influenced URLs MUST be restricted

Severity: Medium (High in internal networks)

NOTE: This is mostly only applicable to apps which will be deployed in a cloud/LAN setup or have other http services on the same box. Sometimes the feature requires this functionality unavoidably (webhooks).

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-REDIRECT-001: Prevent open redirects (including auth flows)

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-CORS-001: CORS must be explicit and least-privilege

Severity: Medium (High if misconfigured with credentials)

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-WEBHOOK-001: Webhook endpoints MUST verify authenticity using the raw body

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-INJECT-001: Prevent SQL injection (use parameterized queries / ORM)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-INJECT-002: Prevent OS command injection and unsafe subprocess use

Severity: Critical to High

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-INJECT-003: Avoid dynamic code execution and unsafe deserialization

Severity: High to Critical

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-LOG-001: Logging MUST NOT leak secrets or sensitive headers

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-ERROR-001: Error handling MUST avoid leaking implementation details in production

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-PROXY-001: Proxy/Middleware must not introduce header smuggling or unsafe header forwarding

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-HOST-001: Host/Origin-derived URL construction MUST be allowlisted

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


NEXT-DOS-001: Rate limiting and resource controls MUST exist for abuse-prone endpoints

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


5) Practical scanning heuristics (how to “hunt”)

When actively scanning, use these high-signal patterns:

Always try to confirm:


6) Sources (accessed 2026-01-27)

Primary framework documentation (Next.js):

Next.js security guidance & advisories:

General web security references (recommended baseline):

[1]: https://nextjs.org/docs/app/getting-started/route-handlers "Getting Started: Route Handlers | Next.js" [2]: https://nextjs.org/docs/app/getting-started/deploying?utm_source=chatgpt.com "Getting Started: Deploying" [3]: https://nextjs.org/docs/pages/building-your-application/routing/api-routes "Routing: API Routes | Next.js" [4]: https://nextjs.org/docs/app/api-reference/functions/headers "Functions: headers | Next.js" [5]: https://nextjs.org/docs/app/api-reference/config/next-config-js/serverActions "next.config.js: serverActions | Next.js" [6]: https://nextjs.org/blog/security-nextjs-server-components-actions "How to Think About Security in Next.js | Next.js" [7]: https://nextjs.org/docs/pages/guides/environment-variables "Guides: Environment Variables | Next.js" [8]: https://nextjs.org/docs/pages/guides/self-hosting?utm_source=chatgpt.com "Guides: Self-Hosting" [9]: https://nextjs.org/docs/app/api-reference/functions/cookies "Functions: cookies | Next.js" [10]: https://nextjs.org/blog/next-16?utm_source=chatgpt.com "Next.js 16" [11]: https://github.com/vercel/next.js/security/advisories/GHSA-9g9p-9gw9-jx7f?utm_source=chatgpt.com "Denial of Service in Image Optimizer · Advisory" [12]: https://nextjs.org/docs/pages/guides/authentication "Guides: Authentication | Next.js"