Demystifying JSON Web Tokens: Structure, Signature Verification & Security Pitfalls
JSON Web Tokens (RFC 7519) are the standard mechanism for stateless authentication across modern microservices, SPAs, and API gateways. However, because JWTs are simply Base64URL-encoded strings, developers frequently confuse token signing with token encryption—leading to severe security vulnerabilities such as credential leakage and unverified claims processing.
In this guide, we break down the anatomy of a JWT, examine how cryptographic signatures work under the hood, and audit the most common implementation vulnerabilities observed in production systems.
1. The Anatomical Breakdown of a JWT
A JSON Web Token consists of three distinct parts separated by period (.) delimiters:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c3JfMTIzIiwibmFtZSI6IkFsZXgiLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE3Mjk5MDAwMDB9.s1G...[signature]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.^^^^^^^^^^^^^^^^^
1. Header 2. Payload 3. Signature
- Header: Specifies the token type (
typ: "JWT") and the cryptographic signing algorithm (alg: "HS256"oralg: "RS256"). - Payload: Contains claims about the subject (user ID, tenant, email, scopes) alongside standard timestamps (
iat: issued at,exp: expiration,nbf: not before). - Signature: The cryptographic proof generated by hashing the header and payload with a secret key or signing with a private RSA/ECDSA key.
2. Symmetric (HMAC) vs. Asymmetric (RSA/ECDSA) Signing
| Feature | Symmetric (HS256 / HS512) | Asymmetric (RS256 / ES256) |
|---|---|---|
| Keys Used | Single shared secret string | Private key to sign, Public key to verify |
| Verification Trust | Any service verifying the token can also forge it | Microservices verify tokens using only the public key |
| Best Use Case | Monolithic backends & internal microservices | OAuth2 / OIDC, Auth0, Supabase, multi-tenant APIs |
3. The Most Dangerous JWT Security Vulnerabilities
A. The "Algorithm None" Exploit (CVE-2015-9235)
The JWT specification originally permitted an algorithm called "none" for un-signed debugging tokens. Malicious actors realized they could decode an existing token, elevate their role to "role": "admin", set "alg": "none" in the header, strip the signature, and submit it to vulnerable authentication libraries that accepted unsigned tokens as valid.
Remediation: Explicitly whitelist allowed signing algorithms on your verification endpoints (e.g. jwt.verify(token, secret, { algorithms: ['HS256'] })).
B. The Key Confusion Attack (HMAC vs. RSA)
If an API expects an RS256 token signed with a private key, an attacker might obtain the server's public RSA certificate (often published openly at /.well-known/jwks.json), re-sign the token using HMAC-SHA256 with the public key as the symmetric secret, and change the header to "alg": "HS256". If the verification routine does not enforce algorithm matching, it verifies the signature against the public key string and grants unauthorized access.
C. Confusing Encoding with Encryption
A standard JWT is Base64URL-encoded, NOT encrypted. Anyone with access to the raw token string (via network interception, browser local storage, or server logs) can inspect every claim in plain text. Never store database connection strings, passwords, or unencrypted Social Security / credit card numbers inside a standard JWT payload.
4. Verifying JWT Signatures Locally in Browser JavaScript (WebCrypto)
You can verify a token's HMAC-SHA256 signature locally inside modern browsers without uploading secrets to any remote server:
async function verifyJwtHmac(token, secretString) {
const [headerB64, payloadB64, signatureB64] = token.split('.');
const dataToSign = new TextEncoder().encode(headerB64 + '.' + payloadB64);
// Convert secret into CryptoKey
const key = await crypto.subtle.importKey(
'raw',
new TextEncoder().encode(secretString),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['verify']
);
// Decode signature from base64url
const signatureBytes = Uint8Array.from(
atob(signatureB64.replace(/-/g, '+').replace(/_/g, '/')),
(c) => c.charCodeAt(0)
);
return await crypto.subtle.verify('HMAC', key, signatureBytes, dataToSign);
}
🔍 Inspect & Audit JWT Claims Securely
Need to verify expiration dates, check issued claims, or debug token headers without risking token exposure to external servers? Use our Client-Side JWT Decoder & Claims Inspector. Everything is parsed 100% in local memory.