Reject non-string/array aud instead of silently discarding it (#6) - #22
Open
jaideeppyne wants to merge 1 commit into
Open
Reject non-string/array aud instead of silently discarding it (#6)#22jaideeppyne wants to merge 1 commit into
aud instead of silently discarding it (#6)#22jaideeppyne wants to merge 1 commit into
Conversation
…ataras#6) Audience.UnmarshalJSON only handled the '"' (string) and '[' (array) cases. Any other JSON type for "aud" -- a number, boolean, or object -- matched no case, left err == nil, and returned with the audience silently set to nil. That is more permissive than encoding/json (which rejects those when decoding into []string) and drops a security-relevant claim on the floor: a token whose "aud" is a number or object parses as if it had no audience at all. Reject unexpected JSON types with an ErrTokenForm error, treat JSON null as an absent audience, and stop assigning the array result when its unmarshal failed. Adds regression tests for the rejected types and for the still-accepted string, array, null and omitted forms.
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.
What
Fixes #6.
Audience.UnmarshalJSONaccepts an"aud"claim of an unexpected JSON type (number, boolean, object) without error and silently discards it.Why
The unmarshaler only handles two cases:
There is no
defaultand nonullhandling, so a number/boolean/object matches no case, leaveserr == nil, and returns withaud == nil. That is more permissive thanencoding/json(which rejects those values when decoding into[]string), and it drops a security-relevant claim:audis how multi-tenant systems scope tokens, so a token whose audience is a number or object parses as if it had no audience at all. The'['branch also assignsaudStringseven when the unmarshal failed.How
case '[': // it's an array of strings. var audStrings []string err = json.Unmarshal(data, &audStrings) - *aud = audStrings + if err == nil { + *aud = audStrings + } +case 'n': // it's null, treat as an absent audience. +default: // any other JSON type (number, boolean, object) is not a valid audience. + err = fmt.Errorf("%w: aud: must be a string or an array of strings", ErrTokenForm) }ErrTokenForm(the package's existing sentinel, used the same way fornbf/iat/exp/iss/sub).nullis treated as an absent audience (no error), matching an omitted claim.Tests
Added to
claims_time_test.go:TestAudienceRejectsInvalidTypes—{"aud":1234},{"aud":true},{"aud":{"x":1}}must fail withErrTokenForm(this fails onmainand passes with the fix);TestAudienceValidFormsStillAccepted— string, array, explicitnull, and omittedaudstill parse to the expected audience.The full suite stays green (
go test ./...).Disclosure: this change was prepared with AI assistance and reviewed/verified by me before submission.