Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

feat: Add an opt-in known_crit check for critical headers - #824

Open
anakinj wants to merge 3 commits into
jwt:mainfrom
anakinj:feat/known-crit
Open

anakinj wants to merge 3 commits into
jwt:mainfrom
anakinj:feat/known-crit

Conversation

@anakinj

@anakinj anakinj commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Description

RFC 7515 (section 4.1.11) says a token must be rejected if its crit header lists an extension the recipient doesn't understand. ruby-jwt doesn't check this, and which extensions an application understands is up to the application, so this adds an opt-in known_crit option:

token = JWT.encode({ sub: 'a' }, key, 'HS256', { crit: ['urn:example:must-understand'], 'urn:example:must-understand' => true })

JWT.decode(token, key, true, algorithm: 'HS256')                                              # => accepted, as before
JWT.decode(token, key, true, algorithm: 'HS256', known_crit: [])                              # raises JWT::InvalidCritError
JWT.decode(token, key, true, algorithm: 'HS256', known_crit: ['urn:example:must-understand']) # => accepted

encoded_token.verify_claims!(:exp, known_crit: ['urn:example:must-understand'])

When known_crit is given, a token is rejected if crit lists anything other than those extensions and b64, which the gem supports itself. A crit that isn't an array of strings is rejected too, including null. A token without crit passes. Without the option, nothing changes.

  • The check: JWT::Claims::KnownCrit, a new class next to JWT::Claims::Crit. It is registered as the known_crit claim verifier, so verify_claims!, claim_errors and valid_claims? on EncodedToken support it.
  • JWT.decode: runs the check after the signature has verified and before the payload claims. The option can be given with a symbol or string key, like the other decode options. It isn't checked when verify is false.
  • README: a "Critical header parameters" section.

Closes #815

Checklist

Before the PR can be merged be sure the following are checked:

  • The pull request title follows Conventional Commits, for example fix: Reject tokens with too many segments
  • There are tests for the fix or feature added/changed

Posted by Claude on behalf of @anakinj.

RFC 7515 says to reject a token whose crit header lists an extension
the recipient doesn't understand. Which extensions an application
understands is up to the application, so the check only runs when
known_crit is given:

  JWT.decode(token, key, true, algorithm: 'HS256', known_crit: [])
  encoded_token.verify_claims!(:exp, known_crit: ['urn:example:ext'])

A token is then rejected with JWT::InvalidCritError if crit lists
anything other than the given extensions and b64, which the gem
supports itself. Without the option nothing changes.
…alid

nil and false entries got past the unknown extension check, because it
tested the remaining entries for truthiness. crit now has to be an
array of strings, so [nil], [false], [1] and nested arrays are
rejected with 'Crit header should be an array of strings'. A crit
header that is present but null is rejected the same way, and an
unknown extension listed twice is named once.

The README says that only unknown extensions depend on known_crit, as
b64: false payloads always require crit: ['b64'], and a spec checks
that crit is checked before the payload claims.
@anakinj
anakinj marked this pull request as ready for review November 29, 2026 11:07

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an opt-in known_crit check for critical headers

1 participant