Skip to content

Robustly parse quoted WWW-Authenticate parameters - #1764

Open
DanielC000 wants to merge 2 commits into
modelcontextprotocol:mainfrom
DanielC000:fix/www-authenticate-quoted-params
Open

Robustly parse quoted WWW-Authenticate parameters#1764
DanielC000 wants to merge 2 commits into
modelcontextprotocol:mainfrom
DanielC000:fix/www-authenticate-quoted-params

Conversation

@DanielC000

Copy link
Copy Markdown

Closes #1088.

ParseWwwAuthenticateParameters split the header on every comma, including commas inside a quoted value. That's not just a misparse — for scope="," the split leaves a bare " fragment, which passes the old leading/trailing-quote check and then slices value[1..^1] on a one-character span, throwing ArgumentOutOfRangeException out of the auth flow. Escaped quotes inside a value were never unescaped either.

Replaced it with a small single-pass tokenizer following the auth-param and quoted-string grammar 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 of AuthenticationHeaderValue further up).

Tests go in AuthTests.cs alongside 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Robustly parse quoted WWW-Authenticate parameters

1 participant