Skip to content

Commit 5fde08a

Browse files
committed
fix: normalize recursive JWT payload errors
1 parent 171062d commit 5fde08a

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎jwt/api_jwt.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _decode_payload(self, decoded: dict[str, Any]) -> dict[str, Any]:
296296
"""
297297
try:
298298
payload: dict[str, Any] = json.loads(decoded["payload"])
299-
except ValueError as e:
299+
except (ValueError, RecursionError) as e:
300300
raise DecodeError(f"Invalid payload string: {e}") from e
301301
if not isinstance(payload, dict):
302302
raise DecodeError("Invalid payload string: must be a json object")

‎tests/test_api_jwt.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pytest
99

10+
import jwt as pyjwt
1011
from jwt.types import Options
1112
from jwt.api_jwk import PyJWK
1213
from jwt.api_jwt import PyJWT
@@ -21,7 +22,7 @@
2122
InvalidSubjectError,
2223
MissingRequiredClaimError,
2324
)
24-
from jwt.utils import base64url_decode
25+
from jwt.utils import base64url_decode, base64url_encode
2526
from jwt.warnings import RemovedInPyjwt3Warning
2627

2728
from .utils import crypto_required, key_path, utc_timestamp
@@ -177,6 +178,21 @@ def test_decode_invalid_payload_string(self, jwt: PyJWT) -> None:
177178

178179
assert "Invalid payload string" in str(exc.value)
179180

181+
def test_decode_deeply_nested_payload_throws_decode_error(self) -> None:
182+
nested_payload = b"[" * 100_000 + b"]" * 100_000
183+
token = ".".join(
184+
(
185+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
186+
base64url_encode(nested_payload).decode(),
187+
"",
188+
)
189+
)
190+
191+
with pytest.raises(DecodeError, match="Invalid payload") as exc:
192+
pyjwt.decode(token, options={"verify_signature": False})
193+
194+
assert isinstance(exc.value.__cause__, RecursionError)
195+
180196
def test_decode_with_non_mapping_payload_throws_exception(self, jwt: PyJWT) -> None:
181197
secret = "secret"
182198
example_jwt = (

‎tests/test_jwks_client.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
from jwt.jwks_client import _NoRedirectHandler
2121
from jwt.api_jwk import PyJWK, PyJWKSet
2222
from jwt.exceptions import (
23+
DecodeError,
2324
PyJWKClientConnectionError,
2425
PyJWKClientError,
2526
PyJWKSetError,
2627
)
2728
from jwt.jwk_set_cache import JWKSetCache
29+
from jwt.utils import base64url_encode
2830

2931
from .utils import crypto_required
3032

@@ -325,6 +327,25 @@ def test_get_signing_key_from_jwt(self) -> None:
325327
"gty": "client-credentials",
326328
}
327329

330+
def test_get_signing_key_from_jwt_rejects_deeply_nested_payload(self) -> None:
331+
nested_payload = b"[" * 100_000 + b"]" * 100_000
332+
header = b'{"alg":"RS256","kid":"test-key"}'
333+
token = ".".join(
334+
(
335+
base64url_encode(header).decode(),
336+
base64url_encode(nested_payload).decode(),
337+
"",
338+
)
339+
)
340+
341+
with mocked_success_response(RESPONSE_DATA_WITH_MATCHING_KID) as open_mock:
342+
jwks_client = PyJWKClient("https://example.test/.well-known/jwks.json")
343+
with pytest.raises(DecodeError, match="Invalid payload") as exc:
344+
jwks_client.get_signing_key_from_jwt(token)
345+
346+
assert isinstance(exc.value.__cause__, RecursionError)
347+
assert open_mock.call_count == 0
348+
328349
def test_get_jwk_set_caches_result(self) -> None:
329350
url = "https://dev-87evx9ru.auth0.com/.well-known/jwks.json"
330351

0 commit comments

Comments
 (0)