Repository navigation
fix: ignore non-RSA keys when parsing the JWKS in the token verifier - #917
Open
frederikprijck wants to merge 2 commits into
Open
frederikprijck wants to merge 2 commits into
frederikprijck wants to merge 2 commits into
Conversation
JwksFetcher._parse_jwks converted every key in the JWKS with the RSA key parser. A JWKS that also contained a key of another type (for example EC or OKP) made PyJWT raise "Not an RSA key", which made the synchronous verifier fail for every token, including RS256 tokens signed with a valid RSA key in the same set. The async verifier swallowed the error and reported "RSA Public Key with ID ... was not found" for every key id. Only keys with kty "RSA" are parsed now, other keys are skipped. Requesting a key id that belongs to a skipped key still fails with the existing "was not found" error. Co-Authored-By: Claude <noreply@anthropic.com>
Add a key without kty, a key with a lowercase kty (kty is case-sensitive) and a JWKS without any RSA key to the verifier tests, for both the sync and the async fetcher. Move the cryptography import to the top of the test module and put the async_token_verifier import in sorted order. The _parse_jwks docstring now says RSA signatures, as the verifier accepts any RSA algorithm. Co-Authored-By: Claude <noreply@anthropic.com>
This branch has not been deployed
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.
Changes
JwksFetcher._parse_jwksconverted every key in the JWKS with the RSA key parser. If the JWKS also contained a key of another type (for exampleEC), PyJWT raisedInvalidKeyError: Not an RSA keyand the whole set was unusable:AsymmetricSignatureVerifier/TokenVerifierfailed for every token, including RS256 tokens signed with a valid RSA key that is in the same set. The error was a PyJWTInvalidKeyError, not the SDK'sTokenValidationError.AsyncJwksFetcherswallows errors with a bareexcept, so it reportedRSA Public Key with ID "..." was not found.for every key id, which hides the actual cause.Only keys with
ktyRSAare parsed now, any other key is skipped (ktyis case-sensitive, RFC 7517 section 4.1, so"rsa"is skipped too). Requesting a key id that belongs to a skipped key fails with the existingTokenValidationError(... was not found), also when no RSA key is left.Observable differences, all for JWKS documents that contain a key that is not an RSA key:
InvalidKeyError. It verifies tokens signed with an RSA key from the set, and raisesTokenValidationErrorfor a key id that is not an RSA key.Nothing changes for JWKS documents that only contain RSA keys.
RFC 7517 section 5 says that implementations should ignore JWKs in a JWK Set that use
ktyvalues they do not understand, so a verifier that only supports RSA keys should not be affected by the other keys a JWKS publishes.src/auth0/authentication/token_verifier.py:JwksFetcher._parse_jwksskips keys whosektyis notRSA(AsyncJwksFetchershares this code path).Alternative considered: catching
InvalidKeyErrorper key. Filtering onktywas preferred as it is explicit, and a malformed RSA key still surfaces as an error.References
Testing
Seven tests were added to
tests/authentication/test_token_verifier.py:JwksFetcherandAsyncJwksFetcherreturn the RSA key from a JWKS that also contains keys that are not RSA keys (an unknown type,EC, a key withoutktyand one with a lowercasekty).TokenValidationError(was not found).TokenValidationErrorand is fetched once.TokenVerifier.verifyaccepts an RS256 token signed with a freshly generated RSA key when the JWKS also contains other key types.Six of the seven fail on
master: four withInvalidKeyError: Not an RSA key, and two (async) because the error is swallowed. The seventh, the async "was not found" test, passes onmasterand guards behavior that is preserved.The
tests/authenticationsuite passes (119 tests), with PyJWT 2.12.1 (the lockfile) and with the latest, 2.15.1, on Python 3.12. Tests outsidetests/authenticationneed a Docker daemon for WireMock and were not run.Checklist
🤖 Generated with Claude Code