Validate JSON Web Tokens by Trust Policy, Not by Decoding Claims

A JSON Web Token (JWT) is a compact format for carrying claims. Decoding its Base64URL segments only reveals data; it does not prove that the token was issued by a trusted identity provider or that its claims are appropriate for the current API. A secure verifier must validate the cryptographic operation and apply an application trust policy to the issuer, audience, time window, token type, and permissions.

JWT headers and claims are supplied with the token. Treat them as attacker-controlled until signature or authenticated-encryption validation succeeds. A library that returns a decoded payload is not necessarily a validation library, and a valid signature does not automatically mean every claim is suitable for every endpoint.

Fix the algorithm and key policy in server configuration

Figure: Validate JSON Web Tokens by Trust Policy, Not by Decoding Claims
Figure: Validate JSON Web Tokens by Trust Policy, Not by Decoding Claims

Configure the verifier with an explicit allowlist of algorithms accepted for that issuer and token type. Do not let the token's alg header choose the verification algorithm or select a different key family. Bind each verification key to one expected algorithm and reject unsigned tokens or unexpected algorithms. RFC 8725 warns about algorithm confusion and requires applications to constrain acceptable algorithms.

Select the key from a trusted issuer configuration, such as a verified JWKS endpoint associated with a preconfigured issuer. Do not fetch a key from an arbitrary jku, x5u, or other URL in the token header. Validate key identifiers against the issuer's trusted key set, and handle key rotation using a controlled cache and refresh process. A kid field is only a lookup hint, not a proof of trust.

For HMAC, every verifier that holds the shared secret can also mint tokens. This may be reasonable within a single service boundary, but it broadens the set of systems that can impersonate the issuer. Asymmetric signing can reduce that distribution problem, but it still requires correct issuer, audience, key, and algorithm checks. Choose a maintained library and keep private signing keys in a managed secret store or key service.

Validate claims against the endpoint's contract

After cryptographic validation, require the expected iss value and exact aud for the API or client. A token intended for a different service must not become valid just because it was signed by a trusted organization. Validate exp and nbf with a small, documented clock-skew allowance, and require iat or other temporal claims when the identity provider's profile requires them. Reject missing, malformed, or contradictory values rather than inventing defaults.

Do not use a broad scope or role claim without checking which operations the endpoint allows. Authorization should be performed for each requested resource and action. A valid token identifies an authenticated principal; it does not establish ownership of every object named in the URL. Keep issuer identity separate from user-controlled profile fields and ensure tenant claims are derived from an issuer policy you trust.

Token types should have mutually exclusive validation rules. An ID token intended for a browser client must not be accepted as an API access token. Use separate audiences, explicit token typing where supported, and separate code paths for distinct JWT profiles. If the application accepts both signed and encrypted JWTs, validate both operations and do not assume successful decryption authenticates the sender.

Understand revocation and token lifetime

Self-contained access tokens can remain valid until expiry unless the system checks revocation or account state elsewhere. Shorter lifetimes reduce exposure but increase renewal traffic and may affect offline clients. Refresh tokens should receive separate protection, rotation, reuse detection, and revocation behavior. Decide what happens when a user is disabled, a role is removed, or a signing key is compromised.

Do not place secrets or unnecessary personal data in a signed JWT. A JWS signature provides integrity, not confidentiality; anyone holding the token can read its claims. If confidentiality is required, use encryption with an authenticated issuer design and validate the inner token as well. Do not use JWT merely because it is a common format; opaque server-side sessions can be simpler where centralized revocation and lookup are acceptable.

Test hostile and confusing token cases

Create automated verifier tests for a valid token from the expected issuer, and for wrong algorithm, wrong key, missing signature, expired token, future nbf, wrong issuer, wrong audience, malformed claims, unknown kid, and a token from a different profile. Include a token whose header advertises a key URL and verify that the service does not make an outbound request to it. Test key rotation with both the current and a controlled retiring key.

Test API authorization separately from token acceptance. A valid token for user A must not read user B's record unless the policy explicitly permits it. Confirm that logging records the request ID and failure category without recording the whole bearer token. Error messages can be useful to the client without exposing signature details, key material, or internal stack traces.

Review checklist

  1. Use a maintained library configured with an explicit algorithm allowlist.
  2. Select keys only from trusted issuer configuration, never a token-provided URL.
  3. Validate signature, issuer, audience, time claims, token type, and profile.
  4. Keep authentication separate from resource authorization.
  5. Define expiration, refresh, revocation, and key-rotation behavior.
  6. Keep secrets and unnecessary personal data out of readable token claims.
  7. Add negative tests for algorithm confusion, wrong issuer/audience, and object access.

RFC 8725 documents JWT Best Current Practices. RFC 7519 defines the JWT claims format. OWASP's JWT Cheat Sheet covers application concerns such as token storage and revocation. The token profile and identity provider documentation remain important because an access token, ID token, and service assertion have different intended recipients.

References


Editorial status: Owner authorized publication without an individual reviewer. Token validation must follow the exact issuer profile in use.