
XSS · CSRF · token theft · CSP · HTTPS & CORS



SPAs (React, Angular, Vue) push logic to the client.
Tokens and config often live in the browser.

Injecting malicious scripts into pages other people open.

Impact: cookie theft and session hijacking
01Sanitize user input before rendering
02Use frameworks with built-in escaping
03React escapes {data} by default
04CSP restricts where scripts load from
05Avoid dangerouslySetInnerHTML
// Sanitize untrusted HTML firstimport DOMPurify from 'dompurify';const clean = DOMPurify.sanitize(userHtml);
// Let the framework escape by default<p>{comment}</p> // text, never HTML
// Defense in depth: same-origin scripts only// Content-Security-Policy: script-src 'self'
// The escape hatch: sanitized input only<div dangerouslySetInnerHTML={{ __html: clean }} />A forged request that rides on the user's logged-in session.


Set-Cookie: session=7f3a9c; HttpOnly; Secure; SameSite=Lax; Path=/Rotate tokens and keep lifetimes short, wherever they are.


$ npm audit# npm audit report nth-check <2.0.1Severity: highInefficient regular expression complexityfix available via `npm audit fix` 3 vulnerabilities (1 moderate, 2 high)$ npm audit fixfixed 3 of 3 vulnerabilities

An allow-list of what your page may load and run, enforced by the browser.

Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://api.example.com; object-src 'none'; base-uri 'self';
Access-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Credentials: true# Never "Access-Control-Allow-Origin: *" on authenticated endpoints
Escape · SameSite · CSP · npm audit

<iframe src="https://slides.ezzdinatef.com/embed/frontend-security-essentials" width="960" height="600" style="border:0;border-radius:12px" allow="fullscreen" title="Frontend Security Essentials"></iframe>