Repository navigation
oauthex: parse WWW-Authenticate in linear time - #1456
guglielmo-san merged 1 commit into
Conversation
splitChallenges looked ahead from every comma by trimming and scanning the rest of the header, so parsing took time quadratic in the header length. A header made of 100 KB of commas took about 5.7s to parse, and the work does not observe context cancellation. Compute the lookahead once per run of commas and blanks, and read only the next token to decide whether a comma starts an auth-param (startsWithAuthParam). The result is unchanged except for a comma inside the name before the '=', such as `x, a,b=c`: a token cannot contain a comma (RFC 9110, section 5.6.2), so that comma now ends the challenge. Add cases to TestSplitChallenges and TestParseWWWAuthenticateLinearTime, which checks that a 100 KB run of commas parses within a deadline. Fixes modelcontextprotocol#1455
78e9a8e to
4ab9b58
Compare
|
The My original timing test did make it fail more often, though. It ran five 200 KB inputs in parallel with the Could a maintainer re-run the failed job? I can't. |
|
@jyotir-aditya thank you for the contribution! |
splitChallengesrescanned the rest of the header after every comma, so parsing took quadratic time (100 KB of commas: 5.7 s onmain). The lookahead is now computed once per run of commas and blanks, andstartsWithAuthParamreads only the next token.Output is unchanged except for one case: a comma inside the name before the
=(for examplex, a,b=c) now ends the challenge. A token cannot contain a comma (RFC 9110, section 5.6.2); before,a,bwas taken as a parameter name.Tests
TestSplitChallenges: cases for params-less challenges, a blank before=, whitespace inside the name, a non-blank space before=, and the comma case above. All but the comma case pass on the old code, so they pin existing behavior.TestParseWWWAuthenticateLinearTime: a 100 KB run of commas must parse within 2 s. The old code takes about 5.7 s and fails; the new code takes well under 100 ms. The input is deliberately small so the test adds little load to the parallelmcptests.splitChallengeswith the old one on 3,000,000 random headers (scratch test, not included): they differ only by the comma rule.gofmt -l .,go vet ./...,go test ./...andgo test -race ./oauthexare clean. I did not run the conformance scripts; the change does not touch protocol behavior.Fixes #1455