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/"<etag>"
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 <noreply@anthropic.com>

* mount: skip the truncate manifest test on freebsd like its fake filer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-27 12:48:08 -08:00
1 parent 397a156104
commit 77333a5578
2 files changed
+57 -5

No files matched your search

+5 -5
View File
@@ -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
}
@@ -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()