fix(s3): block SSE-C writes for new and configured buckets (#9414)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-25 07:09:21 -04:00
1 parent 017b687567
commit bc98bbbb23
7 files changed
+410 -19

No files matched your search

+1
View File
@@ -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:
@@ -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()
@@ -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.
* <p>
* 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());
Map<String, String> metadata = replaceMetadata ? new LinkedHashMap<>() : new LinkedHashMap<>(source.getMetadata());
@@ -1,5 +1,6 @@
package io.floci.aws.services.s3;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.s3.model.CopyObjectOptions;
import io.floci.aws.services.s3.model.MultipartUpload;
@@ -9,6 +10,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
@@ -17,8 +19,11 @@ import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* An object written without encryption headers is encrypted with its bucket's default encryption:
@@ -56,6 +61,64 @@ class S3DefaultEncryptionServiceTest {
assertEquals("AES256", s3Service.getObject(BUCKET, "plain.txt").getServerSideEncryption());
}
@Test
void aNewBucketReportsAndEnforcesSseCustomerBlockingByDefault() throws Exception {
assertTrue(s3Service.getBucketEncryption(BUCKET).contains(
"<BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType></BlockedEncryptionTypes>"));
AwsException denied = assertThrows(AwsException.class,
() -> put("default-blocked.txt", customerKey(new PutObjectOptions())));
assertEquals("AccessDenied", denied.getErrorCode());
assertEquals(403, denied.getHttpStatus());
}
@Test
void aPlainEncryptionRulePreservesSseCustomerBlocking() throws Exception {
s3Service.putBucketEncryption(BUCKET, configuration("AES256", null));
assertTrue(s3Service.getBucketEncryption(BUCKET).contains(
"<BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType></BlockedEncryptionTypes>"));
AwsException denied = assertThrows(AwsException.class,
() -> put("still-blocked.txt", customerKey(new PutObjectOptions())));
assertEquals("AccessDenied", denied.getErrorCode());
assertEquals(403, denied.getHttpStatus());
}
@Test
void configuredEncryptionReadbackPreservesOtherRuleFields() {
s3Service.putBucketEncryption(BUCKET, "<ServerSideEncryptionConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"
+ "<Rule><ApplyServerSideEncryptionByDefault><SSEAlgorithm>aws:kms</SSEAlgorithm>"
+ "<KMSMasterKeyID>alias/R&amp;D</KMSMasterKeyID></ApplyServerSideEncryptionByDefault>"
+ "<BucketKeyEnabled>true</BucketKeyEnabled></Rule></ServerSideEncryptionConfiguration>");
String readback = s3Service.getBucketEncryption(BUCKET);
assertTrue(readback.startsWith("<ServerSideEncryptionConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"));
assertTrue(readback.contains("<SSEAlgorithm>aws:kms</SSEAlgorithm>"));
assertTrue(readback.contains("<KMSMasterKeyID>alias/R&amp;D</KMSMasterKeyID>"));
assertTrue(readback.contains("<BucketKeyEnabled>true</BucketKeyEnabled>"));
assertTrue(readback.contains("<BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType></BlockedEncryptionTypes>"));
}
@Test
void bahrainAndUaeBucketsDoNotBlockSseCustomerEncryptionByDefault() throws Exception {
for (String region : List.of("me-south-1", "me-central-1")) {
String bucket = "sse-c-" + region;
s3Service.createBucket(bucket, region);
assertFalse(s3Service.getBucketEncryption(bucket).contains("BlockedEncryptionTypes"));
S3Object written = s3Service.putObject(bucket, "allowed.txt", DATA, "text/plain", null,
customerKey(new PutObjectOptions()));
assertEquals("AES256", written.getSseCustomerAlgorithm());
s3Service.putBucketEncryption(bucket, blockedEncryption("SSE-C"));
AwsException denied = assertThrows(AwsException.class,
() -> s3Service.putObject(bucket, "blocked.txt", DATA, "text/plain", null,
customerKey(new PutObjectOptions())));
assertEquals("AccessDenied", denied.getErrorCode());
}
}
@Test
void aKmsDefaultIsAppliedWithItsKey() {
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
@@ -104,7 +167,7 @@ class S3DefaultEncryptionServiceTest {
@Test
void anSseCustomerKeyUploadGetsNoDefault() throws Exception {
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY, "NONE"));
S3Object put = put("sse-c.txt", customerKey(new PutObjectOptions()));
@@ -113,6 +176,104 @@ class S3DefaultEncryptionServiceTest {
assertEquals("AES256", put.getSseCustomerAlgorithm());
}
@Test
void aBucketBlockingSseCustomerKeysRejectsPutObject() throws Exception {
s3Service.putBucketEncryption(BUCKET, "<ServerSideEncryptionConfiguration>"
+ "<Rule><BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType>"
+ "</BlockedEncryptionTypes></Rule></ServerSideEncryptionConfiguration>");
AwsException denied = assertThrows(AwsException.class,
() -> put("blocked-sse-c.txt", customerKey(new PutObjectOptions())));
assertEquals("AccessDenied", denied.getErrorCode());
assertEquals(403, denied.getHttpStatus());
assertEquals("AES256", put("allowed-sse-s3.txt", new PutObjectOptions()).getServerSideEncryption());
}
@Test
void aBlockedStreamingPutFailsBeforeReadingItsBody() throws Exception {
PutObjectOptions sseC = customerKey(new PutObjectOptions());
InputStream unread = new InputStream() {
@Override
public int read() {
throw new AssertionError("Blocked upload body must not be read");
}
};
AwsException denied = assertThrows(AwsException.class,
() -> s3Service.putObject(BUCKET, "streamed.txt", unread, UploadChecksums.NONE,
"text/plain", null, sseC));
assertEquals("AccessDenied", denied.getErrorCode());
assertEquals(403, denied.getHttpStatus());
}
@Test
void aBucketBlockingSseCustomerKeysRejectsPostAndDestinationCopy() throws Exception {
s3Service.putObject(SOURCE, "source.txt", DATA, "text/plain", null, new PutObjectOptions());
s3Service.putBucketEncryption(BUCKET, blockedEncryption("SSE-C"));
PutObjectOptions sseC = customerKey(new PutObjectOptions());
AwsException post = assertThrows(AwsException.class,
() -> s3Service.postObject(BUCKET, "posted.txt", DATA, "text/plain", null, sseC));
AwsException copy = assertThrows(AwsException.class,
() -> s3Service.copyObject(SOURCE, "source.txt", BUCKET, "copy.txt", null,
new CopyObjectOptions()
.withSseCustomerAlgorithm(sseC.getSseCustomerAlgorithm())
.withSseCustomerKey(sseC.getSseCustomerKey())
.withSseCustomerKeyMd5(sseC.getSseCustomerKeyMd5())));
assertEquals("AccessDenied", post.getErrorCode());
assertEquals(403, post.getHttpStatus());
assertEquals("AccessDenied", copy.getErrorCode());
assertEquals(403, copy.getHttpStatus());
assertEquals("AES256", s3Service.copyObject(SOURCE, "source.txt", BUCKET, "allowed-copy.txt")
.getServerSideEncryption());
}
@Test
void aBucketBlockingSseCustomerKeysRejectsMultipartInitiation() throws Exception {
s3Service.putBucketEncryption(BUCKET, blockedEncryption("SSE-C"));
AwsException denied = assertThrows(AwsException.class,
() -> s3Service.initiateMultipartUpload(BUCKET, "blocked.bin", "application/octet-stream",
Map.of(), null, customerKey(new PutObjectOptions())));
assertEquals("AccessDenied", denied.getErrorCode());
assertEquals(403, denied.getHttpStatus());
MultipartUpload allowed = s3Service.initiateMultipartUpload(BUCKET, "allowed.bin",
"application/octet-stream", Map.of(), null, new PutObjectOptions());
assertEquals("AES256", allowed.effectiveServerSideEncryption());
}
@Test
void existingSseCustomerObjectCanBeCopiedAsAReadableSourceAfterBlocking() throws Exception {
PutObjectOptions sseC = customerKey(new PutObjectOptions());
s3Service.putBucketEncryption(BUCKET, blockedEncryption("NONE"));
put("existing.txt", sseC);
s3Service.putBucketEncryption(BUCKET, blockedEncryption("SSE-C"));
S3Object copy = s3Service.copyObject(BUCKET, "existing.txt", SOURCE, "copy.txt", null,
new CopyObjectOptions()
.withCopySourceSseCustomerAlgorithm(sseC.getSseCustomerAlgorithm())
.withCopySourceSseCustomerKey(sseC.getSseCustomerKey())
.withCopySourceSseCustomerKeyMd5(sseC.getSseCustomerKeyMd5()));
assertEquals("AES256", s3Service.getObject(BUCKET, "existing.txt").getSseCustomerAlgorithm());
assertEquals("AES256", copy.getServerSideEncryption());
assertNull(copy.getSseCustomerAlgorithm());
}
@Test
void explicitNoneDoesNotBlockSseCustomerWrites() throws Exception {
s3Service.putBucketEncryption(BUCKET, blockedEncryption("NONE"));
S3Object written = put("allowed-sse-c.txt", customerKey(new PutObjectOptions()));
assertEquals("AES256", written.getSseCustomerAlgorithm());
assertNull(written.getServerSideEncryption());
}
@Test
void objectsAlreadyStoredKeepTheirEncryption() {
put("before.txt", new PutObjectOptions());
@@ -140,7 +301,7 @@ class S3DefaultEncryptionServiceTest {
@Test
void anSseCustomerKeyMultipartUploadGetsNoDefault() throws Exception {
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY, "NONE"));
PutObjectOptions sseC = customerKey(new PutObjectOptions());
MultipartUpload upload = s3Service.initiateMultipartUpload(BUCKET, "multipart-sse-c.bin",
"application/octet-stream", Map.of(), null, sseC);
@@ -210,7 +371,7 @@ class S3DefaultEncryptionServiceTest {
@Test
void anUploadThatSendsItsOwnEncryptionRecordsNoDefault() throws Exception {
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY, "NONE"));
MultipartUpload explicit = s3Service.initiateMultipartUpload(BUCKET, "explicit.bin",
"application/octet-stream", Map.of(), null, new PutObjectOptions().withServerSideEncryption("AES256"));
@@ -259,6 +420,7 @@ class S3DefaultEncryptionServiceTest {
s3Service.putObject(SOURCE, "old.txt", DATA, "text/plain", null, new PutObjectOptions());
objectStore.scan(key -> true).forEach(object -> object.setServerSideEncryption(null));
PutObjectOptions sseC = customerKey(new PutObjectOptions());
s3Service.putBucketEncryption(SOURCE, blockedEncryption("NONE"));
s3Service.putObject(SOURCE, "sse-c.txt", DATA, "text/plain", null, sseC);
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
@@ -323,7 +485,7 @@ class S3DefaultEncryptionServiceTest {
@Test
void aCopyEncryptedWithACustomerKeyGetsNoDefault() throws Exception {
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY));
s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY, "NONE"));
put("source.txt", new PutObjectOptions());
PutObjectOptions sseC = customerKey(new PutObjectOptions());
@@ -342,10 +504,24 @@ class S3DefaultEncryptionServiceTest {
}
private static String configuration(String algorithm, String kmsKeyId) {
return configuration(algorithm, kmsKeyId, null);
}
private static String configuration(String algorithm, String kmsKeyId, String blockedType) {
return "<ServerSideEncryptionConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Rule>"
+ "<ApplyServerSideEncryptionByDefault><SSEAlgorithm>" + algorithm + "</SSEAlgorithm>"
+ (kmsKeyId == null ? "" : "<KMSMasterKeyID>" + kmsKeyId + "</KMSMasterKeyID>")
+ "</ApplyServerSideEncryptionByDefault></Rule></ServerSideEncryptionConfiguration>";
+ "</ApplyServerSideEncryptionByDefault>"
+ (blockedType == null ? "" : "<BlockedEncryptionTypes><EncryptionType>" + blockedType
+ "</EncryptionType></BlockedEncryptionTypes>")
+ "</Rule></ServerSideEncryptionConfiguration>";
}
private static String blockedEncryption(String type) {
return "<ServerSideEncryptionConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Rule>"
+ "<ApplyServerSideEncryptionByDefault><SSEAlgorithm>AES256</SSEAlgorithm>"
+ "</ApplyServerSideEncryptionByDefault><BlockedEncryptionTypes><EncryptionType>" + type
+ "</EncryptionType></BlockedEncryptionTypes></Rule></ServerSideEncryptionConfiguration>";
}
private static PutObjectOptions customerKey(PutObjectOptions options) throws Exception {
@@ -1963,6 +1963,73 @@ class S3IntegrationTest {
// --- Server-Side Encryption header preservation ---
@Test
@Order(76)
void anEncryptionRuleWithoutBlockedTypesKeepsSseCustomerBlocking() {
given()
.put("/sse-policy-bucket")
.then()
.statusCode(200);
given()
.body("<ServerSideEncryptionConfiguration><Rule>"
+ "<ApplyServerSideEncryptionByDefault><SSEAlgorithm>AES256</SSEAlgorithm>"
+ "</ApplyServerSideEncryptionByDefault></Rule></ServerSideEncryptionConfiguration>")
.when()
.put("/sse-policy-bucket?encryption")
.then()
.statusCode(200);
given()
.when()
.get("/sse-policy-bucket?encryption")
.then()
.statusCode(200)
.body(containsString("<BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType>"));
given()
.header("x-amz-server-side-encryption-customer-algorithm", "AES256")
.header("x-amz-server-side-encryption-customer-key", SSE_CUSTOMER_KEY)
.header("x-amz-server-side-encryption-customer-key-MD5", SSE_CUSTOMER_KEY_MD5)
.body("blocked")
.when()
.put("/sse-policy-bucket/blocked.txt")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.body("<ServerSideEncryptionConfiguration><Rule>"
+ "<ApplyServerSideEncryptionByDefault><SSEAlgorithm>AES256</SSEAlgorithm>"
+ "</ApplyServerSideEncryptionByDefault><BlockedEncryptionTypes>"
+ "<EncryptionType>NONE</EncryptionType></BlockedEncryptionTypes></Rule>"
+ "</ServerSideEncryptionConfiguration>")
.when()
.put("/sse-policy-bucket?encryption")
.then()
.statusCode(200);
given()
.when()
.get("/sse-policy-bucket?encryption")
.then()
.statusCode(200)
.body(containsString("<EncryptionType>NONE</EncryptionType>"));
given()
.header("x-amz-server-side-encryption-customer-algorithm", "AES256")
.header("x-amz-server-side-encryption-customer-key", SSE_CUSTOMER_KEY)
.header("x-amz-server-side-encryption-customer-key-MD5", SSE_CUSTOMER_KEY_MD5)
.body("allowed")
.when()
.put("/sse-policy-bucket/allowed.txt")
.then()
.statusCode(200);
given().delete("/sse-policy-bucket/allowed.txt");
given().delete("/sse-policy-bucket");
}
@Test
@Order(76)
void createSseBucketAndPutObject() {
@@ -1971,6 +2038,33 @@ class S3IntegrationTest {
.then()
.statusCode(200);
given()
.when()
.get("/sse-bucket?encryption")
.then()
.statusCode(200)
.body(containsString("<BlockedEncryptionTypes><EncryptionType>SSE-C</EncryptionType>"));
given()
.header("x-amz-server-side-encryption-customer-algorithm", "AES256")
.header("x-amz-server-side-encryption-customer-key", SSE_CUSTOMER_KEY)
.header("x-amz-server-side-encryption-customer-key-MD5", SSE_CUSTOMER_KEY_MD5)
.body("blocked")
.when()
.put("/sse-bucket/default-blocked.txt")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.body("<ServerSideEncryptionConfiguration><Rule><BlockedEncryptionTypes>"
+ "<EncryptionType>NONE</EncryptionType></BlockedEncryptionTypes></Rule>"
+ "</ServerSideEncryptionConfiguration>")
.when()
.put("/sse-bucket?encryption")
.then()
.statusCode(200);
given()
.contentType("text/plain")
.header("x-amz-server-side-encryption", "AES256")
@@ -41,6 +41,25 @@ class S3MultipartIntegrationTest {
given()
.when().put("/" + BUCKET)
.then().statusCode(200);
given()
.header("x-amz-server-side-encryption-customer-algorithm", "AES256")
.header("x-amz-server-side-encryption-customer-key", SSE_CUSTOMER_KEY)
.header("x-amz-server-side-encryption-customer-key-MD5", SSE_CUSTOMER_KEY_MD5)
.when()
.post("/" + BUCKET + "/blocked.bin?uploads")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.body("<ServerSideEncryptionConfiguration><Rule><BlockedEncryptionTypes>"
+ "<EncryptionType>NONE</EncryptionType></BlockedEncryptionTypes></Rule>"
+ "</ServerSideEncryptionConfiguration>")
.when()
.put("/" + BUCKET + "?encryption")
.then()
.statusCode(200);
}
@Test
@@ -11,6 +11,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@@ -892,6 +893,31 @@ class S3PresignedPostIntegrationTest {
}
}
@Test
@Order(106)
void presignedPostRejectsSseCustomerEncryptionForNewBucket() throws Exception {
String bucket = "presigned-post-sse-c-blocked-bucket";
String key = "blocked.txt";
byte[] customerKey = "0123456789abcdef0123456789abcdef".getBytes(StandardCharsets.UTF_8);
String encodedKey = Base64.getEncoder().encodeToString(customerKey);
String keyMd5 = Base64.getEncoder().encodeToString(MessageDigest.getInstance("MD5").digest(customerKey));
given().put("/" + bucket).then().statusCode(200);
given()
.multiPart("key", key)
.multiPart("x-amz-server-side-encryption-customer-algorithm", "AES256")
.multiPart("x-amz-server-side-encryption-customer-key", encodedKey)
.multiPart("x-amz-server-side-encryption-customer-key-MD5", keyMd5)
.multiPart("file", "blocked.txt", "blocked".getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + bucket)
.then()
.statusCode(403)
.body(containsString("<Code>AccessDenied</Code>"));
given().get("/" + bucket + "/" + key).then().statusCode(404);
}
@Test
@Order(110)
void presignedPostReturnsVersionIdOnAVersionedBucket() {