s3api: answer InvalidRequest when the destination KMS context is refused

The derived aws:s3:arn of a destination key with a control character
fails the KMS context checks, and the copy answered InternalError.
Return it as a CopyValidationError and let the copy error mapping honor
that type, so the client gets InvalidRequest as PutObject does.

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:47:10 -08:00
1 parent f48a01929a
commit da5ef3d1e8
3 files changed
+22 -12

No files matched your search

+1 -1
View File
@@ -789,7 +789,7 @@ func ParseSSEKMSCopyHeaders(r *http.Request) (destKeyID string, encryptionContex
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 "", nil, false, &CopyValidationError{Code: s3err.ErrInvalidRequest, Message: err.Error()}
}
return destKeyID, encryptionContext, bucketKeyEnabled, nil
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gorilla/mux"
@@ -32,9 +33,9 @@ func newCopyKMSContextTestServer(t *testing.T, source *filer_pb.Entry, bucketEnc
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"})
func copyToDestination(s3a *S3ApiServer, destination string, headers map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPut, "http://s3/b/"+url.PathEscape(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)
@@ -83,7 +84,7 @@ func TestCopyObjectSSEKMSContextKeepsTheDestinationARN(t *testing.T) {
} {
t.Run(name, func(t *testing.T) {
s3a, filer := newCopyKMSContextTestServer(t, tc.source, tc.bucketEncryption)
rr := copyToDestination(s3a, tc.headers)
rr := copyToDestination(s3a, "destination", tc.headers)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
filer.mu.Lock()
@@ -101,24 +102,28 @@ func TestCopyObjectSSEKMSContextKeepsTheDestinationARN(t *testing.T) {
func TestCopyObjectRejectsAContextTheKeyServiceRefuses(t *testing.T) {
kmsKey := SetupTestKMS(t)
t.Cleanup(kmsKey.Cleanup)
for name, contextHeader := range map[string]string{
"not base64": "not base64!",
"not JSON": base64.StdEncoding.EncodeToString([]byte("team=a")),
"control character": base64.StdEncoding.EncodeToString([]byte(`{"team":"a\tb"}`)),
for name, tc := range map[string]struct {
destination string
contextHeader string
}{
"not base64": {"destination", "not base64!"},
"not JSON": {"destination", base64.StdEncoding.EncodeToString([]byte("team=a"))},
"control character": {"destination", base64.StdEncoding.EncodeToString([]byte(`{"team":"a\tb"}`))},
"control character in the destination key": {"dst\tkey", base64.StdEncoding.EncodeToString([]byte(`{"team":"a"}`))},
} {
t.Run(name, func(t *testing.T) {
s3a, filer := newCopyKMSContextTestServer(t, &filer_pb.Entry{Name: "source", Content: []byte("data"),
Attributes: &filer_pb.FuseAttributes{FileMode: defaultFileMode, FileSize: 4, Mime: "text/plain"}}, nil)
rr := copyToDestination(s3a, map[string]string{
rr := copyToDestination(s3a, tc.destination, map[string]string{
s3_constants.AmzServerSideEncryption: "aws:kms",
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID,
s3_constants.AmzServerSideEncryptionContext: contextHeader,
s3_constants.AmzServerSideEncryptionContext: tc.contextHeader,
})
require.Equal(t, http.StatusBadRequest, rr.Code, rr.Body.String())
require.Contains(t, rr.Body.String(), "InvalidRequest")
filer.mu.Lock()
defer filer.mu.Unlock()
require.NotContains(t, filer.entries, "/buckets/b/destination")
require.NotContains(t, filer.entries, "/buckets/b/"+tc.destination)
})
}
}
@@ -99,6 +99,11 @@ func (s3a *S3ApiServer) mapCopyErrorToS3Error(err error) s3err.ErrorCode {
return ssecErr
}
var validationErr *CopyValidationError
if errors.As(err, &validationErr) {
return validationErr.Code
}
// Default to internal error for unknown errors
return s3err.ErrInternalError
}