Skip to content

Keep JWT::JWK#inspect from building the key - #796

Closed
anakinj wants to merge 1 commit into
jwt:mainfrom
anakinj:fix-jwk-inspect-bad-key
Closed

anakinj wants to merge 1 commit into
jwt:mainfrom
anakinj:fix-jwk-inspect-bad-key

Conversation

@anakinj

@anakinj anakinj commented Nov 24, 2026

Copy link
Copy Markdown
Member

Description

#754 made JWT::JWK::KeyBase#inspect show export instead of the key's internals. But export calls private?, which builds the OpenSSL key, and a JWK with a kid isn't built when it's imported. So a JWK with bad key material imported fine and then raised from inspect:

JWT::JWK.import(valid_p256_export.merge(y: valid_p256_export[:x])).inspect
# => JWT::JWKError: Invalid EC point for curve P-256
JWT::JWK.import(kty: 'RSA', n: '!!!', e: 'AQAB', kid: 'a').inspect
# => JWT::Base64DecodeError: Invalid base64 encoding

JWT::JWK::Set#inspect raised too if any member was bad. On 3.3.0 these returned a string. This broke irb, logging and RSpec failure output for exactly the keys someone would be debugging.

inspect now takes the parameters without the private key members, the same members export drops, so it never builds the key. For valid keys the output is unchanged. export still raises for such keys, as #789 intends.

#754 hasn't been released, so there's no changelog row.

Checklist

  • There are tests for the fix or feature added/changed. Both new examples fail without the fix.
  • Specs (740 examples, 0 failures) and RuboCop pass locally.

Posted by Claude on behalf of @anakinj.

JWT::JWK::KeyBase#inspect called #export, and #export calls #private?,
which builds the OpenSSL key. A JWK with a kid is not built when it is
imported, so one with bad key material, such as an EC point that is not
on the curve or an RSA modulus that is not base64, imported fine and
then raised from #inspect. That broke irb, logging and RSpec failure
output, and JWT::JWK::Set#inspect with it, for exactly the keys someone
would be debugging. Before jwt#754 it returned a string.

#inspect now takes the parameters without the private key members, the
same members #export drops, without building the key. #export still
raises for such keys, as jwt#789 intends.
@anakinj

anakinj commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Closing: private_key_elements would have to be defined by every JWT::JWK::KeyBase subclass, including third-party ones, so inspect would raise NoMethodError for them. Leaving inspect as it is for 3.3.1.

Posted by Claude on behalf of @anakinj.

@anakinj anakinj closed this Nov 24, 2026
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.

1 participant