From 77333a5578b7582ffb22eccff98bbe47ecd3b2d3 Mon Sep 17 00:00:00 2001 From: karnati praveen Date: Sat, 28 Nov 2026 02:18:08 +0530 Subject: [PATCH] s3: match copy-source ETag conditions with the shared helper (#13054) * s3: match copy-source ETags as the read and write paths do validateSourceConditionalHeaders compared the source ETag to the header value with strings.Trim, so x-amz-copy-source-if-none-match: W/"" did not match and an unchanged source was copied instead of answered with 412. An If-Match list that contained the source's ETag was rejected for the same reason. RenameObject shares the helper, so x-amz-rename-source-if-* behaved the same way. Use util.ETagListMatches, which the GET and PUT paths already use: strong comparison for If-Match, weak for If-None-Match, and the quoted tag lists RFC 9110 allows. * s3: drop the history comment from the copy-source ETag checks Co-Authored-By: Claude Opus 5.5 * mount: skip the truncate manifest test on freebsd like its fake filer Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Chris Lu Co-authored-by: Claude Opus 5.5 --- weed/s3api/s3api_object_handlers_copy.go | 10 ++-- .../s3api_object_handlers_copy_etag_test.go | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/weed/s3api/s3api_object_handlers_copy.go b/weed/s3api/s3api_object_handlers_copy.go index 40aec1df5d..d8d9e9681b 100644 --- a/weed/s3api/s3api_object_handlers_copy.go +++ b/weed/s3api/s3api_object_handlers_copy.go @@ -2034,16 +2034,16 @@ func (s3a *S3ApiServer) validateConditionalCopyHeaders(r *http.Request, entry *f // matching x-amz-copy-source-if-match with a failing x-amz-copy-source-if-unmodified-since // copies rather than returning 412. func validateSourceConditionalHeaders(r *http.Request, entry *filer_pb.Entry, names sourceConditionalHeaderNames) s3err.ErrorCode { - sourceETag := strings.Trim(copyEntryETag(entry), `"`) - ifMatch := strings.Trim(r.Header.Get(names.ifMatch), `"`) - ifNoneMatch := strings.Trim(r.Header.Get(names.ifNoneMatch), `"`) + sourceETag := copyEntryETag(entry) + ifMatch := r.Header.Get(names.ifMatch) + ifNoneMatch := r.Header.Get(names.ifNoneMatch) - if ifMatch != "" && ifMatch != "*" && ifMatch != sourceETag { + if ifMatch != "" && !util.ETagListMatches(ifMatch, sourceETag, false) { glog.V(3).Infof("%s failed - expected %s, got %s", names.ifMatch, ifMatch, sourceETag) return s3err.ErrPreconditionFailed } - if ifNoneMatch != "" && (ifNoneMatch == "*" || ifNoneMatch == sourceETag) { + if ifNoneMatch != "" && util.ETagListMatches(ifNoneMatch, sourceETag, true) { glog.V(3).Infof("%s failed - matched %s", names.ifNoneMatch, sourceETag) return s3err.ErrPreconditionFailed } diff --git a/weed/s3api/s3api_object_handlers_copy_etag_test.go b/weed/s3api/s3api_object_handlers_copy_etag_test.go index fdf525fbd1..58d6915fc3 100644 --- a/weed/s3api/s3api_object_handlers_copy_etag_test.go +++ b/weed/s3api/s3api_object_handlers_copy_etag_test.go @@ -55,6 +55,58 @@ func TestValidateConditionalCopyHeadersUsesStoredExtendedETag(t *testing.T) { } } +// The copy and rename source conditions match ETags the way the read and write +// paths do: If-None-Match ignores the W/ prefix, and either header accepts the +// comma-separated list RFC 9110 allows. +func TestSourceConditionalHeadersMatchETagLists(t *testing.T) { + etag := "d41d8cd98f00b204e9800998ecf8427e" + entry := &filer_pb.Entry{Attributes: &filer_pb.FuseAttributes{Md5: mustDecodeHexETagForTest(t, etag)}} + + for _, names := range []sourceConditionalHeaderNames{copySourceConditionalHeaders, renameSourceConditionalHeaders} { + failing := []string{ + `"` + etag + `"`, + `W/"` + etag + `"`, + `"0000", "` + etag + `"`, + `*`, + } + for _, value := range failing { + r := httptest.NewRequest(http.MethodPut, "/bucket/dst", nil) + r.Header.Set(names.ifNoneMatch, value) + if got := validateSourceConditionalHeaders(r, entry, names); got != s3err.ErrPreconditionFailed { + t.Errorf("%s: %q = %v, want %v", names.ifNoneMatch, value, got, s3err.ErrPreconditionFailed) + } + } + r := httptest.NewRequest(http.MethodPut, "/bucket/dst", nil) + r.Header.Set(names.ifNoneMatch, `"0000"`) + if got := validateSourceConditionalHeaders(r, entry, names); got != s3err.ErrNone { + t.Errorf("%s: unrelated tag = %v, want %v", names.ifNoneMatch, got, s3err.ErrNone) + } + + passing := []string{ + `"` + etag + `"`, + etag, + `"0000", "` + etag + `"`, + `"` + etag + `", "0000"`, + `*`, + } + for _, value := range passing { + r := httptest.NewRequest(http.MethodPut, "/bucket/dst", nil) + r.Header.Set(names.ifMatch, value) + if got := validateSourceConditionalHeaders(r, entry, names); got != s3err.ErrNone { + t.Errorf("%s: %q = %v, want %v", names.ifMatch, value, got, s3err.ErrNone) + } + } + // If-Match uses the strong comparison, so a weak tag never satisfies it. + for _, value := range []string{`W/"` + etag + `"`, `"0000"`} { + r := httptest.NewRequest(http.MethodPut, "/bucket/dst", nil) + r.Header.Set(names.ifMatch, value) + if got := validateSourceConditionalHeaders(r, entry, names); got != s3err.ErrPreconditionFailed { + t.Errorf("%s: %q = %v, want %v", names.ifMatch, value, got, s3err.ErrPreconditionFailed) + } + } + } +} + func newCopyETagTestEntry(t *testing.T, storedETag, computedETag string) *filer_pb.Entry { t.Helper()