From 42778d2ba65520c4b7a4fe12e7acceac1c9a8415 Mon Sep 17 00:00:00 2001 From: Kevin Pollet Date: Wed, 26 Mar 2025 11:30:05 +0100 Subject: [PATCH] Do not abort request when response content-type is malformed --- pkg/middlewares/compress/compression_handler.go | 6 +++++- pkg/middlewares/compress/compression_handler_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/middlewares/compress/compression_handler.go b/pkg/middlewares/compress/compression_handler.go index 215607ccd..07583028b 100644 --- a/pkg/middlewares/compress/compression_handler.go +++ b/pkg/middlewares/compress/compression_handler.go @@ -233,8 +233,12 @@ func (r *responseWriter) Write(p []byte) (int, error) { // Disable compression according to user wishes in excludedContentTypes or includedContentTypes. if ct := r.rw.Header().Get(contentType); ct != "" { mediaType, params, err := mime.ParseMediaType(ct) + // To align the behavior with the klauspost handler for Gzip, + // if the MIME type is not parsable the compression is disabled. if err != nil { - return 0, fmt.Errorf("parsing content-type media type: %w", err) + r.compressionDisabled = true + r.rw.WriteHeader(r.statusCode) + return r.rw.Write(p) } if len(r.includedContentTypes) > 0 { diff --git a/pkg/middlewares/compress/compression_handler_test.go b/pkg/middlewares/compress/compression_handler_test.go index b078ed71f..89bbe062b 100644 --- a/pkg/middlewares/compress/compression_handler_test.go +++ b/pkg/middlewares/compress/compression_handler_test.go @@ -577,6 +577,11 @@ func Test_ExcludedContentTypes(t *testing.T) { contentType: "", expCompression: true, }, + { + desc: "MIME malformed", + contentType: "application/json;charset=UTF-8;charset=utf-8", + expCompression: false, + }, { desc: "MIME match", contentType: "application/json", @@ -687,6 +692,11 @@ func Test_IncludedContentTypes(t *testing.T) { contentType: "", expCompression: true, }, + { + desc: "MIME malformed", + contentType: "application/json;charset=UTF-8;charset=utf-8", + expCompression: false, + }, { desc: "MIME match", contentType: "application/json",