Frontend Security (React / Next.js)
Rules for writing, reviewing, and fixing frontend code so it is secure by default. Prefer small, targeted changes that preserve existing behavior. Concrete fixes over theory.
Last reviewed: October 2026. Version-sensitive details (Next.js APIs, header values) change; see "Verify before asserting" below.
How to work
- Inspect first. Read
package.json(Next.js and React versions), the router in use (App Router or Pages), the auth model (cookie session or bearer token), and where headers are set today (proxy.ts/middleware.ts,next.config, reverse proxy, hosting platform). Do not add a protection that already exists or conflicts with one that does. - Trace untrusted data from source (user input, URL, API, CMS, uploads, third parties) to sink (DOM, URL, cookie, DB, redirect, file path). Security bugs live at the sinks.
- Pick the mode: implementing, fixing, or auditing. For audits, report each finding with severity, file and line, evidence, and a concrete fix. Do not pad the report with generic advice.
- Make the smallest secure change. Do not rewrite an auth or deployment architecture when a targeted fix works.
- Verify, then report. Say what you checked and what you could not. Never claim something is fixed because a config line exists; confirm the effective behavior (response headers, rendered HTML, test results).
Ask the user only what changes the answer: cookie session vs bearer token, Next.js version, where headers are configured, whether user-supplied HTML is a real requirement. Never ask for real secrets, production tokens, or session cookies.
Non-negotiable rules
- The browser is untrusted. Client-side validation, hidden buttons, disabled controls, route guards, and TypeScript types are UX, not security. Enforce every security decision on the server.
- Render untrusted data as text. Any HTML sink (
dangerouslySetInnerHTML,innerHTML, and similar) needs a maintained sanitizer with a restrictive allowlist, applied at the render boundary. Never use regex to sanitize. - URLs from data are untrusted. Allowlist protocols (
http:,https:, optionallymailto:) before putting them inhreforsrc. Validate redirect targets against relative paths or an explicit host allowlist. - Keep credentials out of JavaScript. Session and refresh tokens belong in
HttpOnly; Secure; SameSitecookies, notlocalStorage,sessionStorage, URLs, or logs. - Cookie-authenticated mutations need CSRF defense and authorization.
SameSitehelps but does not cover everything. Never change state on GET. - Treat every Server Action and Route Handler as a public HTTP endpoint. Authenticate, authorize against the specific resource (ownership, tenant), and validate input with a schema, every time.
- Use a nonce-based CSP with
'strict-dynamic', rolled out via Report-Only first. Do not add'unsafe-inline'or'unsafe-eval'to scripts just to silence errors. Nonces require dynamic rendering. - Secrets stay on the server. Anything in the client bundle or in
NEXT_PUBLIC_*is public. - Money flows are verified server-to-server. Never trust a redirect, query string, or client-reported amount. Compute amounts on the server, verify with the provider, make fulfillment idempotent.
- OTP and login endpoints are attack surface. Rate-limit, cap attempts, expire and hash codes, return generic responses (no account enumeration).
- Uploads are hostile. Validate content (not just extension or client MIME type), generate storage keys server-side, keep storage private, treat SVG and HTML as active content.
- Dependencies are code you run. Commit the lockfile, install from it in CI, audit, and review new packages (especially in auth and payment paths).
- Do not log secrets or personal data: passwords, OTPs, tokens, cookies,
Authorizationheaders, card data, government IDs.
Where to read next
Read only the reference that matches the task:
| Task touches | Read |
|---|---|
| HTML rendering, Markdown, rich text, links, redirects, iframes, postMessage, JSON-LD | references/xss-and-urls.md |
CSP, nonces, security headers, CORS, proxy.ts / middleware.ts | references/csp-headers-cors.md |
| Sessions, cookies, tokens, CSRF, Server Actions, Route Handlers, authorization | references/auth-cookies-csrf.md |
| Forms, schemas, request limits, file uploads, object storage | references/input-validation-uploads.md |
| Payment callbacks, webhooks, OTP / SMS login | references/payments-and-otp.md |
| Third-party scripts, SRI, npm dependencies, supply chain | references/third-party-and-supply-chain.md |
| Finishing a change or running an audit | references/review-checklist.md |
For a full audit, read the checklist first, then the references for each area found in the code.
Verify before asserting
- Next.js naming: current Next.js docs use
proxy.tsexportingproxyfor the nonce pattern; older versions usemiddleware.tsexportingmiddleware. Check the installed version and the official docs before choosing. - Header values and directives: if unsure about a directive or value, check MDN or the framework docs instead of guessing.
- Provider APIs (payments, SMS, storage): use the provider's current official documentation. Do not invent response codes, field names, or amount units.
- Placeholders: never ship placeholder SRI hashes, fake keys, or invented configuration as if they were real.
- If something cannot be verified in this session, say so explicitly in the final message.


