Why SPA security is different
Single Page Applications shift a lot of logic to the browser — which means the attack surface shifts too. Unlike traditional server-rendered apps, your SPA is running your code in an environment you don’t control. This changes how you need to think about XSS, CSRF, authentication, and data exposure.
Cross-Site Scripting (XSS)
XSS is the most common SPA vulnerability. It happens when an attacker injects malicious scripts into your app’s output. Modern frameworks like React escape output by default — but there are still ways to get burned.
- Never use
dangerouslySetInnerHTMLwith untrusted content - Sanitize any user-generated HTML with a library like
DOMPurify - Set a strict Content Security Policy (CSP) header
- Avoid storing sensitive data in
localStorage— it’s accessible to any script on the page
CSRF Protection
Cross-Site Request Forgery tricks authenticated users into making requests they didn’t intend. If your API uses cookie-based auth, you need CSRF protection.
- Use SameSite=Strict or SameSite=Lax on session cookies
- Validate the
OriginandRefererheaders server-side - If using Laravel, the built-in CSRF middleware handles this automatically for state-changing requests
Authentication strategies
The session-vs-JWT debate matters here. For SPAs consuming your own API, HttpOnly cookies with short-lived sessions are generally safer than storing JWTs in localStorage. A JWT can also be stored in an HttpOnly cookie, the same way a session key is, as long as you add the CSRF protection above. See my related post on Session Cookie vs JWT.
If a JWT is stolen from localStorage, an attacker can use it indefinitely until it expires. An HttpOnly cookie can’t be read via JavaScript, so it can’t be stolen that way. It doesn’t stop XSS though, an injected script can still send requests from the page as the logged in user, it just limits the damage.
Security headers
A hardened set of HTTP response headers goes a long way. Configure these at your CDN or server level:
Content-Security-Policy: default-src 'self'; script-src 'self'; connect-src 'self' https://api.example.com; frame-ancestors 'none'
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
If your API is on a different domain or subdomain, add it to connect-src, otherwise default-src 'self' will block the requests. script-src 'self' also blocks inline scripts and most analytics snippets, so start with Content-Security-Policy-Report-Only to see what breaks before enforcing it. frame-ancestors 'none' replaces X-Frame-Options in modern browsers, keep both for older ones.
Dependency hygiene
Your app is only as secure as its weakest dependency. Run npm audit regularly, pin versions where possible, commit your lock file and use npm ci in CI and production builds so you install exactly what’s in it, and audit third-party scripts before adding them. Supply chain attacks are increasingly common.
Security isn’t a feature you add at the end — it’s a mindset you apply throughout. Start with the basics above and you’ll be ahead of the vast majority of SPAs in production today.