s3api: keep the derived aws:s3:arn in a CopyObject SSE-KMS context

ParseSSEKMSCopyHeaders took the destination context header verbatim, so
a copy could carry no aws:s3:arn or name another bucket's ARN, and a KMS
key policy conditioned on aws:s3:arn could be met that way. Without the
header, the default context was built from the filer path, which made
the ARN arn:aws:s3:::b//buckets/b/key.

Build the destination context the way PutObject does: the derived ARN
of the destination object plus the client's extra keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 04:35:24 -08:00
1 parent acc22a4270
commit 1cc59a46c6
2 files changed
+105 -12

No files matched your search

+6 -12
View File
@@ -781,23 +781,17 @@ func ParseSSEKMSCopyHeaders(r *http.Request) (destKeyID string, encryptionContex
return "", nil, false, fmt.Errorf("invalid KMS key ID: %s", destKeyID)
}
// Parse encryption context if provided
if contextHeader := r.Header.Get(s3_constants.AmzServerSideEncryptionContext); contextHeader != "" {
contextBytes, decodeErr := base64.StdEncoding.DecodeString(contextHeader)
if decodeErr != nil {
return "", nil, false, fmt.Errorf("invalid encryption context encoding: %v", decodeErr)
}
if unmarshalErr := json.Unmarshal(contextBytes, &encryptionContext); unmarshalErr != nil {
return "", nil, false, fmt.Errorf("invalid encryption context JSON: %v", unmarshalErr)
}
}
// Parse bucket key enabled flag
if bucketKeyHeader := r.Header.Get(s3_constants.AmzServerSideEncryptionBucketKeyEnabled); bucketKeyHeader != "" {
bucketKeyEnabled = strings.ToLower(bucketKeyHeader) == "true"
}
dstBucket, dstObject := s3_constants.GetBucketAndObject(r)
encryptionContext, err = buildRequestEncryptionContext(dstBucket, dstObject, bucketKeyEnabled, r.Header.Get(s3_constants.AmzServerSideEncryptionContext))
if err != nil {
return "", nil, false, err
}
return destKeyID, encryptionContext, bucketKeyEnabled, nil
}
@@ -0,0 +1,99 @@
package s3api
import (
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
"github.com/stretchr/testify/require"
)
func newCopyKMSContextTestServer(t *testing.T, source *filer_pb.Entry, bucketEncryption *s3_pb.EncryptionConfiguration) (*S3ApiServer, *copyACLFiler) {
t.Helper()
util_http.InitGlobalHttpClient()
volume := startFakeVolumeServer(t)
for _, chunk := range source.GetChunks() {
volume.stored[chunk.GetFileIdString()] = []byte("data")
}
filer := &copyACLFiler{ambiguousPutFiler: &ambiguousPutFiler{volume: volume, apply: true, entries: map[string]*filer_pb.Entry{}}}
s3a := newPutTestServer(t, startFakeFiler(t, filer))
s3a.iam = NewIdentityAccessManagementWithStore(s3a.option, nil, "memory")
t.Cleanup(s3a.iam.Shutdown)
s3a.bucketConfigCache.Set("b", &BucketConfig{Name: "b", Encryption: bucketEncryption})
filer.entries["/buckets/b"] = &filer_pb.Entry{Name: "b", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}}
filer.entries["/buckets/b/source"] = source
return s3a, filer
}
func copyToDestination(s3a *S3ApiServer, headers map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPut, "http://s3/b/destination", nil)
req = mux.SetURLVars(req, map[string]string{"bucket": "b", "object": "destination"})
req.Header.Set("X-Amz-Copy-Source", "/b/source")
for k, v := range headers {
req.Header.Set(k, v)
}
rr := httptest.NewRecorder()
s3a.CopyObjectHandler(rr, req)
return rr
}
func TestCopyObjectSSEKMSContextKeepsTheDestinationARN(t *testing.T) {
kmsKey := SetupTestKMS(t)
t.Cleanup(kmsKey.Cleanup)
attributes := func() *filer_pb.FuseAttributes {
return &filer_pb.FuseAttributes{FileMode: defaultFileMode, FileSize: 4, Mime: "text/plain"}
}
chunked := func() *filer_pb.Entry {
return &filer_pb.Entry{Name: "source", Attributes: attributes(),
Chunks: []*filer_pb.FileChunk{{FileId: fmt.Sprintf("3,%016x%08x", 100, 100), Size: 4}}}
}
explicit := map[string]string{
s3_constants.AmzServerSideEncryption: "aws:kms",
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID,
s3_constants.AmzServerSideEncryptionContext: base64.StdEncoding.EncodeToString([]byte(`{"aws:s3:arn":"arn:aws:s3:::other/o","team":"a"}`)),
}
for name, tc := range map[string]struct {
source *filer_pb.Entry
headers map[string]string
bucketEncryption *s3_pb.EncryptionConfiguration
want map[string]string
}{
"chunked source": {
source: chunked(),
headers: explicit,
want: map[string]string{"aws:s3:arn": "arn:aws:s3:::b/destination", "team": "a"},
},
"inline source": {
source: &filer_pb.Entry{Name: "source", Attributes: attributes(), Content: []byte("data")},
headers: explicit,
want: map[string]string{"aws:s3:arn": "arn:aws:s3:::b/destination", "team": "a"},
},
"bucket default": {
source: chunked(),
bucketEncryption: &s3_pb.EncryptionConfiguration{SseAlgorithm: "aws:kms", KmsKeyId: kmsKey.KeyID},
want: map[string]string{"aws:s3:arn": "arn:aws:s3:::b/destination"},
},
} {
t.Run(name, func(t *testing.T) {
s3a, filer := newCopyKMSContextTestServer(t, tc.source, tc.bucketEncryption)
rr := copyToDestination(s3a, tc.headers)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
filer.mu.Lock()
stored := filer.entries["/buckets/b/destination"]
filer.mu.Unlock()
require.NotNil(t, stored)
sseKey, err := DeserializeSSEKMSMetadata(stored.Extended[s3_constants.SeaweedFSSSEKMSKey])
require.NoError(t, err)
require.Equal(t, kmsKey.KeyID, sseKey.KeyID)
require.Equal(t, tc.want, sseKey.EncryptionContext)
})
}
}