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)) + }) + } +}