From b26e9fb954e22794e8bb74373606220fe3df0940 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 04:20:53 -0800 Subject: [PATCH] s3api: keep the derived aws:s3:arn in a client's KMS encryption context A context header naming aws:s3:arn replaced the object or bucket ARN sent to the key service, on PutObject and, through the context stored at initiate, on every multipart part, so a key policy conditioned on aws:s3:arn could be met with another bucket's ARN. Co-Authored-By: Claude Opus 5.5 --- weed/s3api/filer_multipart_initiate_sse_test.go | 14 ++++++++++++-- weed/s3api/s3_sse_kms.go | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/weed/s3api/filer_multipart_initiate_sse_test.go b/weed/s3api/filer_multipart_initiate_sse_test.go index 82ffc8001a..d1b0a3087c 100644 --- a/weed/s3api/filer_multipart_initiate_sse_test.go +++ b/weed/s3api/filer_multipart_initiate_sse_test.go @@ -238,7 +238,7 @@ func TestMultipartPartsUseInitiateEncryptionContext(t *testing.T) { rr := initiateMultipartWithHeaders(s3a, map[string]string{ s3_constants.AmzServerSideEncryption: "aws:kms", s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID, - s3_constants.AmzServerSideEncryptionContext: base64.StdEncoding.EncodeToString([]byte(`{"department":"finance"}`)), + s3_constants.AmzServerSideEncryptionContext: base64.StdEncoding.EncodeToString([]byte(`{"department":"finance","aws:s3:arn":"arn:aws:s3:::other/o"}`)), }) require.Equal(t, http.StatusOK, rr.Code, rr.Body.String()) var initiated InitiateMultipartUploadResult @@ -263,7 +263,7 @@ func TestMultipartPartsUseInitiateEncryptionContext(t *testing.T) { sseKey, err := DeserializeSSEKMSMetadata(part.Extended[s3_constants.SeaweedFSSSEKMSKey]) require.NoError(t, err) require.Equal(t, "finance", sseKey.EncryptionContext["department"]) - require.Contains(t, sseKey.EncryptionContext, "aws:s3:arn") + require.Equal(t, "arn:aws:s3:::b/o", sseKey.EncryptionContext["aws:s3:arn"]) } func TestUploadPartCopyUsesInitiateEncryptionContext(t *testing.T) { @@ -291,3 +291,13 @@ func TestHandleSSEKMSEncryptionRejectsContextTheKeyServiceRefuses(t *testing.T) _, _, _, errCode := (&S3ApiServer{}).handleSSEKMSEncryption(req, strings.NewReader("data"), 0) require.Equal(t, s3err.ErrInvalidRequest, errCode) } + +func TestBuildRequestEncryptionContextKeepsDerivedARN(t *testing.T) { + header := base64.StdEncoding.EncodeToString([]byte(`{"aws:s3:arn":"arn:aws:s3:::other","team":"a"}`)) + encryptionContext, err := buildRequestEncryptionContext("b", "o", false, header) + require.NoError(t, err) + require.Equal(t, map[string]string{"aws:s3:arn": "arn:aws:s3:::b/o", "team": "a"}, encryptionContext) + encryptionContext, err = buildRequestEncryptionContext("b", "o", true, header) + require.NoError(t, err) + require.Equal(t, map[string]string{"aws:s3:arn": "arn:aws:s3:::b", "team": "a"}, encryptionContext) +} diff --git a/weed/s3api/s3_sse_kms.go b/weed/s3api/s3_sse_kms.go index 43cbae90c7..ae2ced5728 100644 --- a/weed/s3api/s3_sse_kms.go +++ b/weed/s3api/s3_sse_kms.go @@ -419,8 +419,10 @@ func BuildEncryptionContext(bucketName, objectKey string, useBucketKey bool) map return kms.BuildS3EncryptionContext(bucketName, objectKey, useBucketKey) } -// buildRequestEncryptionContext merges the request's context header over the +// buildRequestEncryptionContext adds the request's context header to the // object's default context and applies the checks the KMS call would apply. +// The derived aws:s3:arn is kept so a key policy scoped by it cannot be met +// with another bucket's ARN. func buildRequestEncryptionContext(bucketName, objectKey string, useBucketKey bool, contextHeader string) (map[string]string, error) { userContext, err := parseEncryptionContext(contextHeader) if err != nil { @@ -428,7 +430,9 @@ func buildRequestEncryptionContext(bucketName, objectKey string, useBucketKey bo } encryptionContext := BuildEncryptionContext(bucketName, objectKey, useBucketKey) for k, v := range userContext { - encryptionContext[k] = v + if _, derived := encryptionContext[k]; !derived { + encryptionContext[k] = v + } } if err := validateKMSEncryptionContext(encryptionContext); err != nil { return nil, err