Urgent.News

What's breaking now, across thousands of outlets.

Tech

Stop Storing JWTs in localStorage. Here's Why.

You've probably seen this in a React authentication tutorial: localStorage . setItem ( " token " , response . token ); User logs in. Save the JWT. Send it with every API request. Simple, right? There's one problem: any JavaScript running on your page can read that token. And that includes malicious JavaScript. What's Actually Wrong With localStorage? Imagine your application has an XSS…

Many tutorials suggest storing JWTs in localStorage when implementing authentication. It seems straightforward: save the authentication token and include it with each API request. However, this approach has a significant flaw. Any script running on your webpage can access this token, including malicious JavaScript. If your site has an XSS vulnerability, an attacker could steal the token and gain access to the user's account until it expires or is revoked.

This risk isn't specific to JWTs; any sensitive data stored in localStorage is vulnerable. A better solution is to use HttpOnly cookies instead of localStorage. Set-Cookie headers can help secure these cookies by limiting access to JavaScript. HttpOnly means JavaScript can't read the cookie, Secure ensures the browser only sends it over HTTPS, and SameSite=Strict prevents cross-site requests from sending the cookie.

This way, even if an attacker exploits XSS, they can't access the HttpOnly cookies. However, HttpOnly cookies aren't foolproof. Malicious JavaScript can still send authenticated requests, and CSRF attacks can still occur. To mitigate CSRF risks, implement proper protections like validating request origins, using SameSite cookies, and adding CSRF tokens where necessary.

The double-submit cookie pattern, where the server validates a CSRF token with each request, is a common approach. Additionally, using short-lived JWTs and rotating signing keys securely is crucial. By issuing tokens with limited lifetimes and carefully managing key rotation, you can minimize the impact of potential compromises.

While localStorage can still be used for non-sensitive settings, JWTs for authentication should be stored securely using HttpOnly cookies. Authentication is about more than just login; it requires careful consideration of how tokens are stored, protected, and revoked when needed.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Punjab to Outsource More Govt Schools

The Punjab School Education Department has launched the fourth phase of its school outsourcing program, with plans to hand over … Read More The post Punjab to Outsource More Govt Schools appeared…

Google Recommends Custom 404 Pages and More Selective Redirects

A missing page does not automatically harm a site’s Google rankings. Google’s recent guidance instead places the emphasis on handling dead URLs accurately and helping visitors recover when a page is…

  • Google recommends custom 404 error pages and selective redirects to improve user experience and SEO.
  • Redirect retired URLs only when a suitable substitute exists; return 404 or 410 when not applicable.

More from Thursday 29 October →