fix(s3): refuse a KMS key id without x-amz-server-side-encryption: aws:kms (#9269)

This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-11-24 11:18:52 -04:00
1 parent 97acf496d9
commit cb573492ca
5 files changed
+199 -16

No files matched your search

+1 -1
View File
@@ -811,7 +811,7 @@ Floci now persists and returns the following object attribute state on S3 object
- explicit ACL grant headers (`x-amz-grant-read`, `x-amz-grant-write`, `x-amz-grant-full-control`, `x-amz-grant-read-acp`, `x-amz-grant-write-acp`) with `id` and `uri` grantees on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes
- Object Lock headers (`x-amz-object-lock-mode`, `x-amz-object-lock-retain-until-date`, `x-amz-object-lock-legal-hold`) on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes. A mode without a date or a date without a mode, an unknown mode, or a date that is not an ISO 8601 timestamp is refused with `400 InvalidArgument`. In a bucket without Object Lock, a mode, a date or a legal hold of `ON` is refused with `400 InvalidRequest`, "Bucket is missing Object Lock Configuration", as `PutObjectRetention`, `GetObjectRetention`, `PutObjectLegalHold` and `GetObjectLegalHold` are there. `GetObjectRetention` and `GetObjectLegalHold` of an object without a retention or a legal hold answer `404 NoSuchObjectLockConfiguration`. A `PutObjectRetention` body with a mode but no date, a date but no mode, or an unknown mode is refused with `400 MalformedXML`; one naming neither removes the retention
- object SSE from `x-amz-server-side-encryption` on `PutObject`, `CopyObject`, and multipart initiation, and form fields on browser/presigned POST, returned by `GetObject` and `HeadObject`. An object stored with neither an SSE header nor an SSE-C key takes its bucket's default encryption (`PutBucketEncryption`), SSE-S3 (`AES256`) when the bucket has none. A multipart upload takes it when it is created, as the [`CreateMultipartUpload` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html) says of its parts ("the encryption setting of the uploaded parts is set to the default encryption configuration of the destination bucket"): a default changed afterwards does not reach an upload already created (inferred), and the responses of `CreateMultipartUpload`, `UploadPart`, `UploadPartCopy` and `CompleteMultipartUpload` report it. A `CopyObject` without encryption headers takes the destination bucket's default, not the source object's encryption, as the [`CopyObject` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html) says
- SSE-KMS key IDs from `x-amz-server-side-encryption-aws-kms-key-id` on `PutObject`, browser/presigned POST, and multipart initiation, and from `CopyObject` (only when the request names the encryption; a copy without one takes the destination bucket's default; the parts of an `UploadPartCopy` take the encryption of their upload), returned by `PutObject`, `CopyObject`, `CreateMultipartUpload`, `UploadPart`, `UploadPartCopy`, `CompleteMultipartUpload`, `GetObject`, and `HeadObject`. A bucket's default SSE-KMS key is applied as configured but not simulated: `aws:kms` with no key ID, in the request or in the bucket's default encryption, omits the header rather than returning a synthesized `aws/s3` key ARN
- SSE-KMS key IDs from `x-amz-server-side-encryption-aws-kms-key-id` on `PutObject`, browser/presigned POST, and multipart initiation, and from `CopyObject` (only when the request names the encryption; a copy without one takes the destination bucket's default; the parts of an `UploadPartCopy` take the encryption of their upload), returned by `PutObject`, `CopyObject`, `CreateMultipartUpload`, `UploadPart`, `UploadPartCopy`, `CompleteMultipartUpload`, `GetObject`, and `HeadObject`. A `PutObject` or browser/presigned POST that carries a key ID without `x-amz-server-side-encryption` of `aws:kms` or `aws:kms:dsse`, an empty value included, is refused with `400 InvalidArgument`, "Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms" for `PutObject` (reported against AWS in [aws/aws-cdk#21354](https://github.com/aws/aws-cdk/issues/21354) and [awslabs/amazon-s3-tar-tool#34](https://github.com/awslabs/amazon-s3-tar-tool/issues/34); the POST keeps its own wording). A bucket's default SSE-KMS key is applied as configured but not simulated: `aws:kms` with no key ID, in the request or in the bucket's default encryption, omits the header rather than returning a synthesized `aws/s3` key ARN
Current limitations:
@@ -4160,7 +4160,7 @@ public class S3Controller {
String encryption = S3Service.normalizeServerSideEncryption(
lcFields.get("x-amz-server-side-encryption"));
String kmsKeyId = lcFields.get("x-amz-server-side-encryption-aws-kms-key-id");
if (kmsKeyId != null && !"aws:kms".equals(encryption) && !"aws:kms:dsse".equals(encryption)) {
if (S3Service.kmsKeyIdWithoutKmsEncryption(encryption, kmsKeyId)) {
throw new AwsException("InvalidArgument",
"KMS key ID requires aws:kms or aws:kms:dsse server-side encryption.", 400);
}
@@ -851,10 +851,11 @@ public class S3Service implements Resettable, ResourceProvider {
.orElseThrow(() -> noSuchBucket());
rejectUploadAclWhenOwnershipEnforced(checkedBucket.value(), checkedBucket.account(), effectiveOptions, ownerId());
rejectObjectLockWithoutLockEnabled(checkedBucket.value(), effectiveOptions);
rejectConflictingServerSideEncryption(
normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()),
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
rejectConflictingServerSideEncryption(normalizedServerSideEncryption,
validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(),
effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5()));
rejectKmsKeyIdWithoutKmsEncryption(normalizedServerSideEncryption, effectiveOptions.getSseKmsKeyId());
ChecksumAlgorithm declared = ChecksumAlgorithm.fromWireValue(effectiveOptions.getChecksumAlgorithm());
// Checked again under the bucket lock when the object is stored; this only spares a transfer.
checkWritePreconditions(bucketName, key, effectiveOptions.getIfMatch(), effectiveOptions.getIfNoneMatch());
@@ -1018,6 +1019,7 @@ public class S3Service implements Resettable, ResourceProvider {
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
SseCustomerKey sseCustomerKey = validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5());
rejectConflictingServerSideEncryption(normalizedServerSideEncryption, sseCustomerKey);
rejectKmsKeyIdWithoutKmsEncryption(normalizedServerSideEncryption, effectiveOptions.getSseKmsKeyId());
checkWritePreconditions(bucketName, key, effectiveOptions.getIfMatch(), effectiveOptions.getIfNoneMatch());
byte[] data = body instanceof BytesBody bytes ? bytes.data() : null;
@@ -7266,6 +7268,25 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
/**
* Whether a request or a stored object carries a KMS key id without {@code aws:kms} or
* {@code aws:kms:dsse} encryption: the id only means something with one of those algorithms,
* and AWS refuses a write that carries it with any other algorithm or with none, an empty
* value included.
*/
static boolean kmsKeyIdWithoutKmsEncryption(String serverSideEncryption, String sseKmsKeyId) {
return sseKmsKeyId != null
&& !"aws:kms".equals(serverSideEncryption)
&& !"aws:kms:dsse".equals(serverSideEncryption);
}
private static void rejectKmsKeyIdWithoutKmsEncryption(String serverSideEncryption, String sseKmsKeyId) {
if (kmsKeyIdWithoutKmsEncryption(serverSideEncryption, sseKmsKeyId)) {
throw new AwsException("InvalidArgument",
"Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms", 400);
}
}
private static String computeSseCustomerKeyMd5(String key) {
try {
byte[] decodedKey = Base64.getDecoder().decode(key.trim());
@@ -8716,7 +8737,8 @@ public class S3Service implements Resettable, ResourceProvider {
String effectiveCacheControl = replaceMetadata && effectiveOptions.getCacheControl() != null
? effectiveOptions.getCacheControl()
: source.getCacheControl();
String effectiveSseKmsKeyId = effectiveOptions.getSseKmsKeyId();
String effectiveSseKmsKeyId = new ObjectEncryption(normalizedServerSideEncryption,
effectiveOptions.getSseKmsKeyId()).keptKmsKeyId();
boolean replaceTags = "REPLACE".equalsIgnoreCase(effectiveOptions.getTaggingDirective());
Map<String, String> effectiveTags = replaceTags
? effectiveOptions.getReplacementTagging()
@@ -1995,17 +1995,51 @@ class S3IntegrationTest {
.header("x-amz-server-side-encryption", equalTo("aws:kms"))
.header("x-amz-server-side-encryption-aws-kms-key-id", equalTo(kmsKeyId));
given()
.contentType("text/plain")
.header("x-amz-server-side-encryption", "aws:kms:dsse")
.header("x-amz-server-side-encryption-aws-kms-key-id", kmsKeyId)
.body("dsse-encrypted-content")
.when()
.put("/sse-bucket/dsse-encrypted.txt")
.then()
.statusCode(200)
.header("x-amz-server-side-encryption", equalTo("aws:kms:dsse"))
.header("x-amz-server-side-encryption-aws-kms-key-id", equalTo(kmsKeyId));
given()
.contentType("text/plain")
.header("x-amz-server-side-encryption-aws-kms-key-id", kmsKeyId)
.body("kms-id-without-algorithm")
.when()
.put("/sse-bucket/kms-id-without-algorithm.txt")
.then()
.statusCode(400)
.body(containsString("InvalidArgument"))
.body(containsString("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms"));
given()
.contentType("text/plain")
.header("x-amz-server-side-encryption", "AES256")
.header("x-amz-server-side-encryption-aws-kms-key-id", kmsKeyId)
.body("aes-content")
.body("aes-with-kms-id")
.when()
.put("/sse-bucket/aes-with-kms-id.txt")
.then()
.statusCode(200)
.header("x-amz-server-side-encryption", equalTo("AES256"))
.header("x-amz-server-side-encryption-aws-kms-key-id", nullValue());
.statusCode(400)
.body(containsString("InvalidArgument"))
.body(containsString("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms"));
given()
.contentType("text/plain")
.header("x-amz-server-side-encryption-aws-kms-key-id", "")
.body("empty-kms-id")
.when()
.put("/sse-bucket/empty-kms-id.txt")
.then()
.statusCode(400)
.body(containsString("InvalidArgument"))
.body(containsString("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms"));
}
@Test
@@ -2029,11 +2063,11 @@ class S3IntegrationTest {
given()
.when()
.get("/sse-bucket/aes-with-kms-id.txt")
.get("/sse-bucket/dsse-encrypted.txt")
.then()
.statusCode(200)
.header("x-amz-server-side-encryption", equalTo("AES256"))
.header("x-amz-server-side-encryption-aws-kms-key-id", nullValue());
.header("x-amz-server-side-encryption", equalTo("aws:kms:dsse"))
.header("x-amz-server-side-encryption-aws-kms-key-id", equalTo(kmsKeyId));
}
@Test
@@ -34,6 +34,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
@@ -329,6 +330,132 @@ class S3ServiceTest {
assertTrue(exception.getMessage().contains("Unsupported x-amz-server-side-encryption value"));
}
@Test
void putObjectRefusesKmsKeyIdWithoutServerSideEncryption() {
s3Service.createBucket("test-bucket", "us-east-1");
AwsException exception = assertThrows(AwsException.class, () ->
s3Service.putObject(
"test-bucket",
"kms-id-without-algorithm.txt",
"data".getBytes(StandardCharsets.UTF_8),
"text/plain",
null,
new PutObjectOptions()
.withSseKmsKeyId("arn:aws:kms:us-east-1:000000000000:key/test-key")
)
);
assertEquals("InvalidArgument", exception.getErrorCode());
assertEquals("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms",
exception.getMessage());
assertThrows(AwsException.class, () -> s3Service.getObject("test-bucket", "kms-id-without-algorithm.txt"));
}
@Test
void putObjectRefusesKmsKeyIdWithAes256() {
s3Service.createBucket("test-bucket", "us-east-1");
AwsException exception = assertThrows(AwsException.class, () ->
s3Service.putObject(
"test-bucket",
"kms-id-with-aes256.txt",
"data".getBytes(StandardCharsets.UTF_8),
"text/plain",
null,
new PutObjectOptions()
.withServerSideEncryption("AES256")
.withSseKmsKeyId("arn:aws:kms:us-east-1:000000000000:key/test-key")
)
);
assertEquals("InvalidArgument", exception.getErrorCode());
assertEquals("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms",
exception.getMessage());
}
@Test
void putObjectRefusesAnEmptyKmsKeyIdWithoutServerSideEncryption() {
s3Service.createBucket("test-bucket", "us-east-1");
AwsException exception = assertThrows(AwsException.class, () ->
s3Service.putObject(
"test-bucket",
"empty-kms-id.txt",
"data".getBytes(StandardCharsets.UTF_8),
"text/plain",
null,
new PutObjectOptions().withSseKmsKeyId("")
)
);
assertEquals("InvalidArgument", exception.getErrorCode());
assertEquals("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms",
exception.getMessage());
}
@Test
void putObjectWithKmsKeepsTheKeyId() {
s3Service.createBucket("test-bucket", "us-east-1");
String kmsKeyId = "arn:aws:kms:us-east-1:000000000000:key/test-key";
S3Object put = s3Service.putObject(
"test-bucket",
"kms-with-key-id.txt",
"data".getBytes(StandardCharsets.UTF_8),
"text/plain",
null,
new PutObjectOptions()
.withServerSideEncryption("aws:kms")
.withSseKmsKeyId(kmsKeyId)
);
assertEquals("aws:kms", put.getServerSideEncryption());
assertEquals(kmsKeyId, put.getSseKmsKeyId());
}
@Test
void putObjectWithDsseKeepsTheKeyId() {
s3Service.createBucket("test-bucket", "us-east-1");
String kmsKeyId = "arn:aws:kms:us-east-1:000000000000:key/test-key";
S3Object put = s3Service.putObject(
"test-bucket",
"dsse-with-key-id.txt",
"data".getBytes(StandardCharsets.UTF_8),
"text/plain",
null,
new PutObjectOptions()
.withServerSideEncryption("aws:kms:dsse")
.withSseKmsKeyId(kmsKeyId)
);
assertEquals("aws:kms:dsse", put.getServerSideEncryption());
assertEquals(kmsKeyId, put.getSseKmsKeyId());
}
@Test
void streamedPutObjectRefusesKmsKeyIdWithoutReadingTheBody() {
s3Service.createBucket("test-bucket", "us-east-1");
InputStream body = new InputStream() {
@Override
public int read() throws IOException {
throw new IOException("the body must not be read");
}
};
AwsException exception = assertThrows(AwsException.class, () ->
s3Service.putObject("test-bucket", "streamed-kms-id.txt", body, UploadChecksums.NONE,
"text/plain", null,
new PutObjectOptions()
.withSseKmsKeyId("arn:aws:kms:us-east-1:000000000000:key/test-key"))
);
assertEquals("InvalidArgument", exception.getErrorCode());
assertEquals("Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms",
exception.getMessage());
}
@Test
void putObjectWritesFileToDisk() {
s3Service.createBucket("test-bucket", "us-east-1");
@@ -1216,14 +1343,14 @@ class S3ServiceTest {
@Test
void resolvePathWithTraversalThrows() {
s3Service.createBucket("test-bucket", "us-east-1");
// Blocked: going above the bucket root
AwsException ex = assertThrows(AwsException.class, () ->
AwsException ex = assertThrows(AwsException.class, () ->
s3Service.putObject("test-bucket", "../outside.txt", "data".getBytes(), null, null));
assertEquals("InvalidKey", ex.getErrorCode());
// Blocked: deeper traversal
assertThrows(AwsException.class, () ->
assertThrows(AwsException.class, () ->
s3Service.getObject("test-bucket", "dir/../../../etc/passwd"));
}