Get a Pentest and security assessment of your IT network.

Cyber Security

JWT Lowercase Characters

TL;DR

Yes, a JWT (JSON Web Token) can be all lowercase, but it’s generally not recommended. While the specification doesn’t enforce case sensitivity for the token string itself, libraries and systems often treat them as case-sensitive. This can lead to unexpected validation failures.

Solution Guide

  1. Understanding JWT Structure: A JWT consists of three parts separated by dots (.):
    • Header: Contains metadata about the token (e.g., signing algorithm).
    • Payload: Contains claims – information about the user or entity.
    • Signature: Verifies the integrity of the header and payload.
  2. Case Sensitivity in JWT Components:
    • The header is case-sensitive, particularly the algorithm used for signing (e.g., ‘HS256’ vs ‘hs256’). Using incorrect casing here will cause validation to fail.
    • The payload itself generally isn’t case sensitive as JSON keys are typically treated as such by parsers. However, if you rely on specific string comparisons within the payload, casing matters.
    • The signature is a cryptographic hash and therefore not directly affected by casing in the header or payload.
  3. Token String Case Sensitivity: The entire JWT string (header.payload.signature) is often treated as case-sensitive by libraries.
    • Many validation functions compare the incoming token string exactly with a stored version. If the casing differs, it will be considered invalid.
  4. Potential Issues:
    • Validation Failures: The most common problem. A token generated as ‘eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…’ might fail validation if the system expects ‘eyJALgciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…’.
    • Inconsistent Behaviour: Different libraries or systems may handle casing differently, leading to unpredictable results.
  5. Best Practices:
    • Maintain Consistency: Always generate and validate tokens using the same casing convention. The most common practice is to use camelCase for header keys (e.g., ‘alg’, ‘typ’).
    • Avoid Lowercase-Only Tokens: While technically possible, it’s best to avoid generating JWTs that are entirely lowercase or uppercase. Stick to a consistent case style.
    • Test Thoroughly: If you must handle tokens with varying casing, test your validation logic extensively across different libraries and systems.
  6. Example (Python using PyJWT): This demonstrates generating a token.
    import jwt
    
    encoded_jwt = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
    print(encoded_jwt) # Example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
  7. Example (Node.js using jsonwebtoken): This demonstrates generating a token.
    const jwt = require('jsonwebtoken');
    
    token = jwt.sign({ data: 'payload' }, 'secret', { algorithm: 'HS256' });
    console.log(token); // Example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation