Skip to content

fix(middleware): recognize x-gzip Content-Encoding in Decompress - #3260

Open
waw666waw666 wants to merge 1 commit into
labstack:masterfrom
waw666waw666:fix/decompress-x-gzip-content-encoding
Open

waw666waw666 wants to merge 1 commit into
labstack:masterfrom
waw666waw666:fix/decompress-x-gzip-content-encoding

Conversation

@waw666waw666

@waw666waw666 waw666waw666 commented Dec 2, 2026 •

Copy link
Copy Markdown

Requests with Content-Encoding: x-gzip carry a gzip body (RFC 9110
section 8.4.1.3: x-gzip is equivalent to gzip), but isGzipContentEncoding
only matched gzip itself, so Decompress passed the body through still
compressed and handlers read raw gzip bytes.

The Gzip response middleware already treats x-gzip as gzip (#3239); this
applies the same rule to the request side.

Fixes: no open issue; sibling of #3056 (case-insensitive matching) and
#3239 (x-gzip on the response side).

What changed

  • middleware/decompress.go: isGzipContentEncoding now also matches
    x-gzip (case-insensitively).
  • middleware/decompress_test.go: added TestDecompressContentEncodingXGzip
    (handler-level: x-gzip and X-GZIP bodies are decompressed) and
    TestIsGzipContentEncoding (predicate table: deflate, br, identity,
    xgzip, and multi-coding values still pass through untouched).

How to test

go test ./middleware/ -run 'TestDecompress' -count=1
go test ./middleware/ -race -run 'TestDecompress' -count=1

Full suite (go test ./...): the same 4 pre-existing root-package failures
appear with and without this change (TestEchoStaticRedirectIndex,
TestStartConfig_Start, TestStartConfig_GracefulShutdown,
TestStartConfig_WithHTTP2WithCustomTlsConfig, all sandbox loopback EOF
flakes); zero new failures.

Summary by CodeRabbit

  • Bug Fixes
    • Request decompression now accepts x-gzip encodings, including case variations.

Requests with Content-Encoding: x-gzip carry a gzip body (RFC 9110
section 8.4.1.3: x-gzip is equivalent to gzip), but isGzipContentEncoding
only matched gzip itself, so Decompress passed the body through still
compressed and handlers read raw gzip bytes. The Gzip response middleware
already treats x-gzip as gzip (labstack#3239); this applies the same rule to the
request side.
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

Changes: Gzip content encoding

Layer / File(s) Summary
Recognize x-gzip encoding
middleware/decompress.go, middleware/decompress_test.go
The predicate now accepts case-insensitive gzip and x-gzip values. Tests verify decompression for x-gzip requests and reject empty, unsupported, combined, whitespace-suffixed, and non-hyphenated values.

Suggested reviewers: sonnemusk

Change: Bug fix

Merge Risk: Low · up to 14c0f

Requests that send repeated Content-Encoding header fields, such as x-gzip and gzip, can be only partly decompressed. This is an unusual input, so the risk is low. Joining all Content-Encoding field values before matching would fix it.

Security architecture risk: Minimal · up to 14c0f

Decompress now treats x-gzip as another spelling of gzip. Both spellings use the same decoder, skip rules, and configured size limit. The change adds no route, privilege, or separate decoding path.

Security review details

Security Blast Radius

  • inferred — The additional header spelling affects only requests that reach Decompress and are not skipped. It gives those requests another way to select the same decoder they could already select with gzip.

Trust Boundaries and Controls

  • observed — Both spellings reach the same decompressed-byte limit: a zero configuration value defaults to 100 MB, positive values bound decoded reads, and the existing unlimited configuration also applies to both spellings.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments

Minor 1
  1. Check all Content-Encoding fields before decompressing.
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a02173ef-1149-48db-90b6-958d2f18fb73
Commits

Reviewing files that changed from the base of the PR and between 59aacdd and 14c0f41.

Files selected for processing: 2

  • middleware/decompress.go
  • middleware/decompress_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment thread middleware/decompress.go
// already treats it as gzip; see acceptsGzip).
func isGzipContentEncoding(v string) bool {
return strings.EqualFold(v, GZIPEncoding)
return strings.EqualFold(v, GZIPEncoding) || strings.EqualFold(v, "x-gzip")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Data Integrity & Integration · Quick win

Check all Content-Encoding fields before decompressing.

If a request has separate Content-Encoding: x-gzip and Content-Encoding: gzip fields, Header.Get passes only x-gzip to this predicate. The middleware then removes one layer, although the combined coding should remain unrecognized. Before this change, the request passed through unchanged.

Combine the header field values before matching, so repeated fields receive the same treatment as gzip, br.

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.

1 participant