Web security controls
What you need to know
Web security controls address different threats. Use secure session cookies, origin checks, constrained URLs, input limits and response headers together; none replaces authorization.
By the end, you can: choose defensive cookie, URL and header policies; test negative access.
Map a destination to a known path
function destination(value: unknown): string { if (value === 'catalog') return '/en/catalog'; if (value === 'account') return '/en/account'; return '/en';}console.log(destination('other'));Expected behavior: Unknown values go to /en. Authentication of the account route remains mandatory.
How it works
Cross-site request forgery (CSRF) involves unwanted actions using a browser’s credentials. Cookie SameSite settings and origin checks reduce risk; use the framework and authentication provider's CSRF protections and independently verify permissions. Next.js Server Actions compare request Origin and host information, with explicit allowed-origin configuration for trusted proxy deployments; do not broaden it as a generic fix. Cross-origin resource sharing (CORS) governs browser access to responses and is not an authentication mechanism. For redirects, map a small known destination identifier to a fixed local path instead of trusting arbitrary URLs. For outbound requests and optimized remote images use reviewed destinations and enforce egress limits; a URL string check alone is not protection against redirects, DNS changes or private-network access. React text escaping helps avoid HTML injection, but unsafe raw HTML and unsafe URL schemes require separate controls. A Content Security Policy (CSP) limits resource execution and framing; per-request nonces require dynamic rendering. The documented nonce approach cannot inject a fresh request nonce into a prerendered static shell, including PPR. Generate an unpredictable nonce for each rendered request, keep the request-side CSP used by rendering consistent with the browser response policy, and do not share nonce-bearing HTML across requests. Test actual headers and browser behavior in production mode. Use HttpOnly, Secure and appropriate SameSite cookies for HTTPS sessions, and remember that HttpOnly does not stop all actions by compromised scripts.
Headers for a constrained public route
export function GET(){ return Response.json({status:'ok'}, {headers:{ 'Cache-Control':'no-store', 'X-Content-Type-Options':'nosniff', 'Referrer-Policy':'strict-origin-when-cross-origin' }});}Expected behavior: An eligible Route Handler returns JSON with these headers. This is not a complete CSP or deployment header configuration.
Common mistake
Do not allow every origin to resolve a failing request. Do not copy a static nonce or weaken TLS. Do not treat robots, CORS or an unguessable path as access control.
Practice and verification
Write a control matrix for a private form, public image and outbound API. For each, identify the trusted origin, permission checks, maximum body size and expected deny result. Review deployment-specific CSP separately, including script/style/image sources and framing.
Scope: Proposed Next.js 16.x / React 19, Cache Components disabled unless noted. Framework and browser examples are staged; local standalone tests are reported separately. Exact dependency patches and platform contracts pending.