Skip to content

compress: fix quality value of 0 being ignored in getEncoder - #296

Merged
System-Glitch merged 3 commits into
go-goyave:masterfrom
MaxFreedomPollard:fix/compress-quality-zero
Sep 28, 2026
Merged

System-Glitch merged 3 commits into
go-goyave:masterfrom
MaxFreedomPollard:fix/compress-quality-zero

Conversation

@MaxFreedomPollard

@MaxFreedomPollard MaxFreedomPollard commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

References

Issue(s): N/A
Discussion: N/A

Description

Middleware.getEncoder (middleware/compress/compress.go:144 on master) groups the parsed Accept-Encoding values by priority at line 152 and matches the configured encoders against each group, but it never compares a priority to 0. RFC 9110 section 12.4.2 defines a quality value of 0 as "not acceptable", and section 12.5.3 applies that rule to Accept-Encoding: a listed content coding is acceptable unless it is accompanied by a qvalue of 0. The middleware therefore compresses responses with an encoding the client explicitly refused.

With Encoders: []Encoder{&Gzip{Level: gzip.BestCompression}}:

  • Accept-Encoding: gzip;q=0 comes back with Content-Encoding: gzip and a gzip framed body. A client that sends q=0 for gzip is exactly the client that cannot decode gzip, so the body is unreadable to it.
  • Accept-Encoding: *;q=0 does the same, even though it says no content coding is acceptable at all.
  • Accept-Encoding: gzip;q=0, * also returns gzip, because the wildcard branch at line 165 returns m.Encoders[0] unconditionally. The wildcard matches the codings that are not explicitly listed, so gzip is the one encoding it must not pick here.

That same unconditional m.Encoders[0] panics with index out of range [0] with length 0 when Encoders is empty and the request sends Accept-Encoding: *. Every other header value already returns nil for an empty Encoders slice, so only the wildcard group reaches the panic.

httputil.ParseMultiValuesHeader already parses the quality value correctly since #295 and returns a Priority of 0 for the q=0 entries in the first three headers. Only the consumer was missing.

The fix splits the parsed values before any matching. They come back sorted by descending priority, so the zero-priority values are always a suffix: slices.IndexFunc locates the first one, everything before it is accepted and everything from it onwards is rejected. When nothing is accepted, getEncoder returns nil and the middleware passes the response through uncompressed. The wildcard branch now returns the first configured encoder that is not in the rejected list, and nil when every configured encoder was rejected or when Encoders is empty, which removes the panic. The priority ordering, the rule that the earliest encoder in the slice wins on equal priority, and the hijack and Upgrade short circuits are untouched. The doc comment on Middleware now describes the rejection rule and links both RFC sections.

Thirteen cases were added to the existing TestEncoderPriority table: gzip;q=0, gzip;q=0.000, gzip; Q = 0, gzip;q=0.001, gzip;q=0, br, br;q=0, zstd;q=0, gzip;q=0, *;q=0, *;q=0, gzip, br;q=0, *, br;q=0, *;q=0.5, br;q=0, zstd;q=0, gzip;q=0, *, gzip;q=0, * with a single gzip encoder, and * with an empty Encoders slice. A Rejected encoding subtest in TestCompressMiddleware checks that a full request with Accept-Encoding: gzip;q=0 returns the plain body and no Content-Encoding header. Ten of the table rows and that subtest fail on master, the empty Encoders row by panicking at compress.go:165; the other three rows pass on master and guard behavior that does not change.

go test -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./middleware/compress/... passes and go tool cover -func reports getEncoder at 100% of statements. gofmt -s -l . prints nothing and go vet ./middleware/compress/... is clean. golangci-lint was not run locally.

Possible drawbacks

The headline change is a behavior change: a response to Accept-Encoding: gzip;q=0 used to be gzip encoded and is now sent uncompressed. That is the intended change, RFC 9110 section 12.5.3 requires it, no exported signature changes, and applications keep compiling and working without developer action.

Two further header shapes change with it.

parseQuality (util/httputil/httputil.go:85) returns 0 both for an explicit q=0 and for a quality value it cannot parse, and its doc comment says so. A request sending Accept-Encoding: gzip;q=abc, gzip;q=1e-2 or an out of range value such as gzip;q=-1 used to be compressed at lowest priority and is now not compressed.

