JSON Web Token
| Abbreviation | JWT |
|---|---|
| Standard | RFC 7519 (IETF, May 2015) |
| Security practice | RFC 8725, BCP 225 (February 2020) |
| Related weakness | CWE-347: Improper Verification of Cryptographic Signature |
| Used by | Sign-in sessions and API access, for example Supabase Auth access tokens |
After a user logs in, many web applications hand the browser a JSON Web Token (JWT) as proof of the completed sign-in, and the browser sends it back with each request. The token is a short text string that carries statements, called claims, about a user or another party, written as a JSON object and protected by a signature or by encryption. The IETF defined the format in RFC 7519 in May 2015 to fit in places with little room, such as an HTTP Authorization header or a query parameter.1
Structure
A signed JWT has three parts joined by full stops: a header, a payload and a signature. Each part is written in base64url. The standard's own example, broken over three lines for display:1
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9
.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ
.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXkDecoded, the header reads {"typ":"JWT","alg":"HS256"}, which names the signing algorithm, and the payload reads {"iss":"joe","exp":1300819380,"http://example.com/is_root":true}. The third part is an HMAC with SHA-256, computed over the first two parts.1
The signature lets a recipient detect a change to the header or the payload. Base64url can be reversed by anyone, so whoever holds the token can read every claim.2 Tokens end up in logs, in browser storage and in Referer headers, and a payload should carry no data that its holder or a bystander must not see. Where the claims must stay secret, the standard defines an encrypted form that uses JSON Web Encryption.1
Claims
Seven claim names are registered, each kept to three letters so the token stays small. All of them are optional in the standard, and each application decides which it requires.1
| Claim | Meaning |
|---|---|
iss | Issuer: who created the token |
sub | Subject: whom the token is about, usually a user ID |
aud | Audience: which recipients the token is meant for. A recipient not named in it must reject the token |
exp | Expiration time. From that moment the token must not be accepted |
nbf | Not before: the token is invalid until this time |
iat | Issued at: when the token was created |
jti | JWT ID: a unique value that can be used to detect a replayed token |
Supabase Auth issues a short-lived JWT access token for each signed-in user. Its sub claim holds the user ID and its role claim names the Postgres role, such as authenticated, that row level security policies apply.3
Signing keys
The alg header selects one of two kinds of protection. With a MAC algorithm such as HS256, the same shared secret signs and verifies, so a valid signature proves only that some holder of the secret produced the token. With a digital signature such as RS256, the issuer signs with a private key and recipients verify with the matching public key, which may be published.2 Supabase publishes its projects' public keys at /auth/v1/.well-known/jwks.json and advises against verifying with a shared secret, since anyone who obtains that secret can create tokens for any user.3
Sessions and revocation
A server can check a JWT without a database lookup, which is why JWTs are proposed for "stateless" sessions. The cost is that a signed token stays valid until exp, even after the user signs out. Ending such a session early takes a deny list of revoked tokens, keyed on jti and iss, and that list brings back the server-side state the design set out to avoid; a plain session system may then be the simpler choice. Short expiry times limit how long a copied token can be reused.2
Verification failures
The token names its own algorithm, and the header is read before anything is verified, so the sender of the token, who may be an attacker, chooses the algorithm the server is told to use.4 Libraries that trusted that header field could be attacked through it.5 With "alg": "none" and an empty signature, some libraries accepted any payload as verified. In algorithm confusion, a token expected as RS256 arrives marked HS256, and the library uses the RSA public key, which is public, as the HMAC secret. Tim McLean described both in March 2015.4 In the Node.js package jsonwebtoken, versions 4.2.1 and earlier were affected; the flaw is CVE-2015-9235, rated critical, and was fixed in 4.2.2.6
An HS256 secret that is short or memorable can be found offline by trying candidate keys against one captured token, so RFC 8725 forbids using a human-memorable password as the key.5 Header fields such as jwk and jku can carry or point to a key; a server that trusts a key named by the token accepts tokens the attacker signed. A token issued for one service can be replayed against another that trusts the same issuer when aud is not checked.2 Each of these is a case of improper verification of a cryptographic signature.7
Verifying in code
RFC 8725 requires libraries to let the caller fix the accepted algorithms and to reject the whole token if any check fails.5 In the Node.js library jose, jwtVerify checks the signature and the claims, never accepts none, and requires iss or aud to be present when the issuer or audience option is set.8 Supabase gives this form for its tokens, here with the issuer added:3
import { createRemoteJWKSet, jwtVerify } from 'jose'
const JWKS = createRemoteJWKSet(
new URL('https://project-id.supabase.co/auth/v1/.well-known/jwks.json')
)
const { payload } = await jwtVerify(token, JWKS, {
issuer: 'https://project-id.supabase.co/auth/v1'
})In jsonwebtoken, jwt.verify(token, publicKey, { algorithms: ['RS256'] }) rejects a token whose header names another algorithm. Its jwt.decode returns the payload without checking the signature, and its documentation warns against using it on untrusted tokens.9 Inside a Supabase app, supabase.auth.getClaims() verifies a Supabase-issued token.3
In SecFlash
SecFlash reports code that reads tokens without checking the signature, accepts unsigned tokens or signs them with a short secret written in the code as SF-SEC-738 on the Security page. A JWT found in a public script appears as SF-EXP-302 and one written into the HTML for logged-out visitors as SF-EXP-305, both on the Exposure page, with the decoded payload used to judge what the token grants. A login token kept in localStorage is reported as SF-SEC-503 on the Security page.
See also
- Session management
- Session hijacking
- Authentication
- Privileged key in client-side code
- Row level security
- Category: Security concepts
References
- a b c d e Jones, M., Bradley, J. and Sakimura, N. (May 2015). "RFC 7519: JSON Web Token (JWT)". RFC Editor. IETF.
- a b c d "JSON Web Token Cheat Sheet". OWASP Cheat Sheet Series. OWASP Foundation.
- a b c d "JSON Web Token (JWT)". Supabase Docs.
- a b McLean, Tim (March 2015). "Critical vulnerabilities in JSON Web Token libraries". First published on the Auth0 blog.
- a b c Sheffer, Y., Hardt, D. and Jones, M. (February 2020). "RFC 8725: JSON Web Token Best Current Practices" (BCP 225). RFC Editor. IETF.
- back GitHub Advisory Database (9 October 2018, updated 9 January 2023). "Verification Bypass in jsonwebtoken, CVE-2015-9235" (GHSA-c7hr-j4mj-j2w6). GitHub.
- back "CWE-347: Improper Verification of Cryptographic Signature". Common Weakness Enumeration 5.0. MITRE.
- back "Interface: JWTVerifyOptions". jose documentation. GitHub.
- back "jsonwebtoken" README. auth0/node-jsonwebtoken. GitHub.
External links
- RFC 7515: JSON Web Signature (JWS), RFC Editor
- JSON Web Token (JWT), Supabase Docs