From 5a779f54405a4c64f7cb8f8b56ea482234fe7534 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 03:37:55 -0800 Subject: [PATCH] s3api: validate the SSE-C key and KMS context on CreateMultipartUpload Initiate accepted an SSE-C key whose MD5 did not match, an incomplete SSE-C header set, and a KMS encryption context that was not base64 JSON, then created the upload. Reject them before creating the upload with the same codes PutObject returns, so the headers the response echoes are ones the parts can use. Co-Authored-By: Claude Opus 5.5 --- .../filer_multipart_initiate_sse_test.go | 50 +++++++++++++++++++ weed/s3api/s3api_object_handlers_multipart.go | 8 +++ 2 files changed, 58 insertions(+) diff --git a/weed/s3api/filer_multipart_initiate_sse_test.go b/weed/s3api/filer_multipart_initiate_sse_test.go index ab1b776a39..e4653ab23a 100644 --- a/weed/s3api/filer_multipart_initiate_sse_test.go +++ b/weed/s3api/filer_multipart_initiate_sse_test.go @@ -153,3 +153,53 @@ func TestNewMultipartUploadSSEHeadersMatchUploadEntry(t *testing.T) { require.Equal(t, string(uploadEntry.Extended[s3_constants.SeaweedFSSSEKMSKeyID]), rr.Header().Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId)) require.Empty(t, rr.Header().Get(s3_constants.AmzServerSideEncryptionBucketKeyEnabled)) } + +func TestNewMultipartUploadRejectsInvalidSSEHeaders(t *testing.T) { + customerKey := make([]byte, 32) + otherSum := md5.Sum([]byte("a different key")) + + tests := []struct { + name string + request map[string]string + code string + }{ + { + name: "SSE-C key MD5 mismatch", + request: map[string]string{ + s3_constants.AmzServerSideEncryptionCustomerAlgorithm: "AES256", + s3_constants.AmzServerSideEncryptionCustomerKey: base64.StdEncoding.EncodeToString(customerKey), + s3_constants.AmzServerSideEncryptionCustomerKeyMD5: base64.StdEncoding.EncodeToString(otherSum[:]), + }, + code: "InvalidArgument", + }, + { + name: "SSE-C key missing", + request: map[string]string{ + s3_constants.AmzServerSideEncryptionCustomerAlgorithm: "AES256", + }, + code: "InvalidRequest", + }, + { + name: "KMS context not base64 JSON", + request: map[string]string{ + s3_constants.AmzServerSideEncryption: "aws:kms", + s3_constants.AmzServerSideEncryptionContext: "not-json", + }, + code: "InvalidRequest", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + s3a, f := newMultipartInitiateSSETestServer(t, nil) + rr := initiateMultipartWithHeaders(s3a, tc.request) + require.Equal(t, http.StatusBadRequest, rr.Code, rr.Body.String()) + require.Contains(t, rr.Body.String(), ""+tc.code+"") + f.mu.Lock() + defer f.mu.Unlock() + for path := range f.entries { + require.False(t, strings.HasPrefix(path, "/buckets/b/"+s3_constants.MultipartUploadsFolder+"/"), path) + } + }) + } +} diff --git a/weed/s3api/s3api_object_handlers_multipart.go b/weed/s3api/s3api_object_handlers_multipart.go index 78c4325898..7bceeb5ac3 100644 --- a/weed/s3api/s3api_object_handlers_multipart.go +++ b/weed/s3api/s3api_object_handlers_multipart.go @@ -46,6 +46,14 @@ func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http s3err.WriteErrorResponse(w, r, errCode) return } + if _, err := ParseSSECHeaders(r); err != nil { + s3err.WriteErrorResponse(w, r, MapSSECErrorToS3Error(err)) + return + } + if _, err := parseEncryptionContext(r.Header.Get(s3_constants.AmzServerSideEncryptionContext)); err != nil { + s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest) + return + } // Check if bucket exists, and create it if it doesn't (auto-create bucket) if err := s3a.checkBucket(r, bucket); err == s3err.ErrNoSuchBucket {