javascript-typescript-react-web-frontend-security

This document is designed as a security spec that supports:

React (JavaScript/TypeScript) Web Security Spec (React 19.x, TypeScript 5.x)

This document is designed as a security spec that supports:

  1. Secure-by-default code generation for new React code.
  2. Security review / vulnerability hunting in existing React code (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).


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


1) Operating modes

1.1 Generation mode (default)

When asked to write new React code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in a React 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. App entrypoints, build tooling (Vite/Webpack/CRA/Next), deployment configs, CDN/static hosting config.
  2. Secrets & configuration exposure (env vars, runtime config injection, source maps).
  3. Rendering of untrusted data (XSS/DOM XSS), especially dangerouslySetInnerHTML, markdown/HTML renderers, URL attributes.
  4. Direct DOM usage and dangerous JS execution (innerHTML, eval, new Function, document.write, etc.).
  5. Auth & session patterns (token storage, cookies, CSRF interactions, OAuth flows).
  6. Network layer (axios/fetch wrappers, dynamic base URLs, credentialed requests, data exfil risks).
  7. Navigation & redirect handling (open redirects, window.location, target=_blank, window.open).
  8. Third-party scripts/tags/analytics and integrity controls (CSP, SRI).
  9. Service worker/PWA behavior (HTTPS, caching rules, update strategy).
  10. Security headers posture (CSP, clickjacking, nosniff, referrer policy) in app or at the edge. ([OWASP Cheat Sheet Series][2])

2) Definitions and review guidance

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

Examples include:

2.2 State-changing request (frontend perspective)

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

Frontend-specific note:

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 React frontend misconfigurations.

3.1 Production build and configuration hygiene (MUST)

3.2 Browser-enforced protections (SHOULD, but baseline expectation for modern apps)

3.3 High-risk features baseline (MUST if used)


4) Rules (generation + audit)

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

REACT-CONFIG-001: Never embed secrets in the client bundle (env vars are public)

Severity: Critical (if secrets exposed)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-XSS-001: Do not use dangerouslySetInnerHTML with untrusted content (sanitize or avoid)

Severity: High (Only if you can prove attacker-controlled HTML reaches it)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-XSS-002: Rely on React’s escaping-by-default behavior; do not bypass it

Severity: High (when bypassed)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-DOM-001: Avoid DOM XSS injection sinks in React code (use safe alternatives)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-URL-001: Validate and constrain untrusted URLs used in href, src, navigation, and redirects

Severity: High Only when you can prove they are attacker controlled

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-MARKUP-001: Markdown / rich text rendering must be configured safely

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-TT-001: Use Trusted Types (with CSP) to harden DOM XSS sinks where feasible

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-CSP-001: Deploy and maintain a CSP as defense-in-depth (especially when rendering untrusted content)

Severity: Medium to High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-SRI-001: Use Subresource Integrity (SRI) for third-party scripts and styles (or self-host)

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-3P-001: Third-party JavaScript and tag managers must be minimized and governed

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-AUTH-001: Token and session handling must be resilient to XSS (avoid sensitive storage in Web Storage)

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-CSRF-001: Cookie-authenticated, state-changing requests MUST be CSRF-protected

Severity: High

NOTE: If the application does not use cookie based auth (using Authentication header for example), then CSRF is not a concern.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-AUTHZ-001: Do not rely on frontend-only authorization

Severity: High (only if used as primary protection)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-NET-001: Prevent data exfiltration and credential leakage via dynamic outbound requests

Severity: Medium to High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-REDIRECT-001: Prevent open redirects and untrusted navigation

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-SW-001: Service workers are high-privilege; require HTTPS and safe caching/update rules

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-HEADERS-001: Ensure essential security headers are set for the React app shell (app or edge)

Severity: Medium

Required (typical SPA served from an origin):

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-POSTMSG-001: postMessage must validate origin and treat payload as untrusted data

Severity: Medium to High (depends on what messages can do)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-FILE-001: File uploads and previews must not create client-side active content vulnerabilities

Severity: Medium (can be High if stored-XSS possible)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


REACT-SUPPLY-001: Dependency and supply-chain hygiene (frontend + build tooling)

Severity: Low

Required:

Audit focus:

Insecure patterns:

Detection hints:

Fix:

Notes:


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

When actively scanning, use these high-signal patterns:

Always try to confirm:


6) Sources (accessed 2026-01-26)

Primary React documentation:

OWASP Cheat Sheet Series:

Browser / platform references (MDN, W3C):

Build tooling / env exposure references:

Auth/token storage guidance:

Dependency tooling references:

Sanitizer reference:

[1]: https://create-react-app.dev/docs/adding-custom-environment-variables/ "Adding Custom Environment Variables | Create React App" [2]: https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html "Content Security Policy - OWASP Cheat Sheet Series" [3]: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API "Trusted Types API - Web APIs | MDN" [4]: https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html "HTML5 Security - OWASP Cheat Sheet Series" [5]: https://cheatsheetseries.owasp.org/cheatsheets/Third_Party_Javascript_Management_Cheat_Sheet.html "Third Party Javascript Management - OWASP Cheat Sheet Series" [6]: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html "Cross-Site Request Forgery Prevention - OWASP Cheat Sheet Series" [7]: https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity "Subresource Integrity - Security | MDN" [8]: https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/Clickjacking "Clickjacking - Security | MDN" [9]: https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html "Cross Site Scripting Prevention - OWASP Cheat Sheet Series" [10]: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers "Using Service Workers - Web APIs | MDN" [11]: https://vite.dev/guide/env-and-mode "Env Variables and Modes | Vite" [12]: https://react.dev/reference/react-dom/components/common "Common components (e.g. <div>) – React" [13]: https://github.com/cure53/DOMPurify "GitHub - cure53/DOMPurify: DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:" [14]: https://legacy.reactjs.org/docs/introducing-jsx.html "Introducing JSX – React" [15]: https://www.w3.org/TR/trusted-types/ "Trusted Types" [16]: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps "

draft-ietf-oauth-browser-based-apps-26

" [17]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel "HTML attribute: rel - HTML | MDN" [18]: https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Secure_Contexts "Secure contexts - Security | MDN" [19]: https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html "File Upload - OWASP Cheat Sheet Series" [20]: https://docs.npmjs.com/cli/v10/commands/npm-audit "npm-audit | npm Docs" [21]: https://docs.npmjs.com/cli/v10/commands/npm-ci "npm-ci | npm Docs" [22]: https://cheatsheetseries.owasp.org/cheatsheets/NPM_Security_Cheat_Sheet.html "NPM Security - OWASP Cheat Sheet Series" [23]: https://react.dev/blog/2024/12/05/react-19 "React v19 – React"