diff --git a/cheatsheets_draft/React_Security_Cheat_Sheet.md b/cheatsheets_draft/React_Security_Cheat_Sheet.md new file mode 100644 index 0000000000..0eb6aed6b7 --- /dev/null +++ b/cheatsheets_draft/React_Security_Cheat_Sheet.md @@ -0,0 +1,735 @@ +# React Security Cheat Sheet + +## Introduction + +React is a widely used JavaScript library for building user interfaces. While React provides some built-in protections against common vulnerabilities, it does not enforce secure coding practices by default, and several React patterns introduce security risks that are easy to overlook. As React architectures increasingly move toward the server through patterns such as Server-Side Rendering (SSR) and React Server Components (RSC), the attack surface has expanded beyond traditional client-side vulnerabilities. This cheat sheet provides practical guidance +for developers building React applications to avoid the most common security pitfalls across both client and server rendering contexts. + +Key areas covered: + +- Preventing Cross-Site Scripting (XSS) through safe rendering patterns +- Protecting sensitive data in client state and server-side rendering +- Securing authentication tokens and managing authorization in the UI +- Hardening server-side rendered (SSR) React applications +- Managing third-party dependency and supply chain risk +- Defending against AI-introduced vulnerabilities in developer tooling and the browser runtime + +## Cross-Site Scripting (XSS) Prevention + +Cross-Site Scripting (XSS) occurs when an attacker injects malicious scripts into a web page that are then executed in another user's browser. React escapes dynamic content rendered through JSX by default, converting special characters such as `<`, `>`, and `"` into their HTML-encoded equivalents so the browser treats them as text rather than executable code. This protection applies only to content rendered through JSX. Several React patterns bypass this protection entirely and must be handled with care. + +Related CWE(Common Weakness Enumeration): [CWE-79: Improper Neutralization of Input During Web Page Generation (XSS)](https://cwe.mitre.org/data/definitions/79.html) + +### Avoid Unsafe HTML Injection with dangerouslySetInnerHTML + +React provides `dangerouslySetInnerHTML` as an escape hatch for rendering raw HTML — for example, content produced by a rich text editor or a Content Management System (CMS). When used without sanitization, it bypasses React's built-in escaping and allows injected scripts or malicious event handlers to execute in the browser. + +Always sanitize HTML content before passing it to `dangerouslySetInnerHTML`. Use a dedicated HTML sanitization library such as [DOMPurify](https://github.com/cure53/DOMPurify), which parses the HTML in an isolated sandbox, removes dangerous tags and attributes, and returns a safe HTML string. + +```jsx +// Illustrative example — not production-ready +import DOMPurify from "dompurify"; + +const clean = DOMPurify.sanitize(rawHTML); +return
; +``` + +Sanitize content on the server before storing it, and again on the client before rendering. Client-side sanitization alone does not prevent malicious content from being stored in your database and served to other consumers such as mobile clients or third-party integrations. + +HTML injection via unsanitized content can also enable DOM Clobbering — where injected elements with specific id or name attributes overwrite global JavaScript variables your application depends on. For guidance see [OWASP DOM Clobbering Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/DOM_Clobbering_Prevention_Cheat_Sheet.html) + +Related CWE: [CWE-79: Improper Neutralization of Input During Web Page Generation (XSS)](https://cwe.mitre.org/data/definitions/79.html) + +### Validate URLs Before Rendering + +React does not sanitize the `href`, `src`, or `action` attributes. This creates two distinct risks. First, an attacker can supply a `javascript:` URL which the browser executes as code when the user interacts with the element — a form of DOM-based XSS delivered through an attribute. Second, an attacker can supply a valid `https://` URL pointing to a malicious external site, redirecting users to phishing pages that impersonate your application. + +Validate both the URL scheme and the destination hostname before rendering any user-supplied URL into an attribute. Scheme validation alone prevents code execution via `javascript:`, `data:`, or `vbscript:` URLs but does +not prevent open redirect attacks where the attacker supplies a valid `https://` URL pointing to their own site. + +```jsx +// ❌ Unsafe — user-supplied URL rendered without validation +Click here + +// ❌ Insufficient — scheme check alone does not prevent open redirect +const isSafe = url.startsWith('https://'); +Click here + +// ✅ Safe — validate both scheme and hostname +const url = new URL(userSuppliedUrl); +const isSafe = +(url.protocol === 'https:' || url.protocol === 'http:') && +url.hostname === 'yourdomain.com'; +Click here +``` + +Where the destination cannot be controlled — such as in user-generated content or comment sections — intercept external navigation with a redirect warning page to inform users they are leaving your application rather than silently following the link. + +High-impact URL sinks include `