Files
seaweedfs/weed/s3api/s3api_object_handlers_conditional_write_test.go
T
0fb1387226 s3: compare If-None-Match weakly, as the filer and volume servers do (#13033)
* s3: compare If-None-Match weakly, as the filer and volume servers do

The S3 gateway matched conditional ETags by trimming quotes, so a weak
tag such as W/"etag" in If-None-Match never matched and a revalidating
GET got 200 instead of 304. Move the filer's RFC 9110 matcher to
weed/util and use it in the gateway: weak comparison for If-None-Match,
strong for If-Match. The routed write condition now sets AllowWeak on
IF_ETAG_NOT_MATCH so the check under the object lock agrees with the
gateway's pre-check.

* s3: split write-condition ETag lists the way the gateway matches them

etagList split on every comma and kept empty elements, so the filer's
locked If-None-Match check (now a weak comparison) could match a fragment
of one quoted tag, or an empty element, that the gateway's pre-check
ignores. A PUT with If-None-Match: W/"<etag>,x" passed the pre-check and
then got 412 under the lock. Reuse the gateway's quote-aware splitter and
drop empty elements.

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

---------

Co-authored-by: Mansur Akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:30:33 -08:00

61 lines
2.6 KiB
Go

package s3api
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
// A conditional write with If-Match has nothing to compare against when the key has
// no current version; AWS answers 404 there, not 412.
func TestValidateConditionalHeadersMissingObject(t *testing.T) {
s3a := &S3ApiServer{}
existing := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()},
Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")},
}
deleteMarker := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()},
Extended: map[string][]byte{s3_constants.ExtDeleteMarkerKey: []byte("true")},
}
testCases := []struct {
name string
header string
value string
entry *filer_pb.Entry
want s3err.ErrorCode
}{
{"if-match on missing object", s3_constants.IfMatch, "d41d8cd98f00b204e9800998ecf8427e", nil, s3err.ErrNoSuchKey},
{"if-match star on missing object", s3_constants.IfMatch, "*", nil, s3err.ErrNoSuchKey},
{"if-match on delete marker", s3_constants.IfMatch, "d41d8cd98f00b204e9800998ecf8427e", deleteMarker, s3err.ErrNoSuchKey},
{"if-match mismatch on existing object", s3_constants.IfMatch, "0000", existing, s3err.ErrPreconditionFailed},
{"if-match hit on existing object", s3_constants.IfMatch, "d41d8cd98f00b204e9800998ecf8427e", existing, s3err.ErrNone},
{"if-none-match star on missing object", s3_constants.IfNoneMatch, "*", nil, s3err.ErrNone},
{"if-none-match star on delete marker", s3_constants.IfNoneMatch, "*", deleteMarker, s3err.ErrNone},
{"if-none-match star on existing object", s3_constants.IfNoneMatch, "*", existing, s3err.ErrPreconditionFailed},
{"if-none-match weak hit on existing object", s3_constants.IfNoneMatch, `W/"d41d8cd98f00b204e9800998ecf8427e"`, existing, s3err.ErrPreconditionFailed},
{"if-match weak tag on existing object", s3_constants.IfMatch, `W/"d41d8cd98f00b204e9800998ecf8427e"`, existing, s3err.ErrPreconditionFailed},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodPut, "/bucket/object", nil)
r.Header.Set(tc.header, tc.value)
headers, errCode := parseConditionalHeaders(r)
if errCode != s3err.ErrNone {
t.Fatalf("parseConditionalHeaders: %v", errCode)
}
if got := s3a.validateConditionalHeaders(r, headers, tc.entry, "bucket", "object"); got != tc.want {
t.Errorf("got %v, want %v", got, tc.want)
}
})
}
}