Robustly parse quoted WWW-Authenticate parameters - #1764
Open
DanielC000 wants to merge 2 commits into
Open
Conversation
The naive comma-split in ParseWwwAuthenticateParameters broke on commas or escaped quotes inside a quoted parameter value, and could even throw on a value like "error=\",\"". Replaced it with a small RFC 9110 auth-param/quoted-string tokenizer. Closes modelcontextprotocol#1088.
Swap the reflection-based unit test for coverage in AuthTests.cs, in the repo's existing idiom of driving a real 401 challenge and checking what reaches the outgoing authorization request. Covers the scope="," crash from the issue and a value with both a comma and an escaped quote.
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.
Closes #1088.
ParseWwwAuthenticateParameterssplit the header on every comma, including commas inside a quoted value. That's not just a misparse — forscope=","the split leaves a bare"fragment, which passes the old leading/trailing-quote check and then slicesvalue[1..^1]on a one-character span, throwingArgumentOutOfRangeExceptionout of the auth flow. Escaped quotes inside a value were never unescaped either.Replaced it with a small single-pass tokenizer following the
auth-paramandquoted-stringgrammar from RFC 9110 (§11.6.1 and §5.6.4), including quoted-pair unescaping. It stays a private method, so there's no new public surface.Two things it deliberately doesn't handle, since neither is reachable from the current caller:
token68-form credentials, and multiple challenges combined into one header line (that one's really a limitation ofAuthenticationHeaderValuefurther up).Tests go in
AuthTests.csalongside the existing challenge-header coverage, driving a real 401 rather than poking at the parser directly. Both fail against the old code — the first with the exception above, the second with a truncated scope.This pull request description and the accompanying changes were AI-generated.