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()