The wildcard part is a separate change from the plain q=0 part. Accept-Encoding: gzip;q=0, * used to select the first encoder in the slice and now skips the rejected ones. RFC 9110 section 12.5.3 justifies it independently: the wildcard matches the codings that are not listed in the field, and gzip is listed there. The change only excludes codings the header rejected with q=0; a coding listed with a non-zero quality value can still be selected through the wildcard, as before. lang.Languages.DetectLanguage (lang/lang.go:166) reads the same parser and has the same q=0 gap for Accept-Language; I left it out of this change to keep it to the compress middleware.

@coveralls

coveralls commented Sep 15, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 36429482251

Coverage increased (+0.003%) to 97.144%

Details

  • Coverage increased (+0.003%) from the base build.
  • Patch coverage: 14 of 14 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 5848
Covered Lines: 5681
Line Coverage: 97.14%
Coverage Strength: 278.49 hits per line

💛 - Coveralls

@System-Glitch
System-Glitch self-requested a review September 16, 2026 07:53
@System-Glitch System-Glitch self-assigned this Sep 16, 2026
@System-Glitch System-Glitch added the bug Something isn't working label Sep 16, 2026

@System-Glitch System-Glitch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for this PR. Compliance with standards is very important to me.

As explained in the PR description, the language detection is incorrect as well. A simple fix would be to add the following and a quick test case:

if lang.Priority == 0 { // Not acceptable according to RFC 9110
	continue
}

I'd like to bundle this change in the MR as well so we fix all compliance problems in one patch release.

I would add a small comment to httputil.ParseMultiValuesHeader to tell users that a priority of 0 means that it's not acceptable too.

Comment thread middleware/compress/compress.go Resolved
Comment thread middleware/compress/compress_test.go Resolved
@MaxFreedomPollard

Copy link
Copy Markdown
Contributor Author

Pushed e5284e3. getEncoder filters with lo.FilterReject and returns early on empty Encoders. I kept the rejected list for the wildcard only: with the filter alone, br;q=0, * falls through to Encoders[0], which is br, and three table rows cover that. Happy to drop it with those rows if you prefer. DetectLanguage skips q=0 (four new cases), and the ParseMultiValuesHeader doc now says what priority 0 means.

@System-Glitch System-Glitch left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes and additional docs and tests, that's awesome. 👍🏻

I kept the rejected list for the wildcard only: with the filter alone, br;q=0, * falls through to Encoders[0], which is br, and three table rows cover that. Happy to drop it with those rows if you prefer.

I didn't think about this specific case at first; hence why I thought at first that the rejected list wasn't necessary. But that's good we should keep that.

Currently a bit busy but I'll merge that soon. Thanks a lot for your contribution!

Comment thread lang/lang_test.go Resolved
`getEncoder` grouped the parsed `Accept-Encoding` values by priority but
never compared that priority to 0, so an encoding the client explicitly
refused with `q=0` was still selected and the response was compressed
with it. The parsed values are sorted by descending priority, so the
zero-priority ones are now split off the end of the slice before the
encoders are matched, and the loop only sees encodings the client
actually accepts. When nothing is left, the middleware passes through
uncompressed.

The wildcard branch used to return the first configured encoder
unconditionally, which returned the rejected encoding for a header such
as `gzip;q=0, *`. It now returns the first configured encoder that the
header did not reject, and nil when every encoder was rejected. That
unconditional `m.Encoders[0]` also panicked with an index out of range
when `Encoders` was empty and the request sent `Accept-Encoding: *`; the
lookup that replaces it returns nil instead.
getEncoder now filters the parsed Accept-Encoding values with lo.FilterReject and returns early when no encoder is configured. The wildcard still skips the rejected encodings, because "br;q=0, *" must not select br.

DetectLanguage ignores the languages given a quality value of 0, and the ParseMultiValuesHeader documentation says what a priority of 0 means.
@System-Glitch
System-Glitch force-pushed the fix/compress-quality-zero branch from 84821b4 to 82dedc0 Compare September 28, 2026 13:33
@System-Glitch
System-Glitch merged commit 82dedc0 into go-goyave:master Sep 28, 2026
5 checks passed
@System-Glitch

Copy link
Copy Markdown
Member

Sorry for the delay. This is merged now and I'll tag v5.12.5 very soon. Thanks for the contribution! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants