Overview
Audits JWT implementations for common and critical vulnerabilities. Covers JWT structure review (header alg, "none" attack, RS256 vs HS256), signature validation, expiry and clock skew handling, sensitive data in payload, token storage (httpOnly cookies vs localStorage), refresh token security, algorithm confusion attacks, and a comprehensive JWT audit checklist with code-level indicators and fixes.
When to Use This Skill
- Reviewing or implementing authentication that uses JWTs.
- Auditing an existing auth system (especially custom JWT code).
- The user mentions "JWT", "JSON Web Token", "access token", or is building login/refresh flows.
Prerequisites
- The JWT implementation code (issuance, validation, refresh).
- Example tokens (valid, expired, malformed).
- Knowledge of how tokens are stored and transmitted on the client.
Steps
Decode and inspect sample tokens (use jwt.io or jwt decode):
- Header:
alg, typ, kid.
- Payload: claims (
sub, exp, iat, aud, iss, scopes/roles, any PII?).
- Signature present and looks valid.
Algorithm & signature validation audit:
- Never allow
alg: "none".
- For asymmetric (RS256/ES256): verify with public key, never secret.
- For symmetric (HS256): use a strong, long, random secret; never hardcode.
- Check that the library is actually verifying the signature (many footguns in older or custom code).
- Algorithm confusion (HS256 with public key as secret) — test if the implementation is vulnerable.
Claims validation:
exp is checked and enforced (with small clock skew tolerance, e.g., 30-60s).
aud and iss are validated against expected values.
iat / nbf considered where relevant.
- No critical business logic decisions based solely on unverified claims.
Token storage & transmission:
- httpOnly + Secure + SameSite cookies (strongly preferred for web).
- Never localStorage or sessionStorage for access or refresh tokens (XSS risk).
- Short-lived access tokens (5-15 min) + rotating refresh tokens.
- Refresh tokens stored securely (httpOnly cookie or secure backend session) and rotated on use.
Refresh & revocation:
- Refresh token rotation (old refresh token is invalidated when a new one is issued).
- Refresh token reuse detection (if the same old token is presented again → revoke the whole family and force re-login).
- Ability to revoke individual sessions or all sessions for a user (e.g., on password change or suspected compromise).
Output:
- JWT audit checklist (with pass/fail + evidence + severity).
- Code-level red flags to search for ("alg", "verify", "localStorage.setItem", hard-coded secrets, long-lived tokens, etc.).
- Recommended secure patterns (httpOnly cookie issuance, refresh rotation, library usage).
- Test cases (none alg, alg confusion, expired token, missing signature, etc.).
- Remediation steps for each finding.
Examples
A complete JWT audit report for a typical custom JWT implementation (using jsonwebtoken in Node) with several issues (long-lived tokens, localStorage, missing audience check, no refresh rotation, potential alg confusion due to custom verification) plus the exact fixes and test vectors is included.
Edge Cases & Error Handling
- Clock skew between services: Allow small
exp leeway but not too much.
- Key rotation (kid): Support multiple public keys during rotation; never use the same key forever.
- Token binding (advanced): Consider DPoP or mTLS-bound tokens for high-security scenarios.
Verification
- Run the audit checklist against the implementation — findings are documented with evidence.
- Test vectors (none alg, expired, wrong audience, alg confusion) are run against the validation code — vulnerable paths are identified.
- After remediation, the same test vectors are rejected.
- Token storage is verified in browser (httpOnly cookie, no tokens in localStorage).
- Success: The JWT implementation follows current best practices and resists the common attacks in the checklist.
References
1---2name: jwt-security-auditor3description: Audits JWT implementation for common security vulnerabilities. Use when reviewing authentication systems that use JSON Web Tokens.4license: Apache-2.05---67## Overview89Audits JWT implementations for common and critical vulnerabilities. Covers JWT structure review (header alg, "none" attack, RS256 vs HS256), signature validation, expiry and clock skew handling, sensitive data in payload, token storage (httpOnly cookies vs localStorage), refresh token security, algorithm confusion attacks, and a comprehensive JWT audit checklist with code-level indicators and fixes.1011## When to Use This Skill1213- Reviewing or implementing authentication that uses JWTs.14- Auditing an existing auth system (especially custom JWT code).15- The user mentions "JWT", "JSON Web Token", "access token", or is building login/refresh flows.1617## Prerequisites1819- The JWT implementation code (issuance, validation, refresh).20- Example tokens (valid, expired, malformed).21- Knowledge of how tokens are stored and transmitted on the client.2223## Steps24251. **Decode and inspect sample tokens** (use jwt.io or `jwt decode`):26 - Header: `alg`, `typ`, `kid`.27 - Payload: claims (`sub`, `exp`, `iat`, `aud`, `iss`, scopes/roles, any PII?).28 - Signature present and looks valid.29302. **Algorithm & signature validation audit**:31 - Never allow `alg: "none"`.32 - For asymmetric (RS256/ES256): verify with public key, never secret.33 - For symmetric (HS256): use a strong, long, random secret; never hardcode.34 - Check that the library is actually verifying the signature (many footguns in older or custom code).35 - Algorithm confusion (HS256 with public key as secret) — test if the implementation is vulnerable.36373. **Claims validation**:38 - `exp` is checked and enforced (with small clock skew tolerance, e.g., 30-60s).39 - `aud` and `iss` are validated against expected values.40 - `iat` / `nbf` considered where relevant.41 - No critical business logic decisions based solely on unverified claims.42434. **Token storage & transmission**:44 - httpOnly + Secure + SameSite cookies (strongly preferred for web).45 - Never localStorage or sessionStorage for access or refresh tokens (XSS risk).46 - Short-lived access tokens (5-15 min) + rotating refresh tokens.47 - Refresh tokens stored securely (httpOnly cookie or secure backend session) and rotated on use.48495. **Refresh & revocation**:50 - Refresh token rotation (old refresh token is invalidated when a new one is issued).51 - Refresh token reuse detection (if the same old token is presented again → revoke the whole family and force re-login).52 - Ability to revoke individual sessions or all sessions for a user (e.g., on password change or suspected compromise).53546. **Output**:55 - JWT audit checklist (with pass/fail + evidence + severity).56 - Code-level red flags to search for ("alg", "verify", "localStorage.setItem", hard-coded secrets, long-lived tokens, etc.).57 - Recommended secure patterns (httpOnly cookie issuance, refresh rotation, library usage).58 - Test cases (none alg, alg confusion, expired token, missing signature, etc.).59 - Remediation steps for each finding.6061## Examples6263A complete JWT audit report for a typical custom JWT implementation (using `jsonwebtoken` in Node) with several issues (long-lived tokens, localStorage, missing audience check, no refresh rotation, potential alg confusion due to custom verification) plus the exact fixes and test vectors is included.6465## Edge Cases & Error Handling6667- **Clock skew between services**: Allow small `exp` leeway but not too much.68- **Key rotation (kid)**: Support multiple public keys during rotation; never use the same key forever.69- **Token binding** (advanced): Consider DPoP or mTLS-bound tokens for high-security scenarios.7071## Verification72731. Run the audit checklist against the implementation — findings are documented with evidence.742. Test vectors (none alg, expired, wrong audience, alg confusion) are run against the validation code — vulnerable paths are identified.753. After remediation, the same test vectors are rejected.764. Token storage is verified in browser (httpOnly cookie, no tokens in localStorage).775. Success: The JWT implementation follows current best practices and resists the common attacks in the checklist.7879## References8081- [JWT.io](https://jwt.io/)82- [OWASP JWT Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_for_Java_Cheat_Sheet.html)83- [RFC 7519 - JSON Web Token (JWT)](https://datatracker.ietf.org/doc/html/rfc7519)84- [Auth0 / Okta JWT Best Practices](https://auth0.com/blog/a-look-at-the-latest-draft-for-jwt-bcp/)85- [Refresh Token Rotation](https://auth0.com/docs/secure/tokens/refresh-tokens/refresh-token-rotation)