Repository navigation
Raise JWT::DecodeError for a malformed x5c header - #740
Conversation
anakinj
left a comment
There was a problem hiding this comment.
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:
- RuboCop: this is the one failing check.
lib/jwt/x5c_key_finder.rb:43needs the modifier form (Style/IfUnlessModifier), andbundle exec rubocop -awill fix it. Please rebase onmainas well. - Error class: 3.3.0 reorganized the errors, and
JWT::DecodeErroris now deprecated as something to raise directly. Since these are structurally invalid tokens, please raiseJWT::MalformedTokenErrorin all three places. Existingrescue JWT::DecodeErrorcode still catches it, as it's a subclass. Please also tighten the spec to expect the new class. - Changelog: please add a row under Fixes and enhancements in
CHANGELOG.md, using the format inCONTRIBUTING.md. - Specs: the new
contextsits after the private-style helper methods at the bottom ofspec/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 throughJWT.decodewith thex5coption, 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>
d701677 to
7f8f007
Compare
|
@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. |
The
x5cverification path takes the header value straight off the token and hands it toparse_certificates, which calls.all?on it and maps every element throughOpenSSL::X509::Certificate.new. When the header isn't the array of base64 strings the code assumes, the caller gets a raw exception instead of aJWT::DecodeError:nilraisesNoMethodError(the value has no.all?)OpenSSL::X509::CertificateErrorBoth escape before any signature check runs, so code that rescues
JWT::DecodeErrorfor bad token input (which is theJWT.decodecontract) sees an unexpected error type instead.key_finder.rbalready guards thekidheader this same way, so this just bringsx5cin 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 realfrom()path with each malformed shape and assertJWT::DecodeError.bundle exec rspec spec/jwt/x5c_key_finder_spec.rbis green. (Two ECDSA specs fail on my local OpenSSL build before and after this change, so they're unrelated.)