Skip to content

fix: raise JWT::ClaimValidationError for a payload that is not a JSON object - #794

Open
orien wants to merge 1 commit into
jwt:mainfrom
orien:claim-errors-for-non-numeric-exp-nbf-and-non-object-payload
Open

orien wants to merge 1 commit into
jwt:mainfrom
orien:claim-errors-for-non-numeric-exp-nbf-and-non-object-payload

Conversation

@orien

@orien orien commented Nov 20, 2026 •

Copy link
Copy Markdown

Description

JWT.decode lets plain Ruby errors escape for a correctly signed token whose payload is a JSON array, number, string or null when verify_iss, verify_aud, verify_sub or verify_jti is on. JWT::Claims::Issuer, JWT::Claims::Audience, JWT::Claims::Subject and JWT::Claims::JwtId index the payload directly and raise TypeError or NoMethodError. Neither is a JWT::Error, so a caller that rescues JWT::DecodeError to reject the token gets an unhandled error instead.

This PR has those four verifiers treat a payload that is not a Hash as the claim being absent, so each raises its own JWT::ClaimValidationError (Invalid issuer ... received <none>, Missing jti, and so on). This follows JWT::Claims::Required, which already handles non-Hash payloads this way. A jti validator proc is still called, with a nil jti. Encoding array and string payloads remains supported, and such tokens still decode when no verified claim needs a JSON object.

The non-numeric exp/nbf half of #793 shipped in 3.3.1 as GHSA-gp6v-qjfx-hrxc, so this PR no longer touches it. There is no CHANGELOG row: main has no section for the next version, and #800 moves the changelog to release-please, which generates it from PR titles.

Fixes #793

Checklist

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

  • There are tests for the fix or feature added/changed
  • A description of the changes and a reference to the PR has been added to CHANGELOG.md. More details in the CONTRIBUTING.md

@anakinj

anakinj commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for this, and sorry for the wait. Part of it overlapped with a security fix that was under embargo until today, so I couldn't comment earlier.

The exp/nbf half is now on main and shipped in 3.3.1, as part of GHSA-gp6v-qjfx-hrxc. A non-numeric nbf such as "never" was coerced to 0 by to_i, so the Not Before check passed. JWT::Claims::Expiration and JWT::Claims::NotBefore now raise JWT::InvalidPayload unless the claim is a finite Numeric. Like your change, that rejects numeric strings, and it also covers Float::INFINITY. It covers the Boolean, Array and Hash cases from #793 too.

So this PR will now conflict in expiration.rb, not_before.rb and their specs. Could you rebase it on main and drop the exp/nbf changes? The other half isn't fixed anywhere yet, and I'd like to merge it. That's Issuer, Audience, Subject and JwtId treating a payload that isn't a JSON object as the claim being absent, plus the jwt_spec.rb/encoded_token_spec.rb examples for it. Please also update the CHANGELOG row to describe only that part, under the new v3.3.2 section once #798 is merged.

If you'd rather I push the rebase to your branch, let me know and I'll do that and keep you as the author.

Posted by Claude on behalf of @anakinj.

The iss, aud, sub and jti verifiers indexed the payload directly, so a
correctly signed token whose payload is a JSON array, number, string or
null raised TypeError or NoMethodError from JWT.decode. Neither is a
JWT::Error, so callers rescuing JWT::DecodeError did not catch them.

Treat a payload that is not a Hash as the claim being absent, so each
verifier raises its own ClaimValidationError, as Claims::Required
already does. Payloads that are not JSON objects still decode when no
verified claim needs one.

Fixes jwt#793
@orien
orien force-pushed the claim-errors-for-non-numeric-exp-nbf-and-non-object-payload branch from d245f9c to 15fece0 Compare November 24, 2026 18:22
@orien orien changed the title Raise JWT::ClaimValidationError for a non-numeric exp/nbf or a payload that is not a JSON object fix: raise the claim's own JWT::ClaimValidationError for a payload that is not a JSON object Nov 24, 2026
@orien orien changed the title fix: raise the claim's own JWT::ClaimValidationError for a payload that is not a JSON object fix: raise JWT::ClaimValidationError for a payload that is not a JSON object Nov 24, 2026
@orien

orien commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks @anakinj, so rebased. 👍

@orien

orien commented Nov 24, 2026

Copy link
Copy Markdown
Author

FYI: I removed the changelog entry from the pr, as it looks like you'll use release-please in future.

@anakinj

anakinj commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for your attention. Im trying to lighten the manual steps in the release yes so squeezing in changes to that at some point. Lets see when i have time to continue:)

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.

Plain Ruby error from JWT.decode for a non-numeric exp/nbf or a payload that is not a JSON object

2 participants