fix(s3): preserve SSE-KMS key ID metadata (#2145)

* fix(s3): preserve SSE-KMS key ID metadata

* fix(s3): validate and copy SSE-KMS key metadata

* fix(s3): return SSE headers on CopyObject and UploadPartCopy responses

Both handleCopyObject and handleUploadPartCopy stored the destination's
server-side-encryption settings correctly but never echoed
x-amz-server-side-encryption / x-amz-server-side-encryption-aws-kms-key-id
on their own response — only a follow-up GET/HEAD on the copy revealed it,
which is why the existing SSE tests (checking only via HEAD) never caught
this. Real S3 returns both headers on CopyObjectResult and CopyPartResult.

UploadPartCopy required threading the KMS key id through
CreateMultipartUpload too: MultipartUpload had no field for it, so a part's
destination upload had nowhere to read it back from.

* fix(s3): drop em dashes from merged UploadPartCopy comments

AGENTS.md rules out em dashes anywhere in content. The two comments moved
during the origin/main downmerge (handleUploadPartCopy's SSE-settings note
and the relocated uploadPartCopyPreservesDestinationServerSideEncryption
test) carried one each. Reworded to a period and a comma respectively.

---------

Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
fracermaandPriamo Germosen authored and GitHub committed 2026-09-09 16:03:24 -04:00
1 parent 974d357f88
commit d466ffe099
9 files changed
+265 -5

No files matched your search

@@ -804,6 +804,7 @@ public class S3Controller {
String contentDisposition = httpHeaders.getHeaderString("Content-Disposition");
String cacheControl = httpHeaders.getHeaderString("Cache-Control");
String serverSideEncryption = httpHeaders.getHeaderString("x-amz-server-side-encryption");
String sseKmsKeyId = httpHeaders.getHeaderString("x-amz-server-side-encryption-aws-kms-key-id");
String sseCustomerAlgorithm = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm");
String sseCustomerKey = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key");
String sseCustomerKeyMd5 = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5");
@@ -819,6 +820,7 @@ public class S3Controller {
.withContentDisposition(contentDisposition)
.withCacheControl(cacheControl)
.withServerSideEncryption(serverSideEncryption)
.withSseKmsKeyId(sseKmsKeyId)
.withSseCustomerAlgorithm(sseCustomerAlgorithm)
.withSseCustomerKey(sseCustomerKey)
.withSseCustomerKeyMd5(sseCustomerKeyMd5)
@@ -1325,6 +1327,7 @@ public class S3Controller {
httpHeaders.getHeaderString("Content-Disposition"),
httpHeaders.getHeaderString("x-amz-server-side-encryption"),
httpHeaders.getHeaderString("x-amz-acl"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-aws-kms-key-id"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"),
@@ -1395,6 +1398,12 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
if (obj.getServerSideEncryption() != null) {
resp.header("x-amz-server-side-encryption", obj.getServerSideEncryption());
}
if (obj.getSseKmsKeyId() != null) {
resp.header("x-amz-server-side-encryption-aws-kms-key-id", obj.getSseKmsKeyId());
}
appendSseCustomerHeaders(resp, obj);
return resp.build();
}
@@ -2223,6 +2232,9 @@ public class S3Controller {
if (obj.getServerSideEncryption() != null) {
resp.header("x-amz-server-side-encryption", obj.getServerSideEncryption());
}
if (obj.getSseKmsKeyId() != null) {
resp.header("x-amz-server-side-encryption-aws-kms-key-id", obj.getSseKmsKeyId());
}
appendSseCustomerHeaders(resp, obj);
appendChecksumHeaders(resp, obj.getChecksum());
appendLockHeaders(resp, obj);
@@ -2251,6 +2263,9 @@ public class S3Controller {
if (obj.getServerSideEncryption() != null) {
resp.header("x-amz-server-side-encryption", obj.getServerSideEncryption());
}
if (obj.getSseKmsKeyId() != null) {
resp.header("x-amz-server-side-encryption-aws-kms-key-id", obj.getSseKmsKeyId());
}
appendSseCustomerHeaders(resp, obj);
if (overrides.contentLanguage() != null) {
resp.header("Content-Language", overrides.contentLanguage());
@@ -2311,6 +2326,7 @@ public class S3Controller {
String copyContentDisposition = httpHeaders.getHeaderString("Content-Disposition");
String copyCacheControl = httpHeaders.getHeaderString("Cache-Control");
String copyServerSideEncryption = httpHeaders.getHeaderString("x-amz-server-side-encryption");
String copySseKmsKeyId = httpHeaders.getHeaderString("x-amz-server-side-encryption-aws-kms-key-id");
String cannedAcl = httpHeaders.getHeaderString("x-amz-acl");
String taggingDirective = httpHeaders.getHeaderString("x-amz-tagging-directive");
String taggingHeader = httpHeaders.getHeaderString("x-amz-tagging");
@@ -2330,6 +2346,7 @@ public class S3Controller {
.withContentDisposition(copyContentDisposition)
.withCacheControl(copyCacheControl)
.withServerSideEncryption(copyServerSideEncryption)
.withSseKmsKeyId(copySseKmsKeyId)
.withSseCustomerAlgorithm(httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"))
.withSseCustomerKey(httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"))
.withSseCustomerKeyMd5(httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"))
@@ -2351,6 +2368,12 @@ public class S3Controller {
appendChecksumElements(xmlBuilder, copy.getChecksum());
String xml = xmlBuilder.end("CopyObjectResult").build();
Response.ResponseBuilder response = Response.ok(xml).type(MediaType.APPLICATION_XML);
if (copy.getServerSideEncryption() != null) {
response.header("x-amz-server-side-encryption", copy.getServerSideEncryption());
}
if (copy.getSseKmsKeyId() != null) {
response.header("x-amz-server-side-encryption-aws-kms-key-id", copy.getSseKmsKeyId());
}
appendSseCustomerHeaders(response, copy);
return response.build();
}
@@ -2369,6 +2392,11 @@ public class S3Controller {
sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange,
copySourceSseCustomerHeaders(httpHeaders),
sseCustomerHeaders(httpHeaders));
// The destination multipart upload's own SSE settings (captured at
// CreateMultipartUpload), not anything from this request's headers.
// UploadPartCopy doesn't take server-side-encryption headers itself,
// parts always inherit the upload they belong to.
MultipartUpload destinationUpload = s3Service.listParts(destBucket, destKey, uploadId);
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("CopyPartResult", AwsNamespaces.S3)
@@ -2377,6 +2405,12 @@ public class S3Controller {
.end("CopyPartResult")
.build();
Response.ResponseBuilder response = Response.ok(xml).type(MediaType.APPLICATION_XML);
if (destinationUpload.getServerSideEncryption() != null) {
response.header("x-amz-server-side-encryption", destinationUpload.getServerSideEncryption());
}
if (destinationUpload.getSseKmsKeyId() != null) {
response.header("x-amz-server-side-encryption-aws-kms-key-id", destinationUpload.getSseKmsKeyId());
}
appendSseCustomerHeaders(response,
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
@@ -467,6 +467,9 @@ public class S3Service implements Resettable, ResourceProvider {
object.setContentDisposition(effectiveOptions.getContentDisposition());
object.setCacheControl(effectiveOptions.getCacheControl());
object.setServerSideEncryption(normalizedServerSideEncryption);
object.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption)
? effectiveOptions.getSseKmsKeyId()
: null);
if (sseCustomerKey != null) {
object.setSseCustomerAlgorithm(sseCustomerKey.algorithm());
object.setSseCustomerKeyMd5(sseCustomerKey.keyMd5());
@@ -2306,7 +2309,7 @@ public class S3Service implements Resettable, ResourceProvider {
Map<String, String> metadata, String storageClass,
String contentDisposition, String serverSideEncryption, String acl) {
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
serverSideEncryption, acl, null, null, null, null);
serverSideEncryption, acl, null, null, null, null, null, null, null);
}
public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,
@@ -2315,8 +2318,8 @@ public class S3Service implements Resettable, ResourceProvider {
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
String checksumAlgorithm) {
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
serverSideEncryption, acl, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
checksumAlgorithm, null);
serverSideEncryption, acl, null, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
checksumAlgorithm, null, null);
}
public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,
@@ -2325,7 +2328,7 @@ public class S3Service implements Resettable, ResourceProvider {
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
String checksumAlgorithm, Map<String, String> tagging) {
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
serverSideEncryption, acl, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
serverSideEncryption, acl, null, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
checksumAlgorithm, null, tagging);
}
@@ -2335,6 +2338,18 @@ public class S3Service implements Resettable, ResourceProvider {
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
String checksumAlgorithm, String checksumType,
Map<String, String> tagging) {
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
serverSideEncryption, acl, null, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
checksumAlgorithm, checksumType, tagging);
}
public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,
Map<String, String> metadata, String storageClass,
String contentDisposition, String serverSideEncryption, String acl,
String sseKmsKeyId,
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
String checksumAlgorithm, String checksumType,
Map<String, String> tagging) {
ensureBucketExists(bucket);
if (acl != null && !acl.isBlank()) {
cannedObjectAclXml(acl);
@@ -2349,6 +2364,7 @@ public class S3Service implements Resettable, ResourceProvider {
upload.setStorageClass(ObjectAttributeName.normalizeStorageClass(storageClass));
upload.setContentDisposition(contentDisposition);
upload.setServerSideEncryption(normalizedServerSideEncryption);
upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null);
if (customerKey != null) {
upload.setSseCustomerAlgorithm(customerKey.algorithm());
upload.setSseCustomerKeyMd5(customerKey.keyMd5());
@@ -2523,6 +2539,7 @@ public class S3Service implements Resettable, ResourceProvider {
.withStorageClass(upload.getStorageClass())
.withContentDisposition(upload.getContentDisposition())
.withServerSideEncryption(upload.getServerSideEncryption())
.withSseKmsKeyId(upload.getSseKmsKeyId())
.withAcl(upload.getAcl())
.withTagging(upload.getTagging()));
if (upload.getSseCustomerAlgorithm() != null) {
@@ -3643,6 +3660,7 @@ public class S3Service implements Resettable, ResourceProvider {
copy.setContentDisposition(source.getContentDisposition());
copy.setCacheControl(source.getCacheControl());
copy.setServerSideEncryption(source.getServerSideEncryption());
copy.setSseKmsKeyId(source.getSseKmsKeyId());
copy.setSseCustomerAlgorithm(source.getSseCustomerAlgorithm());
copy.setSseCustomerKeyMd5(source.getSseCustomerKeyMd5());
copy.setSize(source.getSize());
@@ -4078,6 +4096,11 @@ public class S3Service implements Resettable, ResourceProvider {
String effectiveServerSideEncryption = destinationCustomerKey != null
? null
: (normalizedServerSideEncryption != null ? normalizedServerSideEncryption : source.getServerSideEncryption());
String effectiveSseKmsKeyId = "aws:kms".equals(effectiveServerSideEncryption)
? (normalizedServerSideEncryption != null
? effectiveOptions.getSseKmsKeyId()
: source.getSseKmsKeyId())
: null;
boolean replaceTags = "REPLACE".equalsIgnoreCase(effectiveOptions.getTaggingDirective());
Map<String, String> effectiveTags = replaceTags
? effectiveOptions.getReplacementTagging()
@@ -4103,6 +4126,7 @@ public class S3Service implements Resettable, ResourceProvider {
.withContentDisposition(effectiveContentDisposition)
.withCacheControl(effectiveCacheControl)
.withServerSideEncryption(effectiveServerSideEncryption)
.withSseKmsKeyId(effectiveSseKmsKeyId)
.withSseCustomerAlgorithm(effectiveOptions.getSseCustomerAlgorithm())
.withSseCustomerKey(effectiveOptions.getSseCustomerKey())
.withSseCustomerKeyMd5(effectiveOptions.getSseCustomerKeyMd5())
@@ -13,6 +13,7 @@ public class CopyObjectOptions {
private String contentDisposition;
private String cacheControl;
private String serverSideEncryption;
private String sseKmsKeyId;
private String sseCustomerAlgorithm;
private String sseCustomerKey;
private String sseCustomerKeyMd5;
@@ -57,6 +58,9 @@ public class CopyObjectOptions {
public String getServerSideEncryption() { return serverSideEncryption; }
public CopyObjectOptions withServerSideEncryption(String serverSideEncryption) { this.serverSideEncryption = serverSideEncryption; return this; }
public String getSseKmsKeyId() { return sseKmsKeyId; }
public CopyObjectOptions withSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; return this; }
public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; }
public CopyObjectOptions withSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; return this; }
@@ -18,6 +18,7 @@ public class MultipartUpload {
private String storageClass;
private String contentDisposition;
private String serverSideEncryption;
private String sseKmsKeyId;
private String sseCustomerAlgorithm;
private String sseCustomerKeyMd5;
private String acl;
@@ -64,6 +65,9 @@ public class MultipartUpload {
public String getServerSideEncryption() { return serverSideEncryption; }
public void setServerSideEncryption(String serverSideEncryption) { this.serverSideEncryption = serverSideEncryption; }
public String getSseKmsKeyId() { return sseKmsKeyId; }
public void setSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; }
public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; }
public void setSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; }
@@ -12,6 +12,7 @@ public class PutObjectOptions {
private String contentDisposition;
private String cacheControl;
private String serverSideEncryption;
private String sseKmsKeyId;
private String sseCustomerAlgorithm;
private String sseCustomerKey;
private String sseCustomerKeyMd5;
@@ -50,6 +51,9 @@ public class PutObjectOptions {
public String getServerSideEncryption() { return serverSideEncryption; }
public PutObjectOptions withServerSideEncryption(String serverSideEncryption) { this.serverSideEncryption = serverSideEncryption; return this; }
public String getSseKmsKeyId() { return sseKmsKeyId; }
public PutObjectOptions withSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; return this; }
public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; }
public PutObjectOptions withSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; return this; }
@@ -25,6 +25,7 @@ public class S3Object {
private String contentDisposition;
private String cacheControl;
private String serverSideEncryption;
private String sseKmsKeyId;
private String sseCustomerAlgorithm;
private String sseCustomerKeyMd5;
private long size;
@@ -103,6 +104,9 @@ public class S3Object {
public String getServerSideEncryption() { return serverSideEncryption; }
public void setServerSideEncryption(String serverSideEncryption) { this.serverSideEncryption = serverSideEncryption; }
public String getSseKmsKeyId() { return sseKmsKeyId; }
public void setSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; }
public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; }
public void setSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; }