๐Security (XSS, CSRF & more)
15.1XSS (Cross-Site Scripting)
What it is: an attacker injects script that runs in another user's browser, in your site's origin โ stealing cookies/tokens, impersonating the user, defacing the page.
How React helps by default: JSX auto-escapes interpolated values. {userInput} is rendered as text, not HTML โ so <script> in user input shows up as literal characters, not executable script.
Where React can't help you:
// SAFE โ escaped automatically
<div>{userInput}</div>
// DANGEROUS โ bypasses escaping, injects raw HTML
<div dangerouslySetInnerHTML={{ __html: userInput }} />
Only use dangerouslySetInnerHTML with content you control or have sanitized. Sanitize with DOMPurify if you must render user/third-party HTML:
import DOMPurify from 'dompurify';
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(html) }} />
Other XSS vectors to guard:
javascript:URLs inhref/srcโ validate/whitelist URL schemes.- Injecting into
<script>,<style>, or event handlers via templating. - Defense in depth: Content Security Policy (CSP). A CSP header restricts which script sources can execute, blocking injected inline scripts even if one slips through. This is a strong second layer beyond escaping.
15.2CSRF (Cross-Site Request Forgery)
What it is: a malicious site tricks a logged-in user's browser into making an authenticated request to your site (the browser auto-attaches cookies), performing an action the user didn't intend.
Defenses:
- SameSite cookies โ set
SameSite=Lax(orStrict) so cookies aren't sent on cross-site requests. This is the modern first-line defense and is now a browser default (Lax). - CSRF tokens โ server issues a per-session/per-request token that must accompany state-changing requests; a cross-site attacker can't read it (same-origin policy).
- Validate
Origin/Refererheaders on state-changing requests. - Prefer non-cookie auth for APIs โ tokens in the
Authorizationheader (not auto-sent by the browser) sidestep classic CSRF, but then you must protect the token from XSS (don't store sensitive tokens inlocalStorageif you can avoid it; httpOnly cookies protect against XSS token theft but reintroduce CSRF concerns โ hence SameSite + tokens together).
The XSS/CSRF relationship: XSS can defeat most CSRF defenses (a script running in your origin can read tokens). So fixing XSS is foundational โ CSRF protections assume the attacker can't run script in your page.
15.3Other frontend security hygiene
- Never trust the client. All validation/authorization must be re-checked server-side; client checks are UX only.
- Dependency security โ audit packages (
npm audit), keep them updated; supply-chain attacks are real. - Subresource Integrity (SRI) for third-party scripts loaded via CDN โ the browser verifies the file hash matches, rejecting tampered scripts.
- HTTPS everywhere; secure/httpOnly cookie flags; don't leak secrets in the bundle (Part 5.3).
- Clickjacking โ
X-Frame-Options/ CSPframe-ancestorsto prevent your site being iframed by attackers.
๐ง Quiz: Security (XSS, CSRF & more)
2 questions1The core defense against XSS is:
2CSRF is best mitigated with: