diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java index 6961f3f55b..4be04b78fe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java @@ -69,9 +69,14 @@ public class ResourceArnBuilder { if (stripped.isEmpty()) { return AwsArnUtils.Arn.of("s3", "", "", "*").toString(); } - int slash = stripped.indexOf('/'); - if (slash < 0) { - return AwsArnUtils.Arn.of("s3", "", "", stripped).toString(); + // S3VirtualHostFilter rewrites a bucket-level virtual-hosted request (GET /) to + // /bucket/, and the empty key after that separator is not part of the resource. + // A policy names the bucket as arn:aws:s3:::bucket, so the trailing slash has to + // go or a bucket-level ARN never matches. A key that itself ends in a slash + // (a folder marker such as folder/) keeps it, because there the slash is key data. + int firstSlash = stripped.indexOf('/'); + if (firstSlash == stripped.length() - 1) { + stripped = stripped.substring(0, firstSlash); } return AwsArnUtils.Arn.of("s3", "", "", stripped).toString(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java index d39c9e0cfd..5028d2e50c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java @@ -331,6 +331,26 @@ class ResourceArnBuilderTest { assertEquals("arn:aws:s3:::my-bucket/folder/file.json", arn); } + /** + * S3VirtualHostFilter rewrites a virtual-hosted bucket-level request (GET /) to + * /bucket/. The resource is still the bucket, so the ARN must carry no trailing + * slash or a policy naming arn:aws:s3:::bucket stops matching and an allowed + * ListBucket is denied for every SDK that defaults to virtual-hosted addressing. + */ + @Test + void s3BuildsBucketArnForVirtualHostedRewrittenPath() { + when(uriInfo.getPath()).thenReturn("/my-bucket/"); + String arn = builder.build("s3", ctx, "us-east-1", "000000000000"); + assertEquals("arn:aws:s3:::my-bucket", arn); + } + + @Test + void s3KeepsTrailingSlashOfAFolderMarkerKey() { + when(uriInfo.getPath()).thenReturn("/my-bucket/folder/"); + String arn = builder.build("s3", ctx, "us-east-1", "000000000000"); + assertEquals("arn:aws:s3:::my-bucket/folder/", arn); + } + // ── Lambda ────────────────────────────────────────────────────────────────── @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIamEnforcementIntegrationTest.java new file mode 100644 index 0000000000..28e1cebf21 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIamEnforcementIntegrationTest.java @@ -0,0 +1,154 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; + +/** + * Regression tests for the virtual-hosted bucket ARN reported in lex00/floci#213. + * + *
{@code S3VirtualHostFilter} rewrites a bucket-level virtual-hosted request + * ({@code GET /} with a {@code bucket.localhost} Host header) to the path {@code /bucket/}, + * and {@code ResourceArnBuilder} used to turn that into {@code arn:aws:s3:::bucket/}. A policy + * naming the bucket as {@code arn:aws:s3:::bucket} then failed to match, so an allowed + * ListBucket came back denied for every SDK that defaults to virtual-hosted addressing while + * the identical path-style call was allowed. + * + *
Only {@code floci.services.iam.enforcement-enabled} is turned on, matching
+ * {@code S3PresignedUrlIamEnforcementIntegrationTest}, so the requests carry a syntactically
+ * valid but unverified signature. IAM policy evaluation does not depend on the signature
+ * itself being checked.
+ */
+@QuarkusTest
+@TestProfile(S3VirtualHostIamEnforcementIntegrationTest.IamEnforcementProfile.class)
+class S3VirtualHostIamEnforcementIntegrationTest {
+
+ private static final String REGION = "us-east-1";
+
+ @Test
+ void virtualHostedListBucketIsAllowedByABucketArnPolicy() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String bucket = "vhost-iam-allow-" + suffix;
+ String userName = "vhost-allowed-user-" + suffix;
+
+ createBucketAsRoot(bucket);
+ String accessKeyId = createUser(userName);
+ putUserPolicy(userName, "AllowListBucket", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:aws:s3:::%1$s"}
+ ]}""".formatted(bucket));
+
+ given()
+ .header("Host", bucket + ".localhost")
+ .header("Authorization", auth(accessKeyId, "s3"))
+ .when()
+ .get("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("AccessDenied"));
+ }
+
+ private static void createBucketAsRoot(String bucket) {
+ given()
+ .header("Authorization", auth("test", "s3"))
+ .when()
+ .put("/" + bucket)
+ .then()
+ .statusCode(200);
+ }
+
+ private static String createUser(String userName) {
+ given()
+ .formParam("Action", "CreateUser")
+ .formParam("UserName", userName)
+ .header("Authorization", auth("test", "iam"))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ return given()
+ .formParam("Action", "CreateAccessKey")
+ .formParam("UserName", userName)
+ .header("Authorization", auth("test", "iam"))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract()
+ .path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
+ }
+
+ private static void putUserPolicy(String userName, String policyName, String policyDocument) {
+ given()
+ .formParam("Action", "PutUserPolicy")
+ .formParam("UserName", userName)
+ .formParam("PolicyName", policyName)
+ .formParam("PolicyDocument", policyDocument)
+ .header("Authorization", auth("test", "iam"))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static String auth(String accessKeyId, String service) {
+ return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20260629/" + REGION + "/" + service
+ + "/aws4_request, SignedHeaders=host, Signature=abc";
+ }
+
+ public static final class IamEnforcementProfile implements QuarkusTestProfile {
+ @Override
+ public Map