Search tools

Developer guides

JWT alg:none Vulnerability: Why jwt.decode() Fails Security Audits

Calling jwt.decode() skips signature checks and leaves APIs exposed to alg:none token forgery. Learn how verification works and fix it cleanly.

Minimal 3D frosted glass cubes linked in sequence on dark obsidian: an unlatched crimson cube, a teal data block, and a crystalline teal padlock seal

Reading claims with jwt.decode() instead of jwt.verify() lets attackers forge admin tokens without knowing your secret key. A JWT has three Base64URL parts separated by dots, and decode() blindly parses the first two without checking the third.

Here is the immediate fix: never use jwt.decode() for authentication, always call jwt.verify() with an explicit algorithms allowlist, and validate exp, iss, and aud claims on every request.

To inspect a token’s header algorithm, payload timestamps, and signature bytes safely in your browser, paste it into our JWT decoder.

What is the JWT alg:none vulnerability?

The JSON Web Signature (JWS) standard defines "alg": "none" for tokens that require no cryptographic signature. Vulnerable servers accept unsigned tokens whenever verification code reads the token’s own header to decide which algorithm to run.

An attacker builds a header containing {"alg":"none","typ":"JWT"}, puts {"sub":"1","role":"admin"} in the payload, strips the signature after the second dot, and sends the forged string.

Look at how tiny that forged token looks (eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJyb2xlIjoiYWRtaW4ifQ.):

Header (Base64URL):   eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0
Payload (Base64URL):  eyJyb2xlIjoiYWRtaW4ifQ
Signature:            <empty string after trailing dot>

Notice the trailing dot at the end. Even with zero signature bytes, a permissive JWT library sees "alg": "none" and marks the payload valid.

Why is jwt.decode() unsafe in authentication middleware?

decode() only Base64URL-decodes the header and payload JSON strings into objects. It never checks cryptographic signatures, rejects expired timestamps, or notices tampered user IDs.

jwt-alg-none-vulnerability-and-signature-verification-diagram

Teams often confuse decode() with verification because decoded output looks identical to a valid session object. Use decode() only for logging public client metadata, never for access control decisions.

Method / Option Checks signature? Blocks alg:none? Validates exp? Safe for auth?
jwt.decode(token) No No No Never
jwt.verify(token, key) without algorithms Yes Usually, risky with RS/HS Yes Partial
jwt.verify(token, key, { algorithms: ["RS256"] }) Yes Yes Yes Yes

How does HS256/RS256 key confusion bypass signatures?

Even when libraries block "alg": "none", leaving algorithms unspecified creates a second exploit path called asymmetric key confusion. An attacker reads your server’s public RSA key (often published at a JWKS endpoint) and modifies the token header to "alg": "HS256".

If your verification helper uses that same RSA public key file as an HMAC shared secret, it computes the HS256 signature using public bytes anyone can fetch. Pinning expected algorithms stops the token header from choosing the cryptographic check.

How to verify JWTs safely in Node.js and Python

Pass your allowed algorithms explicitly to verify() so the server ignores untrusted alg headers. You should also verify the issuer (iss), audience (aud), and expiration timestamp (exp).

In Node.js using jsonwebtoken:

import jwt from "jsonwebtoken";

export function verifyAccessToken(token, publicKey) {
  return jwt.verify(token, publicKey, {
    algorithms: ["RS256"],
    issuer: "https://auth.example.com",
    audience: "api.example.com",
    clockTolerance: 5,
  });
}

In Python using PyJWT:

import jwt

def verify_access_token(token: str, public_key: str) -> dict:
    return jwt.decode(
        token,
        public_key,
        algorithms=["RS256"],
        issuer="https://auth.example.com",
        audience="api.example.com",
        leeway=5,
    )

Notice how PyJWT names its verified function jwt.decode() while Node’s jsonwebtoken names it jwt.verify(). Checking your library’s exact API prevents accidental bypasses during code reviews.

When debugging exp or iat claim rejections, remember JWT timestamps count seconds since epoch rather than JavaScript milliseconds. Read our guide on Unix timestamps in seconds vs milliseconds if valid tokens fail immediately as expired.

JWT verification FAQ

Can anyone read my JWT payload without the secret key?

Yes. Standard signed JWTs (JWS) are only signed, not encrypted. Anyone who intercepts the string can Base64URL-decode the header and payload, so never put passwords or private keys inside claims.

Why did jwt.verify fail with “invalid algorithm”?

Your middleware’s algorithms allowlist rejected the token header’s alg field. Confirm your auth provider signs with the same algorithm (RS256, ES256, or HS256) that your backend expects.

Should API servers skip signature verification behind an API gateway?

Only if private network rules strictly block direct traffic around the gateway. Even behind a proxy, keeping local jwt.verify() enabled gives defense in depth against misconfigured firewall rules.

Paste any token header or expired claim payload into the browser-local JWT decoder to inspect every field without sending credentials over the network.

Read next