fix: use exact token matching and respect q=0 in matchAcceptEncoding#1133
Open
amitmishra11 wants to merge 1 commit into
Open
fix: use exact token matching and respect q=0 in matchAcceptEncoding#1133amitmishra11 wants to merge 1 commit into
amitmishra11 wants to merge 1 commit into
Conversation
The previous implementation used strings.Contains to match encoding tokens in the Accept-Encoding header. This caused two problems: 1. Substring false positives: "br" incorrectly matched the encoding "b", and "bgzip" incorrectly matched "gzip". 2. q=0 was ignored: per RFC 9110 section 12.5.3, a quality value of 0 means the encoding is explicitly not acceptable. The old code matched "gzip;q=0" as if gzip were acceptable, causing the middleware to compress with an encoding the client explicitly rejected. Fix: rewrite matchAcceptEncoding to split each header token on ";" to separate the encoding name from its quality parameters, trim whitespace, perform an exact comparison of the encoding token, and reject the match when the quality value is 0. Add a table-driven test covering exact matching, substring false positives, q=0 rejection, non-zero quality values, multi-element lists, empty input, and whitespace trimming. Fixes go-chi#1069
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.
Summary
The
matchAcceptEncodinghelper inmiddleware/compress.gousedstrings.Containsto test whether an encoding appears in theAccept-Encodingheader. This caused two correctness bugs:1. Substring false positives
strings.Contains("br", "b")returnstrue, so a client sendingAccept-Encoding: brwould be matched by the encoding"b". Similarly,"bgzip"incorrectly matched"gzip".2.
q=0ignoredPer RFC 9110 section 12.5.3, a quality value of
0means the encoding is not acceptable. The old code matched"gzip;q=0"as if gzip were acceptable, causing the middleware to compress with an encoding the client explicitly refused.Changes
middleware/compress.goRewrote
matchAcceptEncodingto:";"to separate the encoding name from quality parametersq=0(or equivalent0.0,0.00,0.000)middleware/compress_test.goAdded
TestMatchAcceptEncodingwith table-driven cases covering:"br"vs"b","bgzip"vs"gzip")q=0rejectionq=1,q=0.5)Testing
Fixes #1069