From 14c0f414e8e1559ec7104da87ae78feb515ec856 Mon Sep 17 00:00:00 2001 From: waw666waw666 <184799977+waw666waw666@users.noreply.github.com> Date: Wed, 2 Dec 2026 05:27:41 +0000 Subject: [PATCH] fix(middleware): recognize x-gzip Content-Encoding in Decompress 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. --- middleware/decompress.go | 6 ++-- middleware/decompress_test.go | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/middleware/decompress.go b/middleware/decompress.go index 355455ce6..efff9a05c 100644 --- a/middleware/decompress.go +++ b/middleware/decompress.go @@ -129,9 +129,11 @@ func (config DecompressConfig) ToMiddleware() (echo.MiddlewareFunc, error) { } // isGzipContentEncoding reports whether Content-Encoding is gzip. -// Content codings are case-insensitive per RFC 9110 §8.4.1. +// Content codings are case-insensitive per RFC 9110 §8.4.1, and x-gzip is +// equivalent to gzip per RFC 9110 §8.4.1.3 (the Gzip response middleware +// 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") } // limitedGzipReader wraps a gzip reader with size limiting to prevent zip bombs diff --git a/middleware/decompress_test.go b/middleware/decompress_test.go index b154dd5b2..772bbd77a 100644 --- a/middleware/decompress_test.go +++ b/middleware/decompress_test.go @@ -578,3 +578,57 @@ func TestDecompressContentEncodingCaseInsensitive(t *testing.T) { }) } } + +func TestDecompressContentEncodingXGzip(t *testing.T) { + e := echo.New() + body := `{"name":"echo"}` + gz, err := gzipString(body) + assert.NoError(t, err) + + // RFC 9110 §8.4.1.3: x-gzip is equivalent to gzip. The Gzip response + // middleware already treats it as gzip (#3239); Decompress must do + // the same for request bodies, or the handler reads raw gzip bytes. + for _, encoding := range []string{"x-gzip", "X-GZIP"} { + t.Run(encoding, func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(gz)) + req.Header.Set(echo.HeaderContentEncoding, encoding) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + h := Decompress()(func(c *echo.Context) error { + b, err := io.ReadAll(c.Request().Body) + if err != nil { + return err + } + return c.String(http.StatusOK, string(b)) + }) + assert.NoError(t, h(c)) + assert.Equal(t, body, rec.Body.String()) + }) + } +} + +func TestIsGzipContentEncoding(t *testing.T) { + for _, tc := range []struct { + encoding string + want bool + }{ + {"gzip", true}, + {"GZIP", true}, + {"Gzip", true}, + {"x-gzip", true}, + {"X-GZIP", true}, + {"X-Gzip", true}, + {"", false}, + {"deflate", false}, + {"br", false}, + {"identity", false}, + {"xgzip", false}, + {"gzip, br", false}, + {"gzip ", false}, + } { + t.Run(tc.encoding, func(t *testing.T) { + assert.Equal(t, tc.want, isGzipContentEncoding(tc.encoding)) + }) + } +}