
A JWT Is Signed, Not Encrypted: Anyone Can Read Your Token
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
A signed JWT is three Base64url parts
{base64url(json(header))}.{base64url(json(claims))}.{base64url(signature)}. That's the JWS compact serialization from RFC 7515. The header and the claims are plain JSON, run through url so they're safe in URLs and headers. The eyJ that starts almost every token is just how Base64 begins {", the opening of a JSON object.
The signature protects integrity, not secrecy
HTTPS doesn't keep the claims private either
Encryption is a different format: JWE
What the token carried
using System.Security.Claims;using Microsoft.IdentityModel.JsonWebTokens;using Microsoft.IdentityModel.Tokens;public sealed class TokenIssuer(SigningCredentials signing){ public string Issue(Customer c) => new JsonWebTokenHandler().CreateToken( new SecurityTokenDescriptor { Subject = new ClaimsIdentity( [ new Claim("sub", c.Id.ToString()), new Claim("email", c.Email), new Claim("phone", c.Phone), new Claim("date_of_birth", c.DateOfBirth.ToString("O")), new Claim("credit_limit", c.CreditLimit.ToString()), ]), Expires = DateTime.UtcNow.AddMinutes(15), SigningCredentials = signing, // signed, not encrypted });}using System.Security.Claims;using Microsoft.IdentityModel.JsonWebTokens;using Microsoft.IdentityModel.Tokens;public sealed class TokenIssuer(SigningCredentials signing){ // Who, for which API, until when. The API looks up the rest by sub. public string Issue(Customer c) => new JsonWebTokenHandler().CreateToken( new SecurityTokenDescriptor { Subject = new ClaimsIdentity([new Claim("sub", c.Id.ToString())]), Issuer = "https://auth.example.com", Audience = "orders-api", Expires = DateTime.UtcNow.AddMinutes(15), SigningCredentials = signing, });}export function readClaims(token: string): unknown { const payload = token.split(".")[1] ?? ""; const base64 = payload.replace(/-/g, "+").replace(/_/g, "/"); return JSON.parse(atob(base64));}{ "exp": 1793740500, "iat": 1793739600, "nbf": 1793739600, "sub": "7d4c1a2e-5b8f-4e2a-9c61-3f0b8d2a6e45", "email": "mona@example.com", "phone": "+201000000000", "date_of_birth": "1995-04-12", "credit_limit": "50000"}credit_limit to 900000 and kept the original signature, ValidateTokenAsync returned IsValid = false. Readable by anyone, changeable by no one. The fixed version carries only a stable user ID, the issuer, the audience and an expiry, so there's nothing worth reading.
How to apply it in your project
Decode the tokens you issue today Take an access token and an ID token from a test account, decode them, and list every claim. Assume anyone who sees the token can read the same list. Cut the payload to what authorization needs Keep a stable, non-personal user ID in sub, plusiss,aud,expand any scopes or roles the API checks. Remove personal data and internal business values.Look up the rest on the server Fetch profile and account data by subwhen a request needs it, or use opaque reference tokens, as OWASP prefers, for sessions that carry sensitive context.Use JWE only for claims that must travel If another party must read private claims and can't ask your server, sign the token and then encrypt it as a JWE. Validate both layers on the receiving side. Harden validation while you're there Pin the accepted algorithms, use a long random key for HS256 or an asymmetric key pair, and check issuer, audience and expiry on every request.
Key takeaways
A signed JWT is Base64url-encoded JSON plus a signature; anyone can read its claims. The signature proves who issued the token and that it wasn't changed; it hides nothing. HTTPS protects the token in transit only; logs, browser storage and scripts can still read it. Keep personal and business data out of the payload; look it up on the server by sub.Use JWE, signed then encrypted, only when private claims must travel to another party.
التعليقات (0)