From ca5c659d9b48909a3c20ca41e1ab16b85dd6a1db Mon Sep 17 00:00:00 2001 From: Tiago Posse Date: Sat, 5 Dec 2026 20:09:21 +0100 Subject: [PATCH] s3api: answer a malformed SSE-KMS key ID with KMS.NotFoundException (#13433) * s3api: answer a malformed SSE-KMS key ID with KMS.NotFoundException PutObject and UploadPart with an SSE-KMS key ID that has spaces or control characters returned 500 InternalError, which clients retry. CopyObject already answers the same ID with KMS.NotFoundException, the error a KMS gives for a key ID it can't parse. PUT and UploadPart now do too. Co-Authored-By: Claude Opus 5.5 * s3api: refuse a malformed SSE-KMS key ID at CreateMultipartUpload AWS answers a key ID it can't use at CreateMultipartUpload. A malformed one was accepted there, so every UploadPart of the upload failed instead. ValidateRequestEncryption now answers it with KMS.NotFoundException, which also covers PutObject before a bucket auto-create. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- weed/s3api/s3_sse_kms_error_codes_test.go | 41 +++++++++++++++++++++++ weed/s3api/s3_sse_kms_utils.go | 8 +++-- weed/s3api/s3api_copy_validation.go | 8 +++-- weed/s3api/s3api_copy_validation_test.go | 2 ++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/weed/s3api/s3_sse_kms_error_codes_test.go b/weed/s3api/s3_sse_kms_error_codes_test.go index 59b63e028e..d8782d98ed 100644 --- a/weed/s3api/s3_sse_kms_error_codes_test.go +++ b/weed/s3api/s3_sse_kms_error_codes_test.go @@ -1,6 +1,7 @@ package s3api import ( + "encoding/base64" "fmt" "net/http" "net/http/httptest" @@ -54,6 +55,46 @@ func TestSSEKMSPutWithAnUnknownKeyIsKMSKeyNotFound(t *testing.T) { } } +func TestSSEKMSPutWithAMalformedKeyIsKMSKeyNotFound(t *testing.T) { + setupKMSWithoutOnDemandKeys(t) + util_http.InitGlobalHttpClient() + filer := &ambiguousPutFiler{volume: startFakeVolumeServer(t), apply: true, entries: map[string]*filer_pb.Entry{}} + s3a := newPutTestServer(t, startFakeFiler(t, filer)) + + for name, baseIV := range map[string]string{ + "single put": "", + "multipart part": base64.StdEncoding.EncodeToString(make([]byte, s3_constants.AESBlockSize)), + } { + for _, keyID := range []string{"bad key", " k", "k\t"} { + r := httptest.NewRequest(http.MethodPut, "/b/o", nil) + r.Header.Set(s3_constants.AmzServerSideEncryption, "aws:kms") + r.Header.Set(s3_constants.AmzServerSideEncryptionAwsKmsKeyId, keyID) + if baseIV != "" { + r.Header.Set(s3_constants.SeaweedFSSSEKMSBaseIVHeader, baseIV) + } + r = mux.SetURLVars(r, map[string]string{"bucket": "b", "object": "o"}) + _, code, _ := s3a.putToFiler(r, "/buckets/b/o", strings.NewReader("data"), "b", "o", 1, 0, nil, false, "") + require.Equal(t, s3err.ErrKMSKeyNotFound, code, "%s with key ID %q", name, keyID) + } + } +} + +func TestSSEKMSCreateMultipartUploadWithAMalformedKeyIsKMSKeyNotFound(t *testing.T) { + s3a, filer := newMultipartInitiateSSETestServer(t, nil) + rr := initiateMultipartWithHeaders(s3a, map[string]string{ + s3_constants.AmzServerSideEncryption: "aws:kms", + s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "bad key", + }) + require.Equal(t, http.StatusBadRequest, rr.Code, rr.Body.String()) + require.Contains(t, rr.Body.String(), "KMS.NotFoundException") + + filer.mu.Lock() + defer filer.mu.Unlock() + for path := range filer.entries { + require.NotContains(t, path, s3_constants.MultipartUploadsFolder, "upload %s was created", path) + } +} + func TestSSEKMSCopyWithAnUnknownKeyIsKMSKeyNotFound(t *testing.T) { setupKMSWithoutOnDemandKeys(t) util_http.InitGlobalHttpClient() diff --git a/weed/s3api/s3_sse_kms_utils.go b/weed/s3api/s3_sse_kms_utils.go index 2bd7396f9f..9f43aee262 100644 --- a/weed/s3api/s3_sse_kms_utils.go +++ b/weed/s3api/s3_sse_kms_utils.go @@ -40,9 +40,13 @@ func validateKMSEncryptionContext(encryptionContext map[string]string) error { // generateKMSDataKey generates a new data encryption key using KMS // This function encapsulates the common pattern used across all SSE-KMS functions func generateKMSDataKey(keyID string, encryptionContext map[string]string) (*KMSDataKeyResult, error) { - // Validate keyID to prevent injection attacks and malformed requests to KMS service + // Validate keyID to prevent injection attacks and malformed requests to KMS service. + // A KMS answers an ID it can't parse as not found, and so does CopyObject. if !isValidKMSKeyID(keyID) { - return nil, fmt.Errorf("invalid KMS key ID format: key ID must be non-empty, without spaces or control characters") + return nil, &kms.KMSError{ + Code: kms.ErrCodeNotFoundException, + Message: "invalid KMS key ID format: key ID must be non-empty, without spaces or control characters", + } } if err := validateKMSEncryptionContext(encryptionContext); err != nil { diff --git a/weed/s3api/s3api_copy_validation.go b/weed/s3api/s3api_copy_validation.go index 430be30683..32f7399024 100644 --- a/weed/s3api/s3api_copy_validation.go +++ b/weed/s3api/s3api_copy_validation.go @@ -181,14 +181,18 @@ func validateEncryptionCompatibility(headers http.Header) error { // ValidateRequestEncryption rejects PutObject or CreateMultipartUpload // encryption headers that no single method can honor, with the InvalidArgument -// codes S3 returns for them. Under fips140=only it also refuses SSE-C, -// including the CopyObject copy-source headers. +// codes S3 returns for them, and a malformed SSE-KMS key ID with +// KMS.NotFoundException. Under fips140=only it also refuses SSE-C, including +// the CopyObject copy-source headers. func ValidateRequestEncryption(headers http.Header) s3err.ErrorCode { if fips140Enforced() && (hasSSECHeaders(headers) || hasSSECCopyHeaders(headers)) { return s3err.ErrNotImplemented } err := validateEncryptionCompatibility(headers) if err == nil { + if keyID := headers.Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId); keyID != "" && !isValidKMSKeyID(keyID) { + return s3err.ErrKMSKeyNotFound + } return s3err.ErrNone } var validationErr *CopyValidationError diff --git a/weed/s3api/s3api_copy_validation_test.go b/weed/s3api/s3api_copy_validation_test.go index 00edd60101..d6fc3cb5c6 100644 --- a/weed/s3api/s3api_copy_validation_test.go +++ b/weed/s3api/s3api_copy_validation_test.go @@ -47,6 +47,8 @@ func TestValidateRequestEncryption(t *testing.T) { {name: "SSE-S3 with KMS key id", sse: s3_constants.SSEAlgorithmAES256, headers: map[string]string{s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "key-id"}, want: s3err.ErrIncompatibleEncryptionMethod}, {name: "KMS key id without method", headers: map[string]string{s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "key-id"}, want: s3err.ErrIncompatibleEncryptionMethod}, {name: "KMS key id with aws:kms", sse: s3_constants.SSEAlgorithmKMS, headers: map[string]string{s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "key-id"}, want: s3err.ErrNone}, + {name: "malformed KMS key id", sse: s3_constants.SSEAlgorithmKMS, headers: map[string]string{s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "bad key"}, want: s3err.ErrKMSKeyNotFound}, + {name: "KMS key id with a control character", sse: s3_constants.SSEAlgorithmKMS, headers: map[string]string{s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "k\t"}, want: s3err.ErrKMSKeyNotFound}, } for _, tc := range testCases {