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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-04 04:20:53 -08:00
1 parent 1f9c056822
commit b26e9fb954
2 files changed
+18 -4

No files matched your search

@@ -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)
}
+6 -2
View File
@@ -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