fix(s3): enforce auth on object write paths when enforce-auth is enabled (#2326)

PutObject/DeleteObject and their subresources (tagging, retention,
legal-hold, acl, multipart, copy, batch delete, restore) accepted
anonymous requests even with enforce-auth enabled, unlike GetObject.

Also fixes: batch delete failing whole on a bad access key instead of
masking it per-key; explicit policy Deny taking precedence over ACL
grants; public-read-write ACL scoped to creating a new key only, per
AWS's ACL docs, never overwrite/delete; DeleteObject vs
DeleteObjectVersion as distinct actions; and BypassGovernanceRetention
required separately from DeleteObject/PutObjectRetention.

Fixes #2318

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-08-15 22:11:09 -04:00
1 parent 573d4bdaff
commit 16a8f042e7
5 files changed
+812 -5

No files matched your search

@@ -23,6 +23,7 @@ import javax.crypto.spec.SecretKeySpec;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@QuarkusTest
@TestProfile(S3AuthEnforcementIntegrationTest.S3AuthProfile.class)
@@ -37,12 +38,22 @@ class S3AuthEnforcementIntegrationTest {
private static final String DENY_BUCKET = "auth-deny-bucket";
private static final String GET_ONLY_BUCKET = "auth-get-only-bucket";
private static final String VERSION_BUCKET = "auth-version-bucket";
private static final String WRITE_BUCKET = "auth-write-bucket";
private static final String PUBLIC_WRITE_BUCKET = "auth-public-write-bucket";
private static final String PUBLIC_WRITE_ACL_BUCKET = "auth-public-write-acl-bucket";
private static final String DENY_WRITE_ACL_BUCKET = "auth-deny-write-acl-bucket";
private static final String DELETE_VERSION_BUCKET = "auth-delete-version-bucket";
private static final String BYPASS_BUCKET = "auth-bypass-governance-bucket";
private static final String BYPASS_KEY = "bypass-target.txt";
private static final String CONDITIONAL_DENY_ACL_BUCKET = "auth-conditional-deny-acl-bucket";
private static final String PUBLIC_KEY = "public.txt";
private static final String PRIVATE_KEY = "private.txt";
private static final String ERROR_KEY = "error.html";
private static final String ACL_KEY = "acl-list.txt";
private static final String DENY_KEY = "deny.txt";
private static final String VERSION_KEY = "versioned.txt";
private static final String WRITE_KEY = "signed.txt";
private static final String ANON_WRITE_KEY = "anon.txt";
private static final String SIGNING_TIMESTAMP = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'")
.withZone(ZoneOffset.UTC)
.format(Instant.now());
@@ -58,6 +69,7 @@ class S3AuthEnforcementIntegrationTest {
given().when().put("/" + PRIVATE_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("public body")
.when()
.put("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
@@ -65,6 +77,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("private body")
.when()
.put("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
@@ -229,6 +242,7 @@ class S3AuthEnforcementIntegrationTest {
given().when().put("/" + WEBSITE_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.contentType("text/html")
.body("<html>index</html>")
.when()
@@ -275,6 +289,7 @@ class S3AuthEnforcementIntegrationTest {
given().when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.contentType("text/html")
.body("<html>private index</html>")
.when()
@@ -283,6 +298,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.contentType("text/html")
.body("<html>denied</html>")
.when()
@@ -323,6 +339,7 @@ class S3AuthEnforcementIntegrationTest {
given().when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("listed")
.when()
.put("/" + BUCKET_ACL_BUCKET + "/" + ACL_KEY)
@@ -351,6 +368,7 @@ class S3AuthEnforcementIntegrationTest {
given().when().put("/" + DENY_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-acl", "public-read")
.body("denied")
.when()
@@ -499,6 +517,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
String versionId = given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("versioned body")
.when()
.put("/" + VERSION_BUCKET + "/" + VERSION_KEY)
@@ -652,6 +671,651 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AuthorizationQueryParametersError"));
}
@Test
@Order(28)
void unsignedRequestCannotPutObject() {
given().when().put("/" + WRITE_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("signed body")
.when()
.put("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
.statusCode(200);
given()
.body("injected-by-anonymous")
.when()
.put("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(404);
}
@Test
@Order(29)
void unsignedRequestCannotDeleteObject() {
given()
.when()
.delete("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
.statusCode(200)
.body(equalTo("signed body"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.delete("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
.statusCode(204);
}
@Test
@Order(30)
void signedRequestWithBadAccessKeyCannotWriteObject() {
given()
.header("Authorization", BAD_AUTH_HEADER)
.body("bad key body")
.when()
.put("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("InvalidAccessKeyId"));
given()
.header("Authorization", BAD_AUTH_HEADER)
.when()
.delete("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("InvalidAccessKeyId"));
}
@Test
@Order(31)
void bucketPolicyCanExplicitlyAllowAnonymousWrite() {
given().when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200);
given()
.contentType("application/json")
.body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:PutObject"))
.when()
.put("/" + PUBLIC_WRITE_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.body("public write body")
.when()
.put("/" + PUBLIC_WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(200);
given()
.contentType("application/json")
.body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:DeleteObject"))
.when()
.put("/" + PUBLIC_WRITE_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.when()
.delete("/" + PUBLIC_WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(204);
}
@Test
@Order(32)
void unsignedRequestCannotWriteObjectSubresources() {
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("subresource body")
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt")
.then()
.statusCode(200);
given()
.contentType("application/xml")
.body("<Tagging><TagSet><Tag><Key>a</Key><Value>b</Value></Tag></TagSet></Tagging>")
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt?tagging")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.contentType("application/xml")
.body("<Retention><Mode>GOVERNANCE</Mode><RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate></Retention>")
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt?retention")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.contentType("application/xml")
.body("<LegalHold><Status>ON</Status></LegalHold>")
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt?legal-hold")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.contentType("application/xml")
.body(publicReadAcl())
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt?acl")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.when()
.delete("/" + WRITE_BUCKET + "/subresource.txt?tagging")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
@Test
@Order(33)
void unsignedRequestCannotWriteViaMultipartOrRestore() {
given()
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?uploads")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
String uploadId = given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?uploads")
.then()
.statusCode(200)
.extract().body().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body("part data")
.when()
.put("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId + "&partNumber=1")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
String eTag = given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("part data")
.when()
.put("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId + "&partNumber=1")
.then()
.statusCode(200)
.extract().header("ETag");
given()
.contentType("application/xml")
.body(completeMultipartBody(eTag))
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.when()
.delete("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.contentType("application/xml")
.body(completeMultipartBody(eTag))
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId)
.then()
.statusCode(200);
given()
.contentType("application/xml")
.body("<RestoreRequest><Days>1</Days></RestoreRequest>")
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?restore")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
@Test
@Order(34)
void unsignedRequestCannotCopyObject() {
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("copy source body")
.when()
.put("/" + WRITE_BUCKET + "/copy-source.txt")
.then()
.statusCode(200);
given()
.header("x-amz-copy-source", "/" + WRITE_BUCKET + "/copy-source.txt")
.when()
.put("/" + WRITE_BUCKET + "/copy-dest.txt")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-copy-source", "/" + WRITE_BUCKET + "/copy-source.txt")
.when()
.put("/" + WRITE_BUCKET + "/copy-dest.txt")
.then()
.statusCode(200);
}
@Test
@Order(35)
void batchDeleteObjectsDeniesUnauthorizedKeysButAllowsAuthorized() {
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("batch a")
.when()
.put("/" + WRITE_BUCKET + "/batch-a.txt")
.then()
.statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("batch b")
.when()
.put("/" + WRITE_BUCKET + "/batch-b.txt")
.then()
.statusCode(200);
String deleteXml = """
<Delete>
<Object><Key>batch-a.txt</Key></Object>
<Object><Key>batch-b.txt</Key></Object>
</Delete>
""";
given()
.contentType("application/xml")
.body(deleteXml)
.when()
.post("/" + WRITE_BUCKET + "?delete")
.then()
.statusCode(200)
.body(containsString("<Code>AccessDenied</Code>"))
.body(containsString("<Key>batch-a.txt</Key>"))
.body(containsString("<Key>batch-b.txt</Key>"))
.body(not(containsString("<Deleted>")));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + WRITE_BUCKET + "/batch-a.txt")
.then()
.statusCode(200)
.body(equalTo("batch a"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.contentType("application/xml")
.body(deleteXml)
.when()
.post("/" + WRITE_BUCKET + "?delete")
.then()
.statusCode(200)
.body(containsString("<Deleted>"))
.body(not(containsString("AccessDenied")));
}
@Test
@Order(36)
void batchDeleteObjectsWithUnknownAccessKeyFailsWholeRequest() {
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("bad-key batch a")
.when()
.put("/" + WRITE_BUCKET + "/bad-key-batch-a.txt")
.then()
.statusCode(200);
String deleteXml = """
<Delete>
<Object><Key>bad-key-batch-a.txt</Key></Object>
</Delete>
""";
given()
.header("Authorization", BAD_AUTH_HEADER)
.contentType("application/xml")
.body(deleteXml)
.when()
.post("/" + WRITE_BUCKET + "?delete")
.then()
.statusCode(403)
.body("Error.Code", equalTo("InvalidAccessKeyId"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + WRITE_BUCKET + "/bad-key-batch-a.txt")
.then()
.statusCode(200)
.body(equalTo("bad-key batch a"));
}
@Test
@Order(37)
void bucketAclPublicReadWriteAllowsAnonymousCreateButNotOverwriteOrDelete() {
// Per AWS's ACL docs, a bucket-ACL WRITE grant to a non-owner (like AllUsers) "denies
// non-owners the ability to overwrite or delete existing objects" -- it only lets them
// create new ones.
given().when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "?acl")
.then()
.statusCode(200);
given()
.body("anonymous via public-read-write ACL")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + PUBLIC_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(200)
.body(equalTo("anonymous via public-read-write ACL"));
given()
.body("overwrite attempt")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.when()
.delete("/" + PUBLIC_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
@Test
@Order(38)
void explicitPolicyDenyOverridesPublicWriteAcl() {
given().when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + DENY_WRITE_ACL_BUCKET + "?acl")
.then()
.statusCode(200);
given()
.contentType("application/json")
.body(denyObjectActionPolicy(DENY_WRITE_ACL_BUCKET, "s3:PutObject"))
.when()
.put("/" + DENY_WRITE_ACL_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.body("should stay denied despite public-read-write ACL")
.when()
.put("/" + DENY_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
private static String denyObjectActionPolicy(String bucket, String action) {
return """
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Deny",
"Principal": "*",
"Action": "%s",
"Resource": "arn:aws:s3:::%s/*"
}
}
""".formatted(action, bucket);
}
@Test
@Order(39)
void publicWriteAclDoesNotAuthorizeObjectSubresources() {
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("subresource body")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/acl-subresource.txt")
.then()
.statusCode(200);
given()
.contentType("application/xml")
.body("<Tagging><TagSet><Tag><Key>a</Key><Value>b</Value></Tag></TagSet></Tagging>")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/acl-subresource.txt?tagging")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
given()
.contentType("application/xml")
.body(publicReadAcl())
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/acl-subresource.txt?acl")
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
@Test
@Order(40)
void deleteObjectPolicyGrantDoesNotAuthorizeVersionedDelete() {
given().when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200);
given()
.contentType("application/xml")
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
.put("/" + DELETE_VERSION_BUCKET + "?versioning")
.then()
.statusCode(200);
String versionId = given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("versioned delete target")
.when()
.put("/" + DELETE_VERSION_BUCKET + "/" + VERSION_KEY)
.then()
.statusCode(200)
.extract().header("x-amz-version-id");
given()
.contentType("application/json")
.body(publicObjectActionPolicy(DELETE_VERSION_BUCKET, "s3:DeleteObject"))
.when()
.put("/" + DELETE_VERSION_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.when()
.delete("/" + DELETE_VERSION_BUCKET + "/" + VERSION_KEY)
.then()
.statusCode(204);
given()
.when()
.delete("/" + DELETE_VERSION_BUCKET + "/" + VERSION_KEY + "?versionId=" + versionId)
.then()
.statusCode(403)
.body("Error.Code", equalTo("AccessDenied"));
}
@Test
@Order(41)
void deleteObjectDoesNotBypassGovernanceRetentionWithoutDistinctPermission() {
given().when().put("/" + BYPASS_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("locked candidate")
.when()
.put("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)
.then()
.statusCode(200);
given()
.contentType("application/json")
.body(publicObjectActionPolicy(BYPASS_BUCKET, "s3:DeleteObject"))
.when()
.put("/" + BYPASS_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.header("x-amz-bypass-governance-retention", "true")
.when()
.delete("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)
.then()
.statusCode(403)
.body("Error.Code", equalTo("AccessDenied"));
given()
.contentType("application/json")
.body(deleteAndBypassGovernancePolicy(BYPASS_BUCKET))
.when()
.put("/" + BYPASS_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.header("x-amz-bypass-governance-retention", "true")
.when()
.delete("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)
.then()
.statusCode(204);
}
private static String deleteAndBypassGovernancePolicy(String bucket) {
return """
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:DeleteObject", "s3:BypassGovernanceRetention"],
"Resource": "arn:aws:s3:::%s/*"
}
}
""".formatted(bucket);
}
@Test
@Order(42)
void conditionalPolicyDenyFailsClosedOverridingPublicWriteAcl() {
// Floci has no request context to evaluate a Condition against, so a conditional Deny
// is treated as applying (fail closed) -- consistent with S3PublicAccessEvaluatorTest's
// conditionalDenyFailsClosedAndOverridesAllow, this must also override the ACL fallback.
given().when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + CONDITIONAL_DENY_ACL_BUCKET + "?acl")
.then()
.statusCode(200);
given()
.contentType("application/json")
.body(conditionalDenyObjectActionPolicy(CONDITIONAL_DENY_ACL_BUCKET, "s3:PutObject"))
.when()
.put("/" + CONDITIONAL_DENY_ACL_BUCKET + "?policy")
.then()
.statusCode(200);
given()
.body("should stay denied despite the public-read-write ACL")
.when()
.put("/" + CONDITIONAL_DENY_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
.statusCode(403)
.body(containsString("AccessDenied"));
}
private static String conditionalDenyObjectActionPolicy(String bucket, String action) {
return """
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Deny",
"Principal": "*",
"Action": "%s",
"Resource": "arn:aws:s3:::%s/*",
"Condition": {
"StringEquals": {
"aws:SourceIp": "203.0.113.0/24"
}
}
}
}
""".formatted(action, bucket);
}
private static String completeMultipartBody(String eTag) {
return """
<CompleteMultipartUpload>
<Part>
<PartNumber>1</PartNumber>
<ETag>%s</ETag>
</Part>
</CompleteMultipartUpload>
""".formatted(eTag);
}
private static String publicReadPolicy(String bucket) {
return """
{