nextjs-security-patterns-v1Certifiedv1.0.0

Next.js security patterns

Always-on secure coding patterns for Next.js (safe; no dependency edits).

Protects against

CVE-2020-28500CVE-2021-23337CVE-2021-3749CVE-2022-0235CVE-2022-23539CVE-2022-23540CVE-2022-23541CVE-2023-45857CVE-2024-34350CVE-2024-34351CVE-2024-39693CVE-2024-46982CVE-2024-47831CVE-2024-51479CVE-2024-56332CVE-2025-13465CVE-2025-27152CVE-2025-29927CVE-2025-49826CVE-2025-55173CVE-2025-55182CVE-2025-55183CVE-2025-55184CVE-2025-57752CVE-2025-57822CVE-2025-59471CVE-2025-59472CVE-2025-62718CVE-2025-67779CVE-2026-23864CVE-2026-23869CVE-2026-23870CVE-2026-25639CVE-2026-27978CVE-2026-27979CVE-2026-27980CVE-2026-29057CVE-2026-2950CVE-2026-40175CVE-2026-42033CVE-2026-42034CVE-2026-42035CVE-2026-42036CVE-2026-42038CVE-2026-42039CVE-2026-42041CVE-2026-42042CVE-2026-42043CVE-2026-44573CVE-2026-44574CVE-2026-44575CVE-2026-44576CVE-2026-44577CVE-2026-44578CVE-2026-44579CVE-2026-44580CVE-2026-44581CVE-2026-45109CVE-2026-4800GHSA-5j59-xgg2-r9c4GHSA-9qr9-h5gf-34mpGHSA-h25m-26qc-wcjfGHSA-mwv6-3258-q52cGHSA-npm-1111391GHSA-npm-1112182GHSA-npm-1112653GHSA-w37m-7fhw-fmv9

Rule body

# Next.js security patterns

Safe, always-on code patterns that reduce security risk without changing your dependency graph.

These rules use ONLY pattern-level verbs (ALWAYS/NEVER). They MUST NOT prescribe dependency upgrades.

### CVE-2025-29927 — safe patterns

- NEVER rely solely on middleware for authorization of sensitive operations.
- ALWAYS validate authorization at the route handler or API endpoint level.
- NEVER assume middleware execution order protects against path-based bypasses.
- ALWAYS implement defense-in-depth with authorization checks at multiple layers.

### CVE-2022-0235 — safe patterns

- NEVER allow automatic header forwarding on cross-origin redirects without explicit allowlisting.
- ALWAYS validate redirect target URLs against a list of trusted domains before following.
- NEVER follow redirects to different schemes (http→https or vice versa) without user confirmation.

### CVE-2024-34351 — safe patterns

- NEVER pass user-controlled input or Host header values directly to URL construction in Server Actions.
- ALWAYS validate that redirect destinations and outbound requests target only explicitly approved hostnames.
- NEVER rely on request headers alone to determine request origin or target for authorization decisions.

### CVE-2022-23539 — safe patterns

- ALWAYS verify that the key type matches the intended signing algorithm (e.g., EC keys with ES256, RSA keys with RS256).
- NEVER accept arbitrary key types during JWT verification without explicit validation of key-algorithm compatibility.
- ALWAYS reject or warn on DSA, HMAC, or other legacy key types when using asymmetric algorithms like RS256 or ES256.

### CVE-2024-46982 — safe patterns

- NEVER rely on pages router for routes requiring dynamic content or authentication without explicit cache headers.
- ALWAYS use the app router for routes that should never be cached or contain sensitive user data.
- NEVER assume default cache behavior prevents public caching; explicitly set Cache-Control headers on sensitive routes.
- ALWAYS validate and sanitize request headers that influence cache behavior in pages router handlers.

### CVE-2021-3749 — safe patterns

- NEVER pass untrusted or user-controlled HTTP response data to custom header parsers without size limits.
- ALWAYS validate the count and length of header values before processing them through regex patterns.
- NEVER process headers from untrusted sources using complex regular expressions without timeouts or limits.

### CVE-2021-23337 — safe patterns

- NEVER pass user-controlled input directly to template functions.
- ALWAYS compile templates from static, trusted code only.
- NEVER use template delimiters that process JavaScript expressions from external sources.

### CVE-2024-51479 — safe patterns

- NEVER rely solely on pathname matching for authorization decisions in middleware.
- ALWAYS validate authorization against user identity and permissions, not just route patterns.
- ALWAYS implement authorization checks at the handler level as a secondary layer.

### CVE-2025-27152 — safe patterns

- NEVER construct axios request URLs from user input or untrusted data sources.
- ALWAYS validate that request URLs are relative paths before passing to axios when baseURL is configured.
- ALWAYS use URL parsing to reject or sanitize any input containing protocol schemes or hostnames.
- NEVER assume baseURL will prevent requests to arbitrary domains when absolute URLs are provided.

### CVE-2026-25639 — safe patterns

- NEVER spread untrusted objects into configuration merge operations without validation.
- ALWAYS use Object.assign with explicit property whitelisting for config merges.
- NEVER assume object spread operators safely handle reserved properties like __proto__.

### CVE-2026-4800 — safe patterns

- NEVER construct _.template options.imports keys from untrusted input.
- ALWAYS validate and allowlist key names before adding to template imports.
- NEVER use user-controlled data as import names without sanitization.

### CVE-2026-42043 — safe patterns

- NEVER rely solely on hostname string matching for NO_PROXY validation without subnet normalization.
- ALWAYS normalize and canonicalize hostnames to a single form before comparing against NO_PROXY rules.
- NEVER allow requests to loopback ranges (127.0.0.0/8, ::1) to bypass proxy filtering.
- ALWAYS validate that proxy bypass rules account for all canonical IP address representations.

### CVE-2026-42033 — safe patterns

- NEVER assume co-dependencies are safe from prototype pollution; treat Object.prototype as potentially malicious.
- ALWAYS validate response data integrity independently when using axios with untrusted or legacy dependencies.
- NEVER rely on axios request interception alone for security; implement additional transport-layer validation.
- ALWAYS audit dependency trees for known prototype pollution vulnerabilities before deployment.

### CVE-2026-42035 — safe patterns

- NEVER rely on duck-type checking to identify FormData or stream objects without explicit type validation.
- ALWAYS validate that header sources (custom, merged, or programmatic) come from controlled application code only.
- NEVER use unsanitized user input or third-party data to populate Object.prototype or prototype chains.
- ALWAYS prefer explicit constructor checks or instanceof over property-existence checks for type detection.