fix(verify): make regexp audience consistent with string audience for tokens without an aud claim#1038
Open
spokodev wants to merge 1 commit into
Open
Conversation
… tokens without an aud claim A RegExp `audience` option matches a token that has no `aud` claim, while a string `audience` correctly rejects it. The mismatch comes from the missing `aud` being coerced to the string "undefined" before `RegExp.prototype.test` runs, so a permissive pattern such as `/.+/` reports a match. Guard the regexp branch to test only string targets, which lines up its behavior with the string-equality branch and the existing no-aud test expectations.
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.
Description
When
verifyis given a regexpaudience, a token that has noaudclaim is treated differently from how a stringaudiencetreats the same token. A string rejects it; a regexp can accept it. This PR makes the two branches consistent.Reproduction
A token with
aud: ""oraud: []is rejected by both branches, so only the missing-audcase diverges.Existing test expectations
test/claim-aud.test.jsalready has adescribe('without a "aud" value in payload')block asserting that a no-aud token must fail every audience check, including the regexp ones. Those existing tests pass only because they use patterns like/^urn:no-match$/that happen not to match the coerced value. A pattern that matches any string exposes the gap, which is what the added test covers.Root cause
In
verify.js:When
audis absent,payload.audisundefined, sotargetbecomes[undefined]. The regexp branch then runs:RegExp.prototype.testcoerces its argument to a string first, so this evaluatesaudience.test("undefined"). A permissive pattern matches the literal string"undefined"and the check passes. The string-equality branch (audience === targetAudience) does no coercion, so it rejects the same token. Per RFC 7519 section 4.1.3, a verifier that requires an audience should reject a token whoseaudis absent.Fix
Test the regexp only against string targets:
This brings the regexp branch in line with the string-equality branch and with the no-aud expectations already encoded in the test suite.
Testing
Added a test under the existing
without a "aud" value in payloadblock: a regexp that matches any string must still reject a no-aud token. It fails onmaster(the token is accepted) and passes with the fix.npm test: 512 passing, 1 pending, 0 failingnpx eslint verify.js test/claim-aud.test.js: cleanChecklist
master