Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fail when an unusable key occurs #762

Merged
merged 6 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to `Semantic Versioning <https://semver.org/>`__.

Changed
~~~~~~~
- Skip keys with incompatible alg when loading JWKSet by @DaGuich in https://github.com/jpadilla/pyjwt/pull/762

Fixed
~~~~~
Expand Down
9 changes: 8 additions & 1 deletion jwt/api_jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ def __init__(self, keys):
raise PyJWKSetError("The JWK Set did not contain any keys")

for key in keys:
self.keys.append(PyJWK(key))
try:
self.keys.append(PyJWK(key))
except PyJWKError:
# skip unusable keys
Copy link

@kamil-muszynski-90poe kamil-muszynski-90poe Sep 28, 2022

Choose a reason for hiding this comment

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

Hello,

I realize this is a bit late since this is merged already, but it produces a behavior like this:

  File "/usr/local/lib/python3.8/dist-packages/jwt/jwks_client.py", line 96, in get_signing_key_from_jwt
    return self.get_signing_key(header.get("kid"))
  File "/usr/local/lib/python3.8/dist-packages/jwt/jwks_client.py", line 78, in get_signing_key
    signing_keys = self.get_signing_keys()
  File "/usr/local/lib/python3.8/dist-packages/jwt/jwks_client.py", line 65, in get_signing_keys
    jwk_set = self.get_jwk_set(refresh)
  File "/usr/local/lib/python3.8/dist-packages/jwt/jwks_client.py", line 62, in get_jwk_set
    return PyJWKSet.from_dict(data)
  File "/usr/local/lib/python3.8/dist-packages/jwt/api_jwk.py", line 102, in from_dict
    return PyJWKSet(keys)
  File "/usr/local/lib/python3.8/dist-packages/jwt/api_jwk.py", line 97, in __init__
    raise PyJWKSetError("The JWK Set did not contain any usable keys")
jwt.exceptions.PyJWKSetError: The JWK Set did not contain any usable keys

when PyJWKError is raised from line 50. This is actually caused by installing pyjwt only, with no crypto alrogithms available, and can be fixed with pip install pyjwt[crypto], as suggested in the docs - but it took me a bit to figure this out.

Maybe it could be possible to at least log a warning that a key was skipped with an error message details?

continue

if len(self.keys) == 0:
raise PyJWKSetError("The JWK Set did not contain any usable keys")

