From 840615c3576c4af0c7b1eec00dd241c4911a3410 Mon Sep 17 00:00:00 2001 From: lingyicute Date: Sat, 28 Nov 2026 09:52:43 +0000 Subject: [PATCH] fix(s3): trim whitespace and match aws-chunked case-insensitively in Content-Encoding Content-Encoding is a comma-separated list of content-coding tokens with optional surrounding whitespace, and token names are case-insensitive per RFC 9110 Section 8.4. Previously, trimAwsChunkedContentEncoding split on commas and compared each raw segment against "aws-chunked" with exact string equality. Inputs such as "gzip, aws-chunked" or "AWS-Chunked" retained the transport coding in stored object metadata, and "aws-chunked, gzip" left a leading space in the persisted encoding. Trim surrounding whitespace on each comma-separated coding token and match "aws-chunked" with strings.EqualFold. Update the corresponding whitespace cases in TestExtractReplicationMetadataPreservesNormalizedMetadata and add unit tests in TestTrimAwsChunkedContentEncoding. Signed-off-by: lingyicute --- cmd/handler-utils.go | 6 +++++- cmd/handler-utils_test.go | 27 +++++++++++++++++++++++++++ cmd/replication-metadata_test.go | 5 ++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cmd/handler-utils.go b/cmd/handler-utils.go index 3a36466dd..18f801087 100644 --- a/cmd/handler-utils.go +++ b/cmd/handler-utils.go @@ -370,7 +370,11 @@ func trimAwsChunkedContentEncoding(contentEnc string) (trimmedContentEnc string) } var newEncs []string for enc := range strings.SplitSeq(contentEnc, ",") { - if enc != streamingContentEncoding { + // Content-Encoding is a comma separated list with optional + // whitespace around each coding, and coding names are + // case-insensitive, e.g. "gzip, aws-chunked". + enc = strings.TrimSpace(enc) + if !strings.EqualFold(enc, streamingContentEncoding) { newEncs = append(newEncs, enc) } } diff --git a/cmd/handler-utils_test.go b/cmd/handler-utils_test.go index 6a4c7598b..4d65bf42c 100644 --- a/cmd/handler-utils_test.go +++ b/cmd/handler-utils_test.go @@ -392,3 +392,30 @@ func TestGetResource(t *testing.T) { } } } + +// Test trimAwsChunkedContentEncoding() removes the aws-chunked coding wherever +// it appears in the Content-Encoding list, regardless of the optional +// whitespace after the commas or the case of the coding name. +func TestTrimAwsChunkedContentEncoding(t *testing.T) { + testCases := []struct { + in string + expected string + }{ + {"", ""}, + {"aws-chunked", ""}, + {"aws-chunked,gzip", "gzip"}, + {"gzip,aws-chunked", "gzip"}, + {"aws-chunked, gzip", "gzip"}, + {"gzip, aws-chunked", "gzip"}, + {"gzip , aws-chunked", "gzip"}, + {"AWS-Chunked", ""}, + {"gzip, AWS-Chunked", "gzip"}, + {"gzip", "gzip"}, + {"gzip, br", "gzip,br"}, + } + for i, test := range testCases { + if got := trimAwsChunkedContentEncoding(test.in); got != test.expected { + t.Errorf("test %d: input %q: expected %q got %q", i+1, test.in, test.expected, got) + } + } +} diff --git a/cmd/replication-metadata_test.go b/cmd/replication-metadata_test.go index f464f28b5..98c54b946 100644 --- a/cmd/replication-metadata_test.go +++ b/cmd/replication-metadata_test.go @@ -26,9 +26,8 @@ func TestExtractReplicationMetadataPreservesNormalizedMetadata(t *testing.T) { {name: "gzip", wire: []string{"gzip"}, want: "gzip"}, {name: "transport-last", wire: []string{"gzip,aws-chunked"}, want: "gzip"}, {name: "multiple-values", wire: []string{"aws-chunked", "gzip"}, want: "gzip"}, - // Preserve the existing exact-token grammar; whitespace is not normalized here. - {name: "space-before-gzip", wire: []string{"aws-chunked, gzip"}, want: " gzip"}, - {name: "space-before-transport", wire: []string{"gzip, aws-chunked"}, want: "gzip, aws-chunked"}, + {name: "space-before-gzip", wire: []string{"aws-chunked, gzip"}, want: "gzip"}, + {name: "space-before-transport", wire: []string{"gzip, aws-chunked"}, want: "gzip"}, } { for _, lowercase := range []bool{false, true} { name := tc.name + "/canonical"