fix: reject tokens missing 'aud' claim when audience is expected#412
Open
gaoflow wants to merge 1 commit into
Open
fix: reject tokens missing 'aud' claim when audience is expected#412gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When jwt.decode() is called with an audience= argument, tokens that do not carry an 'aud' claim were silently accepted. This enables cross- service token reuse: a valid token issued without an audience claim could be replayed against any service that specifies an expected audience. The fix activates the commented-out guard in _validate_aud: if the caller provides a non-None audience and the token has no 'aud' claim, raise JWTClaimsError instead of returning. The existing require_aud=True option remains the way to enforce the claim when audience= is not given. Tests: rename test_aud_empty_claim to two explicit cases (no-audience passes, audience-given raises), and fix test_require to not pass an audience value for non-aud required-claim tests. Fixes mpdavis#407
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.
Problem
jwt.decode(token, key, audience="my-service")silently accepts tokens that carry noaudclaim at all. An attacker who holds a valid token from any service that omitsaudcan replay it against a service that checks audience, because the missing-claim path simply returns without raising.Minimal reproduction:
PyJWT raises
MissingRequiredClaimError('aud')in the same situation.The root cause is a commented-out guard in
_validate_aud(jwt.py):Fix
Activate the guard: raise
JWTClaimsErrorwhen the caller provides a non-Noneaudiencebut the token has noaudclaim.The existing
options={"require_aud": True}opt-in continues to work as before for callers who need to require the claim without specifying an expected value.Tests
test_aud_empty_claim(which documented the wrong behavior) is replaced by two explicit cases:test_aud_missing_claim_no_audience— no audience expected → still passestest_aud_missing_claim_with_audience— audience given but claim missing → raisesJWTErrortest_requireis corrected to not pass an unrelatedaudience=value when testingrequire_<other-claim>.All 250 tests pass (6 skipped, unchanged).
Closes #407
This pull request was prepared with the assistance of AI, under my direction and review.