Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 1 min read

JWT in localStorage: why AI-generated login code is one XSS bug from account takeover (proofforge.live)

Where you store a login token decides how bad an XSS bug gets. AI-generated auth code very often does this: after login, save the JWT in localStorage and attach it to every request. It works, and it hands the token to an

Where you store a login token decides how bad an XSS bug gets. AI-generated auth code very often does this: after login, save the JWT in localStorage and attach it to every request. It works, and it hands the token to any script that runs on your page.

Why localStorage is risky

Anything running in your page can read localStorage: a compromised npm package, an injected ad script, a stored XSS in a comment field. Once the token is read, the attacker is logged in as your user from anywhere, until it expires.

How to check your own app

  1. Log in, open DevTools, then Application, then Local Storage and Session Storage.
  2. If you see keys like token, access_token, jwt or supabase.auth.token holding a long eyJ... string, it is readable by page scripts.
  3. Decode the token at any JWT decoder and check the exp claim. Tokens that last 30 days or never expire are a bigger problem.

Only check apps you own.

Safer pattern

  • Keep the session in an HttpOnly; Secure; SameSite=Lax cookie set by your server, so page scripts cannot read it.
  • Use short-lived access tokens (minutes) with a refresh flow.
  • Add a Content-Security-Policy so injected scripts are less likely to run.
  • Never put secrets or admin flags in the JWT payload. It is encoded, not encrypted.

If your framework forces client storage, keep expiry short and rotate on logout and password change.

Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.