From bc98bbbb232fff6c952f674644680d2ca599d4f8 Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:09:21 +0800 Subject: [PATCH] fix(s3): block SSE-C writes for new and configured buckets (#9414) --- docs/services/s3.md | 1 + .../floci/aws/services/s3/S3Controller.java | 5 +- .../io/floci/aws/services/s3/S3Service.java | 98 +++++++-- .../s3/S3DefaultEncryptionServiceTest.java | 186 +++++++++++++++++- .../aws/services/s3/S3IntegrationTest.java | 94 +++++++++ .../s3/S3MultipartIntegrationTest.java | 19 ++ .../s3/S3PresignedPostIntegrationTest.java | 26 +++ 7 files changed, 410 insertions(+), 19 deletions(-) diff --git a/docs/services/s3.md b/docs/services/s3.md index 9bdce2eb2d..2b0cbf2514 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -811,6 +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 +- a bucket encryption rule with `BlockedEncryptionTypes` set to `SSE-C` refuses new SSE-C `PutObject`, `CopyObject`, POST and multipart initiation with `403 AccessDenied`. Buckets without a stored configuration report and enforce this setting by default outside Bahrain (`me-south-1`) and UAE (`me-central-1`). A configured rule that omits `BlockedEncryptionTypes` keeps that regional default; only an explicit `NONE` allows SSE-C writes where the default blocks them. Existing SSE-C objects remain available as copy sources. Existing-account exemptions and the timing of a rule change during an in-flight multipart upload are not modeled - 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: diff --git a/src/main/java/io/floci/aws/services/s3/S3Controller.java b/src/main/java/io/floci/aws/services/s3/S3Controller.java index 502cefa6fe..5db319b0bb 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Controller.java +++ b/src/main/java/io/floci/aws/services/s3/S3Controller.java @@ -4218,7 +4218,10 @@ public class S3Controller { S3Object obj = s3Service.postObject(bucket, key, fileData, objectContentType, metadata.isEmpty() ? null : metadata, new PutObjectOptions() .withServerSideEncryption(encryption) - .withSseKmsKeyId(kmsKeyId)); + .withSseKmsKeyId(kmsKeyId) + .withSseCustomerAlgorithm(lcFields.get("x-amz-server-side-encryption-customer-algorithm")) + .withSseCustomerKey(lcFields.get("x-amz-server-side-encryption-customer-key")) + .withSseCustomerKeyMd5(lcFields.get("x-amz-server-side-encryption-customer-key-md5"))); LOG.infov("Presigned POST upload: {0}/{1} ({2} bytes)", bucket, key, fileData.length); String xml = new XmlBuilder() diff --git a/src/main/java/io/floci/aws/services/s3/S3Service.java b/src/main/java/io/floci/aws/services/s3/S3Service.java index 1db3541f2d..868c4ed6d7 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Service.java +++ b/src/main/java/io/floci/aws/services/s3/S3Service.java @@ -852,9 +852,10 @@ public class S3Service implements Resettable, ResourceProvider { rejectUploadAclWhenOwnershipEnforced(checkedBucket.value(), checkedBucket.account(), effectiveOptions, ownerId()); rejectObjectLockWithoutLockEnabled(checkedBucket.value(), effectiveOptions); String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()); - rejectConflictingServerSideEncryption(normalizedServerSideEncryption, - validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), - effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5())); + SseCustomerKey customerKey = validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), + effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5()); + rejectConflictingServerSideEncryption(normalizedServerSideEncryption, customerKey); + rejectBlockedSseCustomerEncryption(checkedBucket.value(), bucketName, key, customerKey); 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. @@ -1019,6 +1020,7 @@ public class S3Service implements Resettable, ResourceProvider { String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()); SseCustomerKey sseCustomerKey = validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5()); rejectConflictingServerSideEncryption(normalizedServerSideEncryption, sseCustomerKey); + rejectBlockedSseCustomerEncryption(bucket, bucketName, key, sseCustomerKey); rejectKmsKeyIdWithoutKmsEncryption(normalizedServerSideEncryption, effectiveOptions.getSseKmsKeyId()); checkWritePreconditions(bucketName, key, effectiveOptions.getIfMatch(), effectiveOptions.getIfNoneMatch()); @@ -4828,6 +4830,7 @@ public class S3Service implements Resettable, ResourceProvider { String normalizedServerSideEncryption = normalizeServerSideEncryption(serverSideEncryption); SseCustomerKey customerKey = validateSseCustomerKey(sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5); rejectConflictingServerSideEncryption(normalizedServerSideEncryption, customerKey); + rejectBlockedSseCustomerEncryption(destination, bucket, key, customerKey); MultipartUpload upload = new MultipartUpload(bucket, key, contentType); upload.setUploadId(orderedMultipartUploadId(upload.getInitiated(), upload.getUploadId())); upload.setOwnerAccountId(getBucketOwnerAccountId(bucket)); @@ -6519,27 +6522,65 @@ public class S3Service implements Resettable, ResourceProvider { /** * Returns the bucket's server-side encryption configuration as XML. *
- * Buckets that have
- * never been configured return the AWS default (SSE-S3 / {@code AES256});
- * since January 2023 AWS applies SSE-S3 as the base level of encryption on
- * every bucket and never returns 404 for {@code GetBucketEncryption}.
+ * Buckets that have never been configured return the AWS default (SSE-S3 / {@code AES256});
+ * since January 2023 AWS applies SSE-S3 as the base level of encryption on every bucket and
+ * never returns 404 for {@code GetBucketEncryption}. New buckets outside Bahrain and UAE also
+ * report SSE-C blocking.
*/
public String getBucketEncryption(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> noSuchBucket());
if (bucket.getEncryptionConfiguration() == null) {
- return new XmlBuilder()
+ XmlBuilder response = new XmlBuilder()
.start("ServerSideEncryptionConfiguration", AwsNamespaces.S3)
.start("Rule")
.start("ApplyServerSideEncryptionByDefault")
.elem("SSEAlgorithm", "AES256")
.end("ApplyServerSideEncryptionByDefault")
- .elem("BucketKeyEnabled", "false")
- .end("Rule")
- .end("ServerSideEncryptionConfiguration")
- .build();
+ .elem("BucketKeyEnabled", "false");
+ if (blocksSseCustomerEncryptionByDefault(bucket)) {
+ response.start("BlockedEncryptionTypes")
+ .elem("EncryptionType", "SSE-C")
+ .end("BlockedEncryptionTypes");
+ }
+ return response.end("Rule").end("ServerSideEncryptionConfiguration").build();
}
- return bucket.getEncryptionConfiguration();
+ String configuration = bucket.getEncryptionConfiguration();
+ XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(configuration);
+ XmlParser.XmlElement rule = root == null ? null : root.child("Rule");
+ if (!blocksSseCustomerEncryptionByDefault(bucket) || rule == null
+ || rule.child("BlockedEncryptionTypes") != null) {
+ return configuration;
+ }
+ XmlBuilder response = new XmlBuilder(configuration.length() + 100)
+ .start("ServerSideEncryptionConfiguration", AwsNamespaces.S3);
+ for (XmlParser.XmlElement child : root.children()) {
+ if (child == rule) {
+ response.start("Rule");
+ for (XmlParser.XmlElement field : rule.children()) {
+ appendBucketEncryptionElement(response, field);
+ }
+ response.start("BlockedEncryptionTypes")
+ .elem("EncryptionType", "SSE-C")
+ .end("BlockedEncryptionTypes")
+ .end("Rule");
+ } else {
+ appendBucketEncryptionElement(response, child);
+ }
+ }
+ return response.end("ServerSideEncryptionConfiguration").build();
+ }
+
+ private static void appendBucketEncryptionElement(XmlBuilder response, XmlParser.XmlElement element) {
+ if (element.children().isEmpty()) {
+ response.elem(element.name(), element.rawText());
+ return;
+ }
+ response.start(element.name());
+ for (XmlParser.XmlElement child : element.children()) {
+ appendBucketEncryptionElement(response, child);
+ }
+ response.end(element.name());
}
public void putBucketEncryption(String bucketName, String encryptionXml) {
@@ -7268,6 +7309,35 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
+ private void rejectBlockedSseCustomerEncryption(Bucket bucket, String bucketName, String key,
+ SseCustomerKey customerKey) {
+ if (customerKey == null || !blocksSseCustomerEncryption(bucket)) {
+ return;
+ }
+ throw accessDeniedException(bucketName, key);
+ }
+
+ private boolean blocksSseCustomerEncryptionByDefault(Bucket bucket) {
+ String region = bucket.getRegion() != null ? bucket.getRegion()
+ : regionResolver != null ? regionResolver.getDefaultRegion() : null;
+ return !"me-south-1".equals(region) && !"me-central-1".equals(region); // partition-literal: S3 exempts Bahrain and UAE from default SSE-C blocking
+ }
+
+ private boolean blocksSseCustomerEncryption(Bucket bucket) {
+ if (bucket.getEncryptionConfiguration() == null) {
+ return blocksSseCustomerEncryptionByDefault(bucket);
+ }
+ XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(bucket.getEncryptionConfiguration());
+ XmlParser.XmlElement rule = root == null ? null : root.child("Rule");
+ XmlParser.XmlElement blocked = rule == null ? null : rule.child("BlockedEncryptionTypes");
+ if (blocked == null) {
+ return blocksSseCustomerEncryptionByDefault(bucket);
+ }
+ return blocked.children().size() != 1
+ || !"EncryptionType".equals(blocked.children().getFirst().name())
+ || !"NONE".equals(blocked.children().getFirst().text());
+ }
+
/**
* 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,
@@ -8715,6 +8785,8 @@ public class S3Service implements Resettable, ResourceProvider {
effectiveOptions.getSseCustomerKey(),
effectiveOptions.getSseCustomerKeyMd5());
rejectConflictingServerSideEncryption(normalizedServerSideEncryption, destinationCustomerKey);
+ Bucket destination = resolveBucket(destBucket).orElseThrow(() -> noSuchBucket());
+ rejectBlockedSseCustomerEncryption(destination, destBucket, destKey, destinationCustomerKey);
boolean replaceMetadata = "REPLACE".equalsIgnoreCase(effectiveOptions.getMetadataDirective());
MapAccessDenied"));
+
+ given().get("/" + bucket + "/" + key).then().statusCode(404);
+ }
+
@Test
@Order(110)
void presignedPostReturnsVersionIdOnAVersionedBucket() {