@staticmethod
def from_dict(obj):
Expand Down
1 change: 1 addition & 0 deletions tests/keys/jwk_keyset_only_unknown_alg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"keys":[{"kid":"lYXxnemSzWNBUoPug_h0hZnjPi5oKCmQ9awQJaZCWWM","kty":"RSA","alg":"RSA-OAEP","use":"enc","n":"k75Ghd4r8h_fdydTAXyMjrGYNnuiG7yevoW1ZIIuegEUK3LLGY0Z3Q8PhCrkmi6LpkPwwR1C8ck9plvSs4vZ9GqmUoi5YcQEile6HjPG3NBwQ-cHWY4ZH_D-ItdzcZUKDxjHYaY-GW1yLeJ1RAh8wMPM7cenA2v0eNIq4HaIXzZJ2Hgxh4Ei-CSYcD0f_TYEySqUEb8jd0dC8frpkYDkOUCVizRBDUEg_hkPSpVqfLP8ekxIHxkC9wcfL-d2FhptxBQYN8NFnIuG9NFXbZ5mdzdmIuN6WPr_CECcgL9qXsph9U-L829dU67ufeBvzEejJ8qwiswslRdx4ZcYjtaBdQ","e":"AQAB","x5c":["MIICnTCCAYUCBgGAUN05KzANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQDDAdUZXN0aW5nMB4XDTIyMDQyMjEwNDAxN1oXDTMyMDQyMjEwNDE1N1owEjEQMA4GA1UEAwwHVGVzdGluZzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJO+RoXeK/If33cnUwF8jI6xmDZ7ohu8nr6FtWSCLnoBFCtyyxmNGd0PD4Qq5Joui6ZD8MEdQvHJPaZb0rOL2fRqplKIuWHEBIpXuh4zxtzQcEPnB1mOGR/w/iLXc3GVCg8Yx2GmPhltci3idUQIfMDDzO3HpwNr9HjSKuB2iF82Sdh4MYeBIvgkmHA9H/02BMkqlBG/I3dHQvH66ZGA5DlAlYs0QQ1BIP4ZD0qVanyz/HpMSB8ZAvcHHy/ndhYabcQUGDfDRZyLhvTRV22eZnc3ZiLjelj6/whAnIC/al7KYfVPi/NvXVOu7n3gb8xHoyfKsIrMLJUXceGXGI7WgXUCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAeMUFrCX4eAfF8i6wILOP5dDJOBN10nPP63VNliQ7+YHu1ZI0VGB7TNrImRE9riH2IWenSXD21DxK31qBlZKNEgaH7rVwwvOZ22qCyWacv1+QdanxAiljD03rU7HOR/tyqcvjl6U2Yadxcq6OWlKKVaa0fNtbPigqAwQ3iVpg9N+OthANYyKHxlmzJKGeEaDA69/uJ6UwektHlv/9BnNFh8We6EwJxYG7/rejI02EgbJFxGO1RlcmigTxRc5l3Dw4WldBIRxWiJgSEkKSfUy5S7sQdFQokZjTyqy6h1ldb/tgrWLIE0srGQ2u/fQeSgPTbAzihaeOf+WKq5RDXoq5bw=="],"x5t":"FaWinuPZQiDMljn3x9DMAuepBYQ","x5t#S256":"_0B--Hh1KgNtdyZqAp1NWUAikRPvlt2HGm__xXpjTi0"}]}
1 change: 1 addition & 0 deletions tests/keys/jwk_keyset_with_unknown_alg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"keys":[{"kid":"U1MayerhVuRj8xtFR8hyMH9lCfVMKlb3TG7mbQAS19M","kty":"RSA","alg":"RS256","use":"sig","n":"omef3NkXf4--6BtUPKjhlV7pf6Vv7HMg-VL-ITX8KQZTD4LTzWO3x9RPwVepKjgfvJe_IiZFaJX78-a7zpcG9mpZG8czp3C8nZSvAJKphvYLd9s9qYrGMFW9t1eHyGwmIQN02VXwHeZ0JDd5X4i7s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rMLJUXceGXGI7WgXUCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAeMUFrCX4eAfF8i6wILOP5dDJOBN10nPP63VNliQ7+YHu1ZI0VGB7TNrImRE9riH2IWenSXD21DxK31qBlZKNEgaH7rVwwvOZ22qCyWacv1+QdanxAiljD03rU7HOR/tyqcvjl6U2Yadxcq6OWlKKVaa0fNtbPigqAwQ3iVpg9N+OthANYyKHxlmzJKGeEaDA69/uJ6UwektHlv/9BnNFh8We6EwJxYG7/rejI02EgbJFxGO1RlcmigTxRc5l3Dw4WldBIRxWiJgSEkKSfUy5S7sQdFQokZjTyqy6h1ldb/tgrWLIE0srGQ2u/fQeSgPTbAzihaeOf+WKq5RDXoq5bw=="],"x5t":"FaWinuPZQiDMljn3x9DMAuepBYQ","x5t#S256":"_0B--Hh1KgNtdyZqAp1NWUAikRPvlt2HGm__xXpjTi0"}]}
22 changes: 20 additions & 2 deletions tests/test_api_jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jwt.algorithms import has_crypto
from jwt.api_jwk import PyJWK, PyJWKSet
from jwt.exceptions import InvalidKeyError, PyJWKError
from jwt.exceptions import InvalidKeyError, PyJWKError, PyJWKSetError

from .utils import crypto_required, key_path

Expand Down Expand Up @@ -274,4 +274,22 @@ def test_keyset_should_index_by_kid(self):
assert jwk == jwk_set["keyid-abc123"]

with pytest.raises(KeyError):
jwk_set["this-kid-does-not-exist"]
_ = jwk_set["this-kid-does-not-exist"]

@crypto_required
def test_keyset_with_unknown_alg(self):
# first keyset with unusable key and usable key
with open(key_path("jwk_keyset_with_unknown_alg.json")) as keyfile:
jwks_text = keyfile.read()
jwks = json.loads(jwks_text)
assert len(jwks.get("keys")) == 2
keyset = PyJWKSet.from_json(jwks_text)
assert len(keyset.keys) == 1

# second keyset with only unusable key -> catch exception
with open(key_path("jwk_keyset_only_unknown_alg.json")) as keyfile:
jwks_text = keyfile.read()
jwks = json.loads(jwks_text)
assert len(jwks.get("keys")) == 1
with pytest.raises(PyJWKSetError):
_ = PyJWKSet.from_json(jwks_text)