From ce7f1ab1852018cc98ef43cbbc003560cdec08a7 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:33:19 -0400 Subject: [PATCH] refactor(iam): match S3 requests on the routed path, as every scope is (#9164) The registry matched an S3 request's route on the path as sent and every other scope's on the path the router dispatched on. S3 needs no exception: S3SlashOnlyKeyFilter rewrites a key of slashes alone escaped (PUT /bucket// is /bucket/%2F), which the routed path keeps as the filter wrote it, and the object a request names comes from S3's own reading of its key (RequestPathLabels), which keeps the slashes it was sent with. The routed path is also the one the router dispatched on, so a path it normalizes, such as /bucket/.., is checked as the operation that runs. A new integration test checks a key of slashes alone under IAM enforcement. --- .../aws/services/iam/IamActionRegistry.java | 9 +++++---- .../aws/services/iam/IamActionRegistryTest.java | 4 ++-- ...tOperationsIamEnforcementIntegrationTest.java | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) 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