Fix Accept-Encoding substring matching in compress middleware#1076
Open
andrewstellman wants to merge 1 commit intogo-chi:masterfrom
Open
Fix Accept-Encoding substring matching in compress middleware#1076andrewstellman wants to merge 1 commit intogo-chi:masterfrom
andrewstellman wants to merge 1 commit intogo-chi:masterfrom
Conversation
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
matchAcceptEncodingin the compression middleware usesstrings.Containsto check whether a client accepts a given encoding. This is a substring match, which produces incorrect results in two cases:gzip;q=0matches as gzip, even thoughq=0means the client explicitly refused itxgzipmatches as gzip, even though it's a different encoding nameThe root cause is on line 242 of
middleware/compress.go:Fix
Replace the substring match with proper token parsing: split the encoding name from any parameters (like
;q=0.5), trim whitespace, do an exact case-insensitive comparison, and check forq=0which means the encoding was explicitly refused.This is a minimal change to
matchAcceptEncodingonly. The rest of the compression middleware is unchanged.Tests
Five new test cases in
TestMatchAcceptEncodingexercise the function directly:gzipgzip;q=0xgzipgzip;q=0.5gzipFull test suite passes (
go test ./...).