mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): authorize a virtual-hosted bucket request against the bucket ARN (#3942)
S3VirtualHostFilter rewrites a bucket-level virtual-hosted request (GET /) to the path /bucket/, and ResourceArnBuilder.buildS3Arn turned that into arn:aws:s3:::bucket/ with a trailing slash. A policy naming the bucket as arn:aws:s3:::bucket then failed to match, so an allowed ListBucket was denied for every SDK that defaults to virtual-hosted addressing while the identical path-style call was allowed. Drop the separator when nothing follows it. A key that itself ends in a slash (a folder marker such as folder/) keeps it, because there the slash is key data rather than a separator. Refs lex00/floci#213 (cherry picked from commit 8624701021f2c6374b6e5d627218eeaa961ce751)
This commit is contained in:
1 parent
869589e932
commit
3c251b2343
3 files changed
+182
-3
No files matched your search
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+154
@@ -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.
|
||||
*
|
||||
* <p>{@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.
|
||||
*
|
||||
* <p>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("<Name>" + bucket + "</Name>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathStyleListBucketIsAllowedByTheSamePolicy() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String bucket = "path-iam-allow-" + suffix;
|
||||
String userName = "path-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("Authorization", auth(accessKeyId, "s3"))
|
||||
.when()
|
||||
.get("/" + bucket)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Name>" + bucket + "</Name>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void virtualHostedListBucketIsDeniedWithoutAGrant() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String bucket = "vhost-iam-deny-" + suffix;
|
||||
String userName = "vhost-denied-user-" + suffix;
|
||||
|
||||
createBucketAsRoot(bucket);
|
||||
String accessKeyId = createUser(userName);
|
||||
|
||||
given()
|
||||
.header("Host", bucket + ".localhost")
|
||||
.header("Authorization", auth(accessKeyId, "s3"))
|
||||
.when()
|
||||
.get("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"));
|
||||
}
|
||||
|
||||
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<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.iam.enforcement-enabled", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user