Raise JWT::DecodeError for a malformed x5c header#740
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
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, 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 the JWT::DecodeError callers expect from bad token input. Guard the input the same way key_finder.rb already guards the kid header: validate it's a non-empty array of strings or certificates and turn an OpenSSL parse failure into JWT::DecodeError. Adds specs driving the real from() path with the malformed shapes. Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.)