diff --git a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java index 2359f17b89..d146938a22 100644 --- a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java +++ b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java @@ -626,9 +626,10 @@ public class IamActionRegistry { */ public String resolveRoute(String credentialScope, ContainerRequestContext ctx) { String method = ctx.getMethod().toUpperCase(); - // An S3 key may end in a slash, and one of slashes alone arrives escaped as S3SlashOnlyKeyFilter rewrites - // it (PUT /bucket// writes the key "/"), so an S3 path is matched as sent, not decoded. - String path = "s3".equals(credentialScope) ? sentPath(ctx.getUriInfo()) : routedPath(ctx); + // Matched on the path the router dispatched on, as every scope is: S3 too, whose key of slashes alone + // S3SlashOnlyKeyFilter rewrites escaped (PUT /bucket// writes the key "/"), and whose own reading of a key, + // which keeps the slashes it was sent with, names the object (RequestPathLabels). + String path = routedPath(ctx); for (ActionRule rule : RULES) { if (rule.matches(credentialScope, method, path, ctx)) { @@ -736,7 +737,7 @@ public class IamActionRegistry { || ctx.getUriInfo().getRequestUri() == null) { return false; } - String path = "s3".equals(credentialScope) ? sentPath(ctx.getUriInfo()) : routedPath(ctx); + String path = routedPath(ctx); return IamRestRoutes.vendored().ambiguous(credentialScope, ctx.getMethod().toUpperCase(), withoutInternalPrefix(ctx, path), ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx), ctx.getHeaders()); diff --git a/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java b/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java index cc0715c78c..103a7c206b 100644 --- a/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java @@ -637,8 +637,8 @@ class IamActionRegistryTest { @Test void aTrailingSlashIsCheckedAsTheRouteWithoutIt() { // The router matches /clusters/ as /clusters, so the rule ending at the route's last segment covers it. - // An S3 path is matched as sent: a key may end in a slash, and one of slashes alone arrives as - // S3SlashOnlyKeyFilter rewrites it, escaped. + // An S3 key may end in a slash, and one of slashes alone arrives as S3SlashOnlyKeyFilter rewrites it, + // escaped. String[][] routes = { {"eks", "POST", "/clusters/", "eks:CreateCluster"}, {"eks", "GET", "/clusters/c/node-groups/ng/", "eks:DescribeNodegroup"}, diff --git a/src/test/java/io/floci/aws/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java index cf610089ca..d808410e88 100644 --- a/src/test/java/io/floci/aws/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java @@ -272,6 +272,22 @@ class S3ObjectOperationsIamEnforcementIntegrationTest { .when().delete(f.path("a/governed")).then().statusCode(204); } + /** + * A key of slashes alone, "/" sent as "/bucket//", is that object, checked as the object operation on it, not as + * the bucket its path would name without them. + */ + @Test + void aKeyOfOnlySlashesIsCheckedAsThatObject() { + Fixture f = new Fixture("slash-only", false); + f.policy(f.allow("s3:PutObject", "s3:GetObject", "s3:DeleteObject")); + HttpResponse put = f.raw("PUT", "/", null, f.accessKeyId, Map.of(), "one slash"); + assertEquals(200, put.statusCode(), put.body()); + HttpResponse get = f.raw("GET", "/", null, f.accessKeyId, Map.of(), null); + assertEquals(200, get.statusCode(), get.body()); + assertEquals("one slash", get.body()); + assertEquals(204, f.raw("DELETE", "/", null, f.accessKeyId, Map.of(), null).statusCode()); + } + @Test void theBypassIsDecidedOnTheKeyARequestNamesEvenWithALeadingSlash() { // A key that starts with "/" travels as "//" after the bucket, which JAX-RS collapses to the