Skip to content

Raise JWT::DecodeError for a malformed x5c header - #740

Merged
anakinj merged 1 commit into
jwt:mainfrom
arpitjain099:fix/x5c-header-type-guard
Nov 24, 2026
Merged

anakinj merged 1 commit into
jwt:mainfrom
arpitjain099:fix/x5c-header-type-guard

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

The x5c verification path takes the header value straight off the token and hands it to parse_certificates, which calls .all? on it and maps every element through OpenSSL::X509::Certificate.new. When the header isn't the array of base64 strings the code assumes, the caller gets a raw exception instead of a JWT::DecodeError:

  • a bare string, a number, or nil raises NoMethodError (the value has no .all?)
  • an empty array, or an array holding a non-certificate, raises OpenSSL::X509::CertificateError

Both escape before any signature check runs, so code that rescues JWT::DecodeError for bad token input (which is the JWT.decode contract) sees an unexpected error type instead. key_finder.rb already guards the kid header this same way, so this just brings x5c in line with it.

The patch validates that the value is a non-empty array of strings or certificates and turns an OpenSSL parse failure into JWT::DecodeError. The added specs drive the real from() path with each malformed shape and assert JWT::DecodeError.

bundle exec rspec spec/jwt/x5c_key_finder_spec.rb is green. (Two ECDSA specs fail on my local OpenSSL build before and after this change, so they're unrelated.)

@anakinj anakinj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and sorry it sat for so long! The fix does what it says. I applied it to current main and ran malformed x5c headers through JWT.decode: missing, 1, "abc", {}, [], [1], [null], a non-certificate string, and a valid certificate mixed with junk. All of them raise a raw Ruby error on main and a JWT error with this PR, and a valid certificate with a bad signature still gets JWT::VerificationError. The full spec suite passes too.

A few small things before merging:

  1. RuboCop: this is the one failing check. lib/jwt/x5c_key_finder.rb:43 needs the modifier form (Style/IfUnlessModifier), and bundle exec rubocop -a will fix it. Please rebase on main as well.
  2. Error class: 3.3.0 reorganized the errors, and JWT::DecodeError is now deprecated as something to raise directly. Since these are structurally invalid tokens, please raise JWT::MalformedTokenError in all three places. Existing rescue JWT::DecodeError code still catches it, as it's a subclass. Please also tighten the spec to expect the new class.
  3. Changelog: please add a row under Fixes and enhancements in CHANGELOG.md, using the format in CONTRIBUTING.md.
  4. Specs: the new context sits after the private-style helper methods at the bottom of spec/jwt/x5c_key_finder_spec.rb, with no blank line before it. Please move it up next to the other examples. It would also be good to add a hash and a mix of a valid certificate with a non-string to the list, plus one example through JWT.decode with the x5c option, since that's how untrusted tokens reach this code.

Let me know if you'd rather I push these changes to your branch, and I'll happily do that and keep you as the author.

Posted by Claude on behalf of @anakinj.

X5cKeyFinder#from passes the raw x5c header straight into
parse_certificates, which calls .all? on it and maps each element
through OpenSSL::X509::Certificate.new. If a token's x5c header is not
an array of base64 strings (a bare string, a number, nil, a hash, an
empty array, or an array holding a non-certificate), the finder raises
an uncaught NoMethodError or OpenSSL::X509::CertificateError before any
signature check, instead of a JWT error callers expect from bad token
input.

Guard the input the same way key_finder.rb already guards the kid
header: validate it is a non-empty array of strings or certificates and
turn an OpenSSL parse failure into JWT::MalformedTokenError, the current
class for structurally invalid tokens. It is a JWT::DecodeError
subclass, so existing rescues keep working. Adds specs driving the real
from() path and JWT.decode with the malformed shapes.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
@arpitjain099
arpitjain099 force-pushed the fix/x5c-header-type-guard branch from d701677 to 7f8f007 Compare November 23, 2026 00:52
@arpitjain099

Copy link
Copy Markdown
Contributor Author

@anakinj thanks for the careful review. All four are done: RuboCop passes with the modifier form, the three raises now use JWT::MalformedTokenError with the spec tightened to match, a CHANGELOG row added under Fixes and enhancements, and the malformed context moved up with a hash, a cert-plus-non-string, and a JWT.decode example. Rebased on main. Please take another look when convenient.

@anakinj
anakinj merged commit 02c8410 into jwt:main Nov 24, 2026
15 checks passed
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.

2 participants