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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-12-05 11:09:21 -08:00
1 parent 90aae21e6b
commit ca5c659d9b
4 files changed
+55 -4

No files matched your search

+41
View File
@@ -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()
+6 -2
View File
@@ -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 {
+6 -2
View File
@@ -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
+2
View File
@@ -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 {