Urgent.News

What's breaking now, across thousands of outlets.

Tech

7 JWT Security Mistakes I See in Almost Every Auth Implementation

Most JWT security problems don't come from a broken library. They come from a handful of small decisions made once during setup and never revisited: which algorithm to trust, where to store the token, what to actually validate on the way in. Here are the ones that come up most often, and what to do instead. 1. Trusting the alg header instead of specifying it yourself If your verification code…

1. In most JWT security issues, the root cause is not a broken library but rather a few essential decisions that are made during setup and then ignored. These decisions include choosing an algorithm to trust, where to store the token, and what to validate upon receiving the token. The most common issues that arise are discussed below, along with suggested solutions.

2. Trusting the algorithm header rather than explicitly specifying it can leave you vulnerable to algorithm confusion attacks, such as the classic "alg: none" bypass. To avoid this, specify the expected algorithm in your library's verification code rather than allowing the token to determine it. This ensures that only the intended algorithm is used for token verification.

3. Storing tokens in localStorage is a risky practice as any script running on the page can read localStorage, including compromised npm packages or XSS payloads. Instead, use HttpOnly cookies with Secure and SameSite flags, which are not readable by JavaScript. This prevents unauthorized access to the token and enhances security overall.

4. Another critical mistake is issuing long-lived access tokens without a revocation plan. Since JWTs cannot be deleted once issued, it's crucial to consider this before deploying your application. A short expiration time (15 minutes is a reasonable default) combined with a server-side revocable refresh token can mitigate most risks without the need for a full blocklist.

5. Skipping claim validation beyond just signature checks is another prevalent mistake. While a verified signature proves that the token hasn't been tampered with, it provides no information about its expiration, intended service, or issuer. Always explicitly check the exp (expiration), iss (issuer), and aud (audience) claims to ensure the token is valid for your specific service and has not expired.

6. Assuming RS256 is inherently safer than HS256 is a misconception. The safety of an algorithm depends on the setup rather than the algorithm name itself. For a single service verifying its own tokens, HS256 is sufficient. However, when multiple services need to independently verify tokens, RS256's public/private key split becomes essential. Neither algorithm is inherently more secure in isolation; the key lies in the implementation.

7. Finally, never assume that verifying token failures is unnecessary. Most teams focus on testing the happy path extensively but rarely consider the failure scenario. When handling an expired token, a tampered signature, or a token signed by the wrong key, it's crucial to know how your application behaves in these situations. Conduct thorough testing to ensure your app can handle unexpected token issues gracefully, which is vital for maintaining overall security.

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

AMD buys chip startup that hardwires AI models into its silicon

Taalas' current chip runs a small version of Meta's Llama 3.1, though the company is working on chips for bigger and more advanced models.

  • AMD acquires Toronto-based AI chip startup Taalas.
  • Taalas specializes in custom, hard-wired chips for AI models.
  • Acquisition aims to expand AMD's AI solutions portfolio.

The Synth Sounded Fine When Nobody Was Listening

This is my entry for DEV's Summer Bug Smash — Smash Stories. There is a specific kind of bug that makes you doubt your own ears.

  • Browser-based audio engine muffled in real-time playback
  • Live playback path ran on scheduler, offline render had none
  • Fix increased backward-seek threshold to prevent node cancellation

One Extra Slash: Hunting a Windows Path Bug Down to a Single Formatter

This is my entry for DEV's Summer Bug Smash — Smash Stories. Not every good bug is an epic. Some of them are one character long, and the interesting part is entirely in the hunt.

  • Single extra slash in Windows file path caused by formatter
  • Bug missed initially due to no error in path handling
  • Issue identified by comparing raw bytes with processing code

More from Thursday 6 August →