Posts Tagged ‘Cryptography’
[DevoxxFR 2022] Do You Really Know JWT?
Do You Really Know JWT? Insights from Devoxx France 2022
Karim Pinchon, a backend developer at Ornikar, delivered an illuminating talk titled “Do You Really Know JWT?” (watch on YouTube). With a decade of experience across Java, PHP, and Go, Karim dives into JSON Web Tokens (JWT), a standard for secure data transfer in authentication and authorization. This session explores JWT’s structure, cryptographic foundations, vulnerabilities, and best practices, moving beyond common usage in OAuth2 and OpenID Connect.
Understanding JWT Structure and Cryptography
Karim begins by demystifying JWT, a compact, secure token for transferring JSON data, often used in HTTP headers for authentication. A JWT comprises three parts—header, payload, and signature—encoded in Base64 and concatenated with dots. The header specifies the cryptographic algorithm (e.g., HMAC, RSA), the payload contains claims (data), and the signature ensures integrity. Karim demonstrates this using jwt.io, showing how decoding reveals JSON objects.
He distinguishes token types: reference tokens (database-backed) and value tokens (self-contained, like JWT). JWT supports two forms: compact (Base64-encoded) and JSON (with additional features like multiple signatures). Karim introduces related standards under JOSE (JSON Object Signing and Encryption), including JWS (signed tokens), JWE (encrypted tokens), JWK (key management), and JWA (algorithms). Cryptographic operations like signing (for integrity) and encryption (for confidentiality) underpin JWT’s security.
Payload Claims and Use Cases
The payload is JWT’s core, divided into three claim types:
- Registered Claims: Standard fields like issuer (
iss
), audience (aud
), expiration (exp
), and token ID (jti
) for validation. - Public Claims: Defined by IANA for protocols like OpenID Connect, carrying user data (e.g., name, email) in ID tokens.
- Private Claims: Custom data agreed upon by parties, kept minimal for compactness.
Karim highlights JWT’s versatility in:
- API Authentication: Tokens in
Authorization
headers validate requests without database lookups. - OAuth2: Access tokens may be JWTs, carrying authorization data.
- OpenID Connect: ID tokens propagate user identity.
- Stateless Sessions: Storing session data (e.g., e-commerce carts) client-side, enhancing scalability.
He cautions that stateless sessions require careful implementation to avoid complexity.
Security Vulnerabilities and Attacks
Karim dedicates significant time to JWT’s security risks, demonstrating attacks via a PHP library on his GitHub. Common vulnerabilities include:
- Unsecured Tokens: Setting the header’s algorithm to
none
bypasses signature verification, a flaw exploited in some libraries. Karim shows a test where a modified token passes validation due to this. - RSA Public Key as Shared Key: An attacker changes the algorithm from RSA to HMAC, using the public key as a shared secret, tricking servers into validating tampered tokens.
- Brute Force: Weak secrets (e.g., “azerty”) are vulnerable to brute-force attacks.
- Encrypted Data Modification: Some encryption algorithms allow payload tampering (e.g., flipping
is_admin
fromfalse
totrue
) without breaking the cipher. - Token Substitution: Using a token from one service (where the user is admin) on another without proper audience validation.
Karim emphasizes the JWT paradox: the header, which specifies validation details, can’t be trusted until the token is validated. He attributes these issues to developers’ reliance on unvetted libraries, not poor coding.
Best Practices for Secure JWT Usage
To mitigate risks, Karim offers practical advice:
- Protect Secrets: Use strong, rotated keys. Avoid sharing symmetric keys with external partners; prefer asymmetric keys (e.g., RSA).
- Restrict Algorithms: Servers should only accept predefined algorithms (e.g., one or two), ignoring the header’s
alg
field. - Validate Claims: Check
iss
,aud
, andexp
to ensure the token’s legitimacy. Reject tokens not intended for your service. - Use Trusted Libraries: Avoid custom implementations. Modern libraries require explicit algorithm whitelists, reducing
none
algorithm risks. - Short Token Lifespans: Minimize revocation needs with short-lived tokens. Avoid external revocation lists, as they undermine JWT’s autonomy.
- Ensure Confidentiality: Since JWS payloads are Base64-encoded (readable), avoid sensitive data. Use JWE for encryption if needed, and transmit over HTTPS.
Karim also mentions alternatives like Biscuits (from Clever Cloud), PASETO, and Google’s Macaroons, which address JWT’s flaws, such as untrusted headers.
Links
- YouTube Video: Do You Really Know JWT?
- Karim Pinchon: LinkedIn, Twitter, GitHub
- Ornikar: Official Website
- JWT: Official Website
Hashtags: #DevoxxFrance #KarimPinchon #JWT #Security #Cryptography #Authentication #Authorization #OAuth2 #OpenIDConnect #JWS #JWE #JWK #Ornikar #PHP #Java