diff --git a/weed/s3api/s3_constants/header.go b/weed/s3api/s3_constants/header.go index 6256d4376b..82ffdc9fe5 100644 --- a/weed/s3api/s3_constants/header.go +++ b/weed/s3api/s3_constants/header.go @@ -82,6 +82,7 @@ const ( AmzObjectLockMode = "X-Amz-Object-Lock-Mode" AmzObjectLockRetainUntilDate = "X-Amz-Object-Lock-Retain-Until-Date" AmzObjectLockLegalHold = "X-Amz-Object-Lock-Legal-Hold" + AmzBypassGovernanceRetention = "X-Amz-Bypass-Governance-Retention" // S3 checksum headers AmzChecksumAlgorithm = "X-Amz-Checksum-Algorithm" diff --git a/weed/s3api/s3api_governance_permissions_test.go b/weed/s3api/s3api_governance_permissions_test.go index 9c05fbcd13..7cb6b648dc 100644 --- a/weed/s3api/s3api_governance_permissions_test.go +++ b/weed/s3api/s3api_governance_permissions_test.go @@ -239,6 +239,18 @@ func TestGovernanceBypassHeader(t *testing.T) { expectedResult: true, description: "Header with 'true' value should enable bypass", }, + { + name: "bypass_header_title_case", + headerValue: "True", + expectedResult: true, + description: "botocore and the AWS CLI send 'True'", + }, + { + name: "bypass_header_upper_case", + headerValue: "TRUE", + expectedResult: true, + description: "Header value is case-insensitive", + }, { name: "bypass_header_false", headerValue: "false", @@ -266,7 +278,7 @@ func TestGovernanceBypassHeader(t *testing.T) { req.Header.Set("x-amz-bypass-governance-retention", tt.headerValue) } - result := req.Header.Get("x-amz-bypass-governance-retention") == "true" + result := governanceBypassRequested(req) if result != tt.expectedResult { t.Errorf("bypass header check = %v, want %v. %s", result, tt.expectedResult, tt.description) @@ -450,7 +462,7 @@ func TestGovernancePermissionHTTPFlow(t *testing.T) { } // Test the header processing logic used in handlers - bypassGovernance := req.Header.Get("x-amz-bypass-governance-retention") == "true" + bypassGovernance := governanceBypassRequested(req) if bypassGovernance != tt.expectedBypassGovernance { t.Errorf("Expected bypassGovernance to be %v, got %v", tt.expectedBypassGovernance, bypassGovernance) @@ -471,7 +483,7 @@ func TestGovernancePermissionMethodCalls(t *testing.T) { // Extract parameters as done in the handler bucket, object := s3_constants.GetBucketAndObject(req) versionId := req.URL.Query().Get("versionId") - bypassGovernance := req.Header.Get("x-amz-bypass-governance-retention") == "true" + bypassGovernance := governanceBypassRequested(req) // Verify the parameters are extracted correctly // Note: The actual bucket and object extraction depends on the URL structure @@ -491,7 +503,7 @@ func TestGovernancePermissionMethodCalls(t *testing.T) { // Extract parameters as done in the handler bucket, object := s3_constants.GetBucketAndObject(req) - bypassGovernance := req.Header.Get("x-amz-bypass-governance-retention") == "true" + bypassGovernance := governanceBypassRequested(req) versioningEnabled := true // Would be determined by isVersioningEnabled(bucket) // Verify the parameters are extracted correctly diff --git a/weed/s3api/s3api_object_handlers_put.go b/weed/s3api/s3api_object_handlers_put.go index 425a26101b..8ecbb04c16 100644 --- a/weed/s3api/s3api_object_handlers_put.go +++ b/weed/s3api/s3api_object_handlers_put.go @@ -2594,11 +2594,8 @@ func (s3a *S3ApiServer) validateObjectLockHeaders(r *http.Request, bucket, objec } } - // Check for governance bypass header - only valid for buckets with Object Lock enabled - bypassGovernance := r.Header.Get("x-amz-bypass-governance-retention") == "true" - // Governance bypass headers are only valid for buckets with Object Lock enabled - if bypassGovernance && !objectLockEnabled { + if governanceBypassRequested(r) && !objectLockEnabled { return ErrGovernanceBypassVersioningRequired } diff --git a/weed/s3api/s3api_object_lock_actions_authz_test.go b/weed/s3api/s3api_object_lock_actions_authz_test.go index a90f7d93ae..c87d8a863d 100644 --- a/weed/s3api/s3api_object_lock_actions_authz_test.go +++ b/weed/s3api/s3api_object_lock_actions_authz_test.go @@ -91,6 +91,38 @@ func TestGovernanceBypassDoesNotInheritDeleteObjectVersion(t *testing.T) { } } +func TestEvaluateGovernanceBypassRequestHeaderCase(t *testing.T) { + iam := &IdentityAccessManagement{} + require.NoError(t, iam.PutPolicy("BypassGovernance", + `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:DeleteObjectVersion","s3:BypassGovernanceRetention"],"Resource":"arn:aws:s3:::worm/*"}]}`)) + s3a := &S3ApiServer{iam: iam} + + for _, tc := range []struct { + header string + want bool + }{ + {"true", true}, + {"True", true}, + {"TRUE", true}, + {"false", false}, + {"", false}, + } { + req := httptest.NewRequest(http.MethodDelete, "http://localhost:8333/worm/doc.txt?versionId=abc123", nil) + req = mux.SetURLVars(req, map[string]string{"bucket": "worm", "object": "doc.txt"}) + if tc.header != "" { + req.Header.Set(s3_constants.AmzBypassGovernanceRetention, tc.header) + } + req = req.WithContext(s3_constants.SetIdentityInContext(req.Context(), &Identity{ + Name: "breaker", + PolicyNames: []string{"BypassGovernance"}, + Account: &Account{Id: "test-account"}, + })) + if got := s3a.evaluateGovernanceBypassRequest(req, "worm", "/doc.txt"); got != tc.want { + t.Errorf("header %q: evaluateGovernanceBypassRequest() = %v, want %v", tc.header, got, tc.want) + } + } +} + // The object-lock request headers on PutObject/CreateMultipartUpload must be // authorized as s3:PutObjectRetention / s3:PutObjectLegalHold; s3:PutObject // alone used to set retention and legal hold on new versions. diff --git a/weed/s3api/s3api_object_lock_headers_test.go b/weed/s3api/s3api_object_lock_headers_test.go index fed8355236..02ab5314f9 100644 --- a/weed/s3api/s3api_object_lock_headers_test.go +++ b/weed/s3api/s3api_object_lock_headers_test.go @@ -520,6 +520,14 @@ func TestValidateObjectLockHeaders(t *testing.T) { assert.True(t, errors.Is(err, ErrGovernanceBypassVersioningRequired)) }) + t.Run("Title-case governance bypass header on non-versioned bucket", func(t *testing.T) { + req := httptest.NewRequest("PUT", "/bucket/object", nil) + req.Header.Set("x-amz-bypass-governance-retention", "True") + + err := s3a.validateObjectLockHeaders(req, "bucket", "object", false) + assert.True(t, errors.Is(err, ErrGovernanceBypassVersioningRequired)) + }) + t.Run("Governance bypass header on versioned bucket should pass", func(t *testing.T) { req := httptest.NewRequest("PUT", "/bucket/object", nil) req.Header.Set("x-amz-bypass-governance-retention", "true") diff --git a/weed/s3api/s3api_object_retention.go b/weed/s3api/s3api_object_retention.go index 6df625ed39..919e121386 100644 --- a/weed/s3api/s3api_object_retention.go +++ b/weed/s3api/s3api_object_retention.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "time" "github.com/seaweedfs/seaweedfs/weed/glog" @@ -543,11 +544,15 @@ func (s3a *S3ApiServer) checkGovernanceBypassPermission(request *http.Request, b return true } +// AWS SDKs send the boolean as "true" or "True". +func governanceBypassRequested(r *http.Request) bool { + return strings.EqualFold(r.Header.Get(s3_constants.AmzBypassGovernanceRetention), "true") +} + // evaluateGovernanceBypassRequest evaluates if governance bypass is requested and permitted func (s3a *S3ApiServer) evaluateGovernanceBypassRequest(r *http.Request, bucket, object string) bool { // Step 1: Check if governance bypass was requested via header - bypassRequested := r.Header.Get("x-amz-bypass-governance-retention") == "true" - if !bypassRequested { + if !governanceBypassRequested(r) { // No bypass requested - normal retention enforcement applies return false }