Definition
JSON Web Token (JWT) security involves practices and mechanisms to ensure JWTs' integrity, confidentiality, and validity, crucial for authentication and information exchange. JWTs are digitally signed tokens asserting claims between parties, often used in web authentication.How It Works
- 1Token Creation: A server creates a JWT by encoding claims and signing them with a secret or public/private key pair.
- 2Token Transmission: The token is sent to the client, usually as an HTTP header.
- 3Token Verification: The server checks the JWT's signature and validates claims like expiration and issuer.
- 4Access Granting: If valid, the server grants the requested access or service.
Key Characteristics
- Base64URL Encoding: JWTs have three parts: header, payload, and signature, encoded in Base64URL.
- Algorithms: Supports signing algorithms like HMAC, RSA, and none ('none' is risky).
- Claims: Includes registered claims (e.g., iss, exp) and custom claims.
Comparison
| Feature | JWT | OAuth 2.0 |
|---|---|---|
| Format | JSON-based | Token-based |
| Use Case | Authentication, Data Safety | Authorization |
| Security | Signature, Claims | Scopes, Expiry |
Real-World Example
CVE-2022-23529 reveals a vulnerability in the jsonwebtoken library where improper algorithm handling could lead to security bypasses. An attacker could exploit this by switching from RS256 to HS256.Detection & Prevention
- Use Strong Secrets: Ensure the signing secret or key is complex.
- Algorithm Restriction: Avoid 'none' and enforce strict algorithm policies.
- Expiration Enforcement: Set expiration (exp) claims to limit token lifespan.
- Secure Transmission: Use HTTPS to prevent token interception.
- Testing Tools: Use jwt.io for manual inspection and Burp Suite's JWT extension for automated testing.
Common Misconceptions
- JWTs are always secure: They're only as secure as their implementation.
- Tokens don't need expiration: Tokens should have a lifespan to mitigate risks.
- 'None' algorithm is safe: This can lead to easy token forgery and should be avoided.