Learn the concept
Security
Key vulnerabilities include XSS (mitigated by React's auto-escaping, but watch dangerouslySetInnerHTML and URL injection), CSRF (use tokens in API requests, SameSite cookies), and insecure storage (avoid localStorage for sensitive data, prefer httpOnly cookies).
XSS (Cross-Site Scripting):
React's Built-in Protection:
{} by default<div>{userInput}</div> is safeXSS Vectors to Watch:
dangerouslySetInnerHTML - bypasses escaping, sanitize with DOMPurifyjavascript: URLs in href/src - NOT escaped, validate URLsCSRF (Cross-Site Request Forgery):
SameSite=Strict or SameSite=Lax cookiesAuthentication & Storage:
Additional Security Measures:
npm audit for vulnerable dependenciesReact Server Components Security (Critical):
CVE-2025-55182 (React2Shell, CVSS 10.0):
Follow-up CVEs (December 2025):
Server Actions Security:
"use server" create public HTTP endpoints// SAFE: React auto-escapes by default
function Comment({ text }) {
return <p>{text}</p>; // <script> becomes <script>
}
// DANGEROUS: dangerouslySetInnerHTML bypasses escaping
function UnsafeHTML({ html }) {
return <div dangerouslySetInnerHTML={{ __html: html }} />;
}
// SAFE: Sanitize with DOMPurify before using
import DOMPurify from 'dompurify';
function SafeHTML({ html }) {
const sanitized = DOMPurify.sanitize(html, {
ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p'],
ALLOWED_ATTR: ['href', 'target']
});
return <div dangerouslySetInnerHTML={{ __html: sanitized }} />;
}
// DANGEROUS: javascript: URL injection
function UnsafeLink({ href, children }) {
// User could pass: javascript:alert('XSS')
return <a href={href}>{children}</a>;
}
// SAFE: Validate URL protocol
function SafeLink({ href, children }) {
const isValidUrl = (url) => {
try {
const parsed = new URL(url, window.location.origin);
return ['http:', 'https:', 'mailto:'].includes(parsed.protocol);
} catch {
return false;
}
};
if (!isValidUrl(href)) {
return <span>{children}</span>; // Render as text
}
return <a href={href}>{children}</a>;
}Sanitizing user-generated content and avoiding dangerouslySetInnerHTML in React applications
Validating all Server Action inputs and protecting against RSC Flight protocol vulnerabilities
Auditing React dependencies for known vulnerabilities and maintaining patched versions
Build an interactive checklist app covering React security best practices with code examples
Create a deliberately vulnerable React app with XSS, CSRF, and injection vectors for security training, then fix each vulnerability