fix(iam): record a denied browser PostObject in CloudTrail (#6030)

Under IAM enforcement a denied S3 request is recorded in CloudTrail as
the operation that was refused, but a browser POST upload left no
record. Its credential is only in the form fields, so S3Controller
authorizes it through IamEnforcementFilter.authorizeAdditionalResource,
which threw AccessDenied without going through the denial emitter.

The POST path now calls a new authorizePostObject, which checks
s3:PutObject as before and, when IAM refuses it, records the denial as
PostObject before refusing. AWS's S3 developer guide lists PostObject
among the object-level API actions logged as CloudTrail events; the
current user guide's data events table no longer names it. The
emitter's recording half is split out so both paths share it, and a
record it cannot write is logged as a warning.

The denial is recorded in the Region the form's credential names, as a
presigned URL's is in the one its query credential names, so a trail
that logs only that Region gets it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-19 21:32:47 -05:00
1 parent 24a960a599
commit 37717537a4
4 files changed
+190 -17

No files matched your search

@@ -1,18 +1,32 @@
package io.github.hectorvent.floci.services.s3;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailLogWriter;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Regression coverage for the presigned POST gap in #3195/#3504: {@code S3Controller} only ran
@@ -35,6 +49,14 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest {
private static final DateTimeFormatter AMZ_DATE_FMT =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
@Inject
CloudTrailLogWriter trailWriter;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void presignedPostIsDeniedWhenIdentityPolicyDeniesPutObject() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
@@ -186,6 +208,96 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest {
.statusCode(204);
}
/**
* A browser POST is refused outside the filter that records IAM denials, since its credential is
* only in the form, so it left no CloudTrail record at all. It is now recorded as PostObject, the
* name AWS's S3 developer guide gives it among the object-level actions CloudTrail logs, in the
* Region its credential names: a trail that logs only that Region, not the default one, gets it.
*/
@Test
void aDeniedPresignedPostIsRecordedInCloudTrailAsPostObject() throws IOException {
String region = "eu-west-1";
String suffix = UUID.randomUUID().toString().substring(0, 8);
String bucket = "presigned-post-trail-" + suffix;
String logBucket = "presigned-post-trail-logs-" + suffix;
String trail = "presigned-post-trail-" + suffix;
String userName = "presigned-post-trail-user-" + suffix;
createBucketAsRoot(bucket, region);
createBucketAsRoot(logBucket, region);
cloudTrail(region, "CreateTrail", "{\"Name\":\"" + trail + "\",\"S3BucketName\":\"" + logBucket + "\"}");
cloudTrail(region, "PutEventSelectors", """
{"TrailName":"%s","EventSelectors":[{"ReadWriteType":"All","IncludeManagementEvents":false,
"DataResources":[{"Type":"AWS::S3::Object","Values":["arn:aws:s3:::%s/"]}]}]}"""
.formatted(trail, bucket));
cloudTrail(region, "StartLogging", "{\"Name\":\"" + trail + "\"}");
String accessKeyId = createUser(userName);
putUserPolicy(userName, "DenyPutObject", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Deny","Action":"s3:PutObject","Resource":"*"}
]}""");
try {
given()
.multiPart("key", "trailed.txt")
.multiPart("x-amz-credential", presignedCredential(accessKeyId, region))
.multiPart("file", "trailed.txt",
"should not be stored".getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + bucket)
.then()
.statusCode(403)
.body("Error.Code", equalTo("AccessDenied"));
trailWriter.flushNow();
List<JsonNode> records = trailRecords(logBucket);
assertTrue(records.stream().anyMatch(record -> "PostObject".equals(record.path("eventName").asText())
&& "AccessDenied".equals(record.path("errorCode").asText())
&& bucket.equals(record.path("requestParameters").path("bucketName").asText())
&& "trailed.txt".equals(record.path("requestParameters").path("key").asText())),
"Expected a PostObject AccessDenied record for " + bucket + "/trailed.txt: " + records);
} finally {
cloudTrail(region, "DeleteTrail", "{\"Name\":\"" + trail + "\"}");
}
}
private static void cloudTrail(String region, String operation, String body) {
given()
.header("Authorization", auth("test", "cloudtrail", region))
.header("X-Amz-Target", "CloudTrail_20131101." + operation)
.contentType("application/x-amz-json-1.1")
.body(body)
.when()
.post("/")
.then()
.statusCode(200);
}
/** Every record in the log files the trail delivered to {@code logBucket}. */
private static List<JsonNode> trailRecords(String logBucket) throws IOException {
String listing = given()
.header("Authorization", auth("test", "s3"))
.when()
.get("/" + logBucket + "?list-type=2")
.then()
.statusCode(200)
.extract().asString();
List<JsonNode> records = new ArrayList<>();
Matcher key = Pattern.compile("<Key>([^<]+\\.json\\.gz)</Key>").matcher(listing);
while (key.find()) {
byte[] compressed = given()
.header("Authorization", auth("test", "s3"))
.when()
.get("/" + logBucket + "/" + key.group(1))
.then()
.statusCode(200)
.extract().asByteArray();
try (GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
new ObjectMapper().readTree(in.readAllBytes()).path("Records").forEach(records::add);
}
}
return records;
}
private static void putBucketPolicy(String bucket, String policyDocument) {
given()
.header("Authorization", auth("test", "s3"))
@@ -198,9 +310,13 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest {
}
private static String presignedCredential(String accessKeyId) {
return presignedCredential(accessKeyId, REGION);
}
private static String presignedCredential(String accessKeyId, String region) {
String amzDate = AMZ_DATE_FMT.format(Instant.now());
String dateStamp = amzDate.substring(0, 8);
return accessKeyId + "/" + dateStamp + "/" + REGION + "/s3/aws4_request";
return accessKeyId + "/" + dateStamp + "/" + region + "/s3/aws4_request";
}
private static void createBucketAsRoot(String bucket) {
@@ -212,6 +328,17 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest {
.statusCode(200);
}
private static void createBucketAsRoot(String bucket, String region) {
given()
.header("Authorization", auth("test", "s3", region))
.body("<CreateBucketConfiguration><LocationConstraint>" + region
+ "</LocationConstraint></CreateBucketConfiguration>")
.when()
.put("/" + bucket)
.then()
.statusCode(200);
}
private static String createUser(String userName) {
return createUser(userName, null);
}
@@ -259,8 +386,12 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest {
}
private static String auth(String accessKeyId, String service) {
return auth(accessKeyId, service, REGION);
}
private static String auth(String accessKeyId, String service, String region) {
String amzDate = AMZ_DATE_FMT.format(Instant.now()).substring(0, 8);
return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/" + amzDate + "/" + REGION + "/" + service
return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/" + amzDate + "/" + region + "/" + service
+ "/aws4_request, SignedHeaders=host, Signature=abc";
}
}