Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions middleware/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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.

}

// limitedGzipReader wraps a gzip reader with size limiting to prevent zip bombs
Expand Down
54 changes: 54 additions & 0 deletions middleware/decompress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}