LambdaWorks
Product InnovationTech ExcellenceAI EnablementOur WorkBlogGet in touch
LambdaWorks
Home
SERVICES
Product Innovation
Tech Excellence
AI Enablement
Music Industry
TypeScript Services
Scala Services
SPRINTS
MVP Sprint
Modernization Pilot
AI Validation Sprint
Ongoing Dev
Software Factory
ABOUT
Company
Our Work
Blog
Careers
Contact
Tech Radar
Privacy Policy
office@lambdaworks.io
+381 63 834 94 29
Miše Dimitrijevića 12
Novi Sad, Serbia
InstagramLinkedInX
2026 © LambdaWorks d.o.o.
We build digital products.
  1. Home
  2. /Blog
  3. /JWT Best Practices
Data & BackendTutorial

JWT Best Practices

JWTs are powerful for authentication, but require strict security —validate claims, store securely, rotate keys, guard against XSS and more to prevent from attacks.

Ivana Kašiković·Aug 8, 2025·4 min read

Think Your JWTs Are Safe? Think Again.

In today’s digital world, where apps constantly exchange sensitive data, security is an important part of every application. Whether you're protecting authentication data or internal APIs, keeping data safe should be a top priority. These days, many apps rely on JSON Web Tokens (JWTs) for authentication. They're simple, efficient, and widely used for powering modern API flows and making stateless sessions fast and scalable.

But don’t let that simplicity fool you. Although JWTs might be easy to set up, a small mistake in implementation can lead to serious problems, from exposing user data to giving attackers unauthorized access. The impact of a badly handled token can cause huge damage.

Never Trust the Header Alone

One of the sneakiest attack vectors in JWTs hides in a place you might overlook, the header. This part of the token tells your app which algorithm was used to sign it. Sounds straightforward, but the problem is that attackers can inject their value and bypass your security.

ivana blog.jpg

Back in the past, researchers (with help from Auth0) discovered a clever attack. If your app expects tokens signed with RS256 (asymmetric), an attacker could modify the token header to say HS256 (symmetric) instead. Then, using your public key as the HMAC secret, they could forge a valid-looking token. Suddenly, they’re authenticated as anyone they want.

Even worse, some JWT libraries once accepted none value for alg, meaning no signature at all. Attackers could send unsigned tokens, and the server would detect them as valid, that way they could completely bypass authentication.

To avoid this situation, never trust the algorithm declared in the token itself. Instead, enforce the expected algorithm on the server side, hardcode it or keep a strict whitelist (like only allowing RS256). Also, make sure you’re using solid JWT libraries that handle signature validation securely by default and make sure that you keep those libs up to date.

Use Secure Storage — Stop Leaving Tokens Lying Around

Storing JWTs in Local Storage or Session Storage might seem convenient, but it’s a dangerous trap. These storage locations are accessible to JavaScript, so if your site suffers from an XSS vulnerability, attackers can steal tokens and impersonate users. This combination of XSS and JWT theft is a recipe for disaster.

A safer way to keep tokens in is in secure, HTTP-only cookies. They are safe from XSS attacks since JavaScript cannot access them. Additionally, to lower the danger of CSRF, make sure to set the Secure and SameSite flags.

If you must use Local Storage (e.g., in SPAs or mobile apps) pay attention to:

  • enforce strict Content Security Policies,
  • sanitize all inputs,
  • use short-lived tokens,
  • pair access tokens with securely stored refresh tokens to maintain usability without compromising security.

Additionally, make sure to implement token expiration and revocation strategies. Use short-lived access tokens with refresh tokens and maintain token blacklisting to revoke leaked tokens. Also, have in mind to regularly rotate your signing keys using JWT’s kid header to keep things running smoothly.

Validate All Claims — Don’t Just Decode, Enforce

Don’t blindly trust JWT contents just because the signature’s valid. Just because a JWT's signature is valid doesn’t mean the token is safe to trust.

For secure apps it is crucial to check critical claims like:

  • exp (Expiration Time): without enforcing this, a hacker could reuse an expired token indefinitely.
  • nbf (Not Before): this ensures the token isn’t used before it’s supposed to be — an often-skipped check that can open timing-based attack windows.
  • iat (Issued At): useful for freshness checks and debugging, but can also be part of replay attack detection.
  • aud (Audience): crucial. If your app doesn’t validate that the token was intended for it, an attacker might trick your backend into accepting a token meant for another service.
  • scope: specifies the exact permissions the token holder has — defining what actions the client can perform on a protected resource on the user's behalf. It is a key part of permission control — without checking, you risk granting users more access than intended.

These aren’t always auto-validated by libraries. Also be careful how you handle decoded claims. If you merge JWT payloads into user objects, attackers can inject malicious nested fields like __proto__ to grant privileges through prototype pollution. That's the case especially in JavaScript environments.

Verifying the signature is only step one. To truly trust the token, you need to validate the full context — every claim, every time. Otherwise, you're giving attackers a signed pass with no expiration date or destination check.

Conclusion

All in all, JWTs are powerful tools, but you have to use them carefully. Their convenience and efficiency make them ideal for modern authentication, yet a single oversight can turn them into a dangerous weak point. So pay attention that you store them securely, validate properly, and never assume they’re safe just because they "look" valid.

Secure your tokens before they betray you.

References:

  • Portswigger. (n.d.) JWT attacks
  • JWT.io. (2010, December 1) JSON Web Token Introduction
Share on:

On this page

  1. Never Trust the Header Alone
  2. Use Secure Storage — Stop Leaving Tokens Lying Around
  3. Validate All Claims — Don’t Just Decode, Enforce
  4. Conclusion

More from the LambdaWorks blog

Data & BackendTutorialOct 10, 2025

From Prisma to Zod: Streamlined Types, Validation, and Error Handling

If you're tired of rewriting your data structures multiple times in different places, see how you can take your Prisma schemas and turn them into powerful Zod validation and OpenAPI docs — all from one source of truth. Keep your TypeScript code clean, consistent, and bug-free with less hassle!

Aleksandar Petaković
AI & AgentsSoftware DesignTutorialNov 12, 2026

Spec-driven development with Claude Code

Dimitrije Bulaja introduces spec-driven development, compares GitHub Spec Kit, OpenSpec, and GSD, and walks through a feature in Claude Code. Learn how each framework structures requirements, planning, and implementation.

Dimitrije BulajaDimitrije Bulaja
Data & BackendCase StudyAug 28, 2026

Every Query Hit All 18 Shards. Then We Bent the Index Around the Planet.

See how we used Google S2 geosharding to route local searches across a 55-million-document OpenSearch index from 18 shards to as few as one.

Dragutin MarjanovićDragutin Marjanović

Have a project in mind?

Tell us what you're building. We'll tell you how we'd approach it.

Let's talk

An Agentic Software Development Agency

office@lambdaworks.io·+381 63 834 94 29

Miše Dimitrijevića 12, Novi Sad, Serbia

Privacy Policy
2026 © LambdaWorks d.o.o.

An Agentic Software Development Agency

office@lambdaworks.io+381 63 834 94 29

Miše Dimitrijevića 12, Novi Sad, Serbia

SERVICESProduct InnovationTech ExcellenceAI EnablementMusic IndustryTypeScript ServicesScala Services
SPRINTSMVP SprintModernization PilotAI Validation SprintOngoing DevSoftware Factory
ABOUTCompanyOur WorkBlogCareersContactSoftware Factory
Privacy Policy
InstagramLinkedInX
2026 © LambdaWorks d.o.o.