fix(s3): place the encryption condition keys on a put, and on a part from its upload (#7050)

* fix(s3): place the encryption condition keys on a put, and on a part from its upload

A policy on s3:x-amz-server-side-encryption or
s3:x-amz-server-side-encryption-aws-kms-key-id read neither key, so a Deny under a
negated operator refused every put, encrypted or not.

A PutObject, CopyObject or CreateMultipartUpload now carries both from its headers, each
value as sent, in the enforcement filter and in S3's own bucket policy check. A part or a
completion sends neither header and carries what its upload recorded when it was created.
A part naming an upload not held answers NoSuchUpload, as S3 does under such a policy.

* fix(s3): check as an object write a PUT or POST naming an upload but not routed as a part

The enforcement filter let through a request naming an upload not held, and judged one
naming a held upload by that upload's encryption, on uploadId alone. A PUT with no
partNumber, or a POST asking for a new upload, creates an object or an upload whatever
that parameter says, so it skipped IAM or read the wrong encryption. Both now match the
routes S3Controller takes as a part or a completion. A part copy naming an upload not held
stays checked, since it names a source.

* test(s3): put the multipart encryption cases beside kmsOnlyBucket

The multipart cases copied S3BucketPolicyEnforcementIntegrationTest's two-statement KMS-only policy
and BUCKET_KEY. They now sit beside kmsOnlyBucket and reuse it, with the policy written once
(putKmsOnlyPolicy). The two cases whose upload is opened before the policy stands use a bucket with
no default encryption, as before: S3 records the bucket's default on an upload that asks for none,
where Floci records only what the request asks for.
This commit is contained in:
Charles Swanberg authored and GitHub committed 2026-11-02 14:46:31 -04:00
1 parent 1b4f99cb8a
commit fccc81b61d
8 files changed
+376 -7

No files matched your search

@@ -10,6 +10,7 @@ import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -842,4 +843,274 @@ class S3BucketPolicyEnforcementIntegrationTest {
.statusCode(403)
.body(containsString("<Code>AccessDenied</Code>"));
}
private static final String BUCKET_KEY = "arn:aws:kms:us-east-1:111122223333:key/11111111-1111-1111-1111-111111111111";
private static final String OTHER_KEY = "arn:aws:kms:us-east-1:111122223333:key/22222222-2222-2222-2222-222222222222";
/** The refusals and the admission are what S3 answered under these two statements, measured 2026-10-30. */
@Test
void aPutIsJudgedByItsOwnEncryptionHeadersNotTheBucketsDefault() {
EncryptionFixture fixture = kmsOnlyBucket();
assertEncryptedPut(fixture, "none.txt", null, null, 403);
assertEncryptedPut(fixture, "aes.txt", "AES256", null, 403);
assertEncryptedPut(fixture, "bucket-key.txt", "aws:kms", BUCKET_KEY, 200);
assertEncryptedPut(fixture, "other-key.txt", "aws:kms", OTHER_KEY, 403);
assertEncryptedPut(fixture, "no-key.txt", "aws:kms", null, 403);
}
@Test
void aCopyIsJudgedByItsOwnEncryptionHeaders() {
EncryptionFixture fixture = kmsOnlyBucket();
assertEncryptedPut(fixture, "source.txt", "aws:kms", BUCKET_KEY, 200);
String source = "/" + fixture.bucket() + "/source.txt";
given().filter(fixture.user().signer()).header("x-amz-copy-source", source)
.put("/" + fixture.bucket() + "/plain-copy.txt")
.then().statusCode(403).body(containsString("<Code>AccessDenied</Code>"));
given().filter(fixture.user().signer()).header("x-amz-copy-source", source)
.header(SSE, "aws:kms")
.header(SSE_KEY, BUCKET_KEY)
.put("/" + fixture.bucket() + "/encrypted-copy.txt")
.then().statusCode(200);
}
@Test
void anAllowOnTheEncryptionKeyAdmitsOnlyTheMatchingPut() {
EncryptionFixture fixture = allowOnlyBucket("s3:x-amz-server-side-encryption", "aws:kms");
assertEncryptedPut(fixture, "kms.txt", "aws:kms", null, 200);
assertEncryptedPut(fixture, "aes.txt", "AES256", null, 403);
assertEncryptedPut(fixture, "none.txt", null, null, 403);
}
@Test
void anAllowOnTheKmsKeyIdKeyAdmitsOnlyTheMatchingPut() {
EncryptionFixture fixture = allowOnlyBucket("s3:x-amz-server-side-encryption-aws-kms-key-id", BUCKET_KEY);
assertEncryptedPut(fixture, "bucket-key.txt", "aws:kms", BUCKET_KEY, 200);
assertEncryptedPut(fixture, "other-key.txt", "aws:kms", OTHER_KEY, 403);
assertEncryptedPut(fixture, "no-key.txt", "aws:kms", null, 403);
}
/**
* A multipart upload under the same two statements. A part sends neither header, so it is judged
* by what its upload recorded. AWS does not document this; it was measured on S3 on 2026-10-30,
* each case here as S3 answered it. A case whose upload is opened before the policy stands uses a
* bucket with no default encryption: S3 records the bucket's default on an upload that asks for
* none, where Floci records only what the request asks for, so on a bucket defaulting to
* {@code aws:kms} such a part is admitted on S3 and refused here.
*/
@Test
void anUploadOpenedWithTheBucketsKeyHasItsPartsAdmittedAndStandsUnderThatKey() {
EncryptionFixture fixture = kmsOnlyBucket();
S3RequestSigner user = fixture.user().signer();
String uploadId = given().filter(user).header(SSE, "aws:kms").header(SSE_KEY, BUCKET_KEY)
.post("/" + fixture.bucket() + "/large.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
String etag = uploadPart(user, fixture, "large.bin", uploadId)
.then().statusCode(200).extract().header("ETag");
given().filter(user).contentType("application/xml")
.body("<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>" + etag
+ "</ETag></Part></CompleteMultipartUpload>")
.post("/" + fixture.bucket() + "/large.bin?uploadId=" + uploadId)
.then().statusCode(200);
given().filter(user).head("/" + fixture.bucket() + "/large.bin")
.then().statusCode(200)
.header(SSE, equalTo("aws:kms"))
.header(SSE_KEY, equalTo(BUCKET_KEY));
}
@Test
void anUploadOpenedWithNoEncryptionUnderThePolicyIsRefusedAndNoObjectStands() {
EncryptionFixture fixture = kmsOnlyBucket();
given().filter(fixture.user().signer()).post("/" + fixture.bucket() + "/plain.bin?uploads")
.then().statusCode(403).body(containsString("AccessDenied"));
given().filter(fixture.owner()).head("/" + fixture.bucket() + "/plain.bin").then().statusCode(404);
}
@Test
void aPartOfAnUploadThatRecordedNoEncryptionIsRefusedAndNoObjectStands() {
EncryptionFixture fixture = bucketWithoutDefaultEncryption();
String uploadId = given().filter(fixture.user().signer()).post("/" + fixture.bucket() + "/plain.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
putKmsOnlyPolicy(fixture);
uploadPart(fixture.user().signer(), fixture, "plain.bin", uploadId)
.then().statusCode(403).body(containsString("AccessDenied"));
given().filter(fixture.owner()).head("/" + fixture.bucket() + "/plain.bin").then().statusCode(404);
given().filter(fixture.owner()).delete("/" + fixture.bucket() + "/plain.bin?uploadId=" + uploadId)
.then().statusCode(204);
}
@Test
void aPartOfAnUploadThatRecordedAnotherEncryptionIsRefused() {
EncryptionFixture fixture = bucketWithoutDefaultEncryption();
String uploadId = given().filter(fixture.user().signer()).header(SSE, "AES256")
.post("/" + fixture.bucket() + "/aes.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
putKmsOnlyPolicy(fixture);
uploadPart(fixture.user().signer(), fixture, "aes.bin", uploadId)
.then().statusCode(403).body(containsString("AccessDenied"));
given().filter(fixture.owner()).delete("/" + fixture.bucket() + "/aes.bin?uploadId=" + uploadId)
.then().statusCode(204);
}
@Test
void aPartNamingAnUploadNotHeldAnswersNoSuchUpload() {
EncryptionFixture fixture = kmsOnlyBucket();
uploadPart(fixture.user().signer(), fixture, "unknown.bin", "no-such-upload")
.then().statusCode(404).body(containsString("NoSuchUpload"));
}
@Test
void aPutNamingAnUploadButNoPartIsAnObjectWriteAndIsChecked() {
EncryptionFixture fixture = kmsOnlyBucket();
S3RequestSigner unpermitted = encryptionUser("unpermitted-" + suffix(), null).signer();
given().filter(unpermitted).contentType("text/plain").body("a write")
.put("/" + fixture.bucket() + "/written.txt?uploadId=no-such-upload")
.then().statusCode(403).body(containsString("AccessDenied"));
given().filter(fixture.owner()).head("/" + fixture.bucket() + "/written.txt").then().statusCode(404);
}
@Test
void aPutNamingAHeldUploadButNoPartIsJudgedByItsOwnHeaders() {
EncryptionFixture fixture = kmsOnlyBucket();
S3RequestSigner kmsOnly = encryptionUser("kms-only-identity-" + suffix(), """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:*","Resource":"*",
"Condition":{"StringEquals":{"s3:x-amz-server-side-encryption":"aws:kms"}}}]}""").signer();
String uploadId = given().filter(kmsOnly).header(SSE, "aws:kms").header(SSE_KEY, BUCKET_KEY)
.post("/" + fixture.bucket() + "/held.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().filter(kmsOnly).contentType("text/plain").body("a write")
.put("/" + fixture.bucket() + "/written.txt?uploadId=" + uploadId)
.then().statusCode(403).body(containsString("AccessDenied"));
given().filter(fixture.owner()).delete("/" + fixture.bucket() + "/held.bin?uploadId=" + uploadId)
.then().statusCode(204);
}
@Test
void aPartCopyNamingAnUploadNotHeldIsChecked() {
EncryptionFixture fixture = kmsOnlyBucket();
S3RequestSigner unpermitted = encryptionUser("unpermitted-" + suffix(), null).signer();
given().filter(unpermitted).header("x-amz-copy-source", "/" + fixture.bucket() + "/source.txt")
.put("/" + fixture.bucket() + "/copied.bin?partNumber=1&uploadId=no-such-upload")
.then().statusCode(403).body(containsString("AccessDenied"));
}
private record EncryptionFixture(String bucket, S3RequestSigner owner, Credentials user) {
}
private static final String SSE = "x-amz-server-side-encryption";
private static final String SSE_KEY = "x-amz-server-side-encryption-aws-kms-key-id";
private static final String ALLOW_S3_AND_KMS = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:*","kms:*"],"Resource":"*"}]}""";
private static void assertEncryptedPut(EncryptionFixture fixture, String key, String encryption, String kmsKeyId,
int status) {
RequestSpecification request = given().filter(fixture.user().signer()).contentType("text/plain").body(key);
if (encryption != null) {
request.header(SSE, encryption);
}
if (kmsKeyId != null) {
request.header(SSE_KEY, kmsKeyId);
}
request.put("/" + fixture.bucket() + "/" + key).then().statusCode(status);
}
private static Response uploadPart(S3RequestSigner signer, EncryptionFixture fixture, String key, String uploadId) {
return given().filter(signer).contentType("application/octet-stream").body("a part".getBytes())
.put("/" + fixture.bucket() + "/" + key + "?partNumber=1&uploadId=" + uploadId);
}
/** A bucket defaulting to {@code aws:kms} that denies any other put, and a user allowed {@code s3:*} and {@code kms:*}. */
private EncryptionFixture kmsOnlyBucket() {
String suffix = suffix();
EncryptionFixture fixture = encryptionBucket("kms-only-" + suffix,
encryptionUser("kms-only-user-" + suffix, ALLOW_S3_AND_KMS), suffix, true);
putKmsOnlyPolicy(fixture);
return fixture;
}
/** A bucket with no default encryption and no policy yet, and a user allowed {@code s3:*} and {@code kms:*}. */
private EncryptionFixture bucketWithoutDefaultEncryption() {
String suffix = suffix();
return encryptionBucket("multipart-" + suffix,
encryptionUser("multipart-user-" + suffix, ALLOW_S3_AND_KMS), suffix, false);
}
/** Denies a put that does not name {@code aws:kms}, or names a key other than the bucket's. */
private static void putKmsOnlyPolicy(EncryptionFixture fixture) {
putBucketPolicyAs(fixture.bucket(), """
{"Version":"2012-10-17","Statement":[
{"Sid":"DenyUnencryptedPuts","Effect":"Deny","Principal":"*","Action":"s3:PutObject",
"Resource":"arn:aws:s3:::%1$s/*",
"Condition":{"StringNotEquals":{"s3:x-amz-server-side-encryption":"aws:kms"}}},
{"Sid":"DenyWrongKmsKey","Effect":"Deny","Principal":"*","Action":"s3:PutObject",
"Resource":"arn:aws:s3:::%1$s/*",
"Condition":{"StringNotEqualsIfExists":{"s3:x-amz-server-side-encryption-aws-kms-key-id":"%2$s"}}}]}
""".formatted(fixture.bucket(), BUCKET_KEY), fixture.owner());
}
/** A bucket whose policy alone allows a user with no identity policy, on {@code key} being {@code value}. */
private EncryptionFixture allowOnlyBucket(String key, String value) {
String suffix = suffix();
EncryptionFixture fixture = encryptionBucket("allow-only-" + suffix,
encryptionUser("allow-only-user-" + suffix, null), suffix, true);
putBucketPolicyAs(fixture.bucket(), """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"%s"},"Action":"s3:PutObject",
"Resource":"arn:aws:s3:::%s/*",
"Condition":{"StringEquals":{"%s":"%s"}}}]}
""".formatted(fixture.user().userArn(), fixture.bucket(), key, value), fixture.owner());
return fixture;
}
private static String suffix() {
return UUID.randomUUID().toString().substring(0, 8);
}
private Credentials encryptionUser(String userName, String policy) {
cleanup.register(() -> EnforcementFixtures.removeUser(endpoint.getPort(), auth(ACCOUNT_A, "iam"), userName));
Credentials user = createUser(userName, ACCOUNT_A);
if (policy != null) {
putUserPolicy(userName, "AllowEncryptedPuts", policy, ACCOUNT_A);
}
return user;
}
private EncryptionFixture encryptionBucket(String bucket, Credentials user, String suffix, boolean kmsDefault) {
String ownerName = "encryption-owner-" + suffix;
cleanup.register(() -> EnforcementFixtures.removeUser(endpoint.getPort(), auth(ACCOUNT_A, "iam"), ownerName));
Credentials owner = createAccountAdmin(ownerName, ACCOUNT_A);
cleanup.register(() -> EnforcementFixtures.removeBucket(endpoint.getPort(), owner.signer(), bucket));
createBucketAs(bucket, owner.signer());
if (kmsDefault) {
given().filter(owner.signer()).contentType("application/xml").body("""
<ServerSideEncryptionConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Rule>
<ApplyServerSideEncryptionByDefault><SSEAlgorithm>aws:kms</SSEAlgorithm>
<KMSMasterKeyID>%s</KMSMasterKeyID></ApplyServerSideEncryptionByDefault>
</Rule></ServerSideEncryptionConfiguration>""".formatted(BUCKET_KEY))
.put("/" + bucket + "?encryption").then().statusCode(200);
}
return new EncryptionFixture(bucket, owner.signer(), user);
}
}