fix(iam): match requests moved under an internal prefix on their model's routes (#8067)

This commit is contained in:
mrfelek authored and GitHub committed 2026-11-12 07:40:00 -04:00
1 parent 94ba685306
commit f6b95fda16
10 files changed
+248 -126

No files matched your search

@@ -708,6 +708,49 @@ class IamRestRouteEnforcementIntegrationTest {
as(user, region, "guardduty").get("/detector").then().statusCode(200);
}
/**
* A GuardDuty malware protection plan request, which GuardDutyRouteFilter moves onto {@code /_guardduty}, is
* checked as the model's route for it, on the plan its path names, and so is one sent to {@code /_guardduty}
* itself: a caller with no grant is refused before a plan is created, a grant scoped to the plan lets
* {@code GetMalwareProtectionPlan} read it and refuses another, and a deny on the plan keeps it from being
* deleted under an allow on everything.
*/
@Test
void aGuardDutyMalwareProtectionPlanRequestIsCheckedOnThePlanItsPathNames() {
String user = user("route-plan");
String region = "eu-west-3";
String plan = "{\"role\":\"arn:aws:iam::" + ACCOUNT + ":role/malware-scan\","
+ "\"protectedResource\":{\"s3Bucket\":{\"bucketName\":\"route-plan-" + suffix() + "\"}}}";
refused(as(user, region, "guardduty").body(plan).post("/malware-protection-plan").then(),
"guardduty:CreateMalwareProtectionPlan");
refused(as(user, region, "guardduty").get("/malware-protection-plan").then(),
"guardduty:ListMalwareProtectionPlans");
// GuardDuty refuses a second plan for one bucket, so this create also shows the refused one made none.
String id = given().header("Authorization", auth(ADMIN, region, "guardduty"))
.contentType("application/json").body(plan).post("/malware-protection-plan")
.then().statusCode(200).extract().path("malwareProtectionPlanId");
cleanup.register(() -> given().header("Authorization", auth(ADMIN, region, "guardduty"))
.delete("/malware-protection-plan/" + id));
String arn = "arn:aws:guardduty:" + region + ":" + ACCOUNT + ":malware-protection-plan/" + id;
refused(as(user, region, "guardduty").get("/malware-protection-plan/" + id).then(),
"guardduty:GetMalwareProtectionPlan");
refused(as(user, region, "guardduty").get("/_guardduty/malware-protection-plan/" + id).then(),
"guardduty:GetMalwareProtectionPlan");
policy(user, allow("guardduty:GetMalwareProtectionPlan", arn));
as(user, region, "guardduty").get("/malware-protection-plan/" + id).then().statusCode(200)
.body("arn", equalTo(arn));
as(user, region, "guardduty").get("/_guardduty/malware-protection-plan/" + id).then().statusCode(200)
.body("arn", equalTo(arn));
refused(as(user, region, "guardduty").get("/malware-protection-plan/other" + id).then(),
"guardduty:GetMalwareProtectionPlan");
policy(user, allow("guardduty:*", "*") + "," + deny("guardduty:DeleteMalwareProtectionPlan", arn));
refused(as(user, region, "guardduty").delete("/malware-protection-plan/" + id).then(),
"guardduty:DeleteMalwareProtectionPlan");
as(user, region, "guardduty").get("/malware-protection-plan/" + id).then().statusCode(200);
}
/**
* A segment ending in a matrix parameter, which the router ignores, is checked as the route it reaches, on the
* resource its path names: the routed path carries no matrix parameter, so the rule matches and the ARN is the
@@ -161,6 +161,22 @@ class IamRestRoutesTest {
assertEquals(Optional.of("GetRoot"), get("/"));
}
/**
* A path a route filter moved under an internal prefix, given without it, was matched decoded, so a label's value
* may hold the slashes its encoding hid: when the path matches no route with each label taking one segment, its
* labels take the segments their values span. A route that matches with one segment a label still wins, and an
* ordinary path's labels never span.
*/
@Test
void aPathMovedUnderAPrefixMayHoldTheSlashesItsLabelsWereDecodedTo() {
assertEquals(Optional.of("GetThing"), routes.operation("svc", "GET", "/things/web/part", null, true));
assertEquals(Optional.empty(), routes.operation("svc", "GET", "/things/web/part", null, false));
assertEquals(Map.of("ThingName", "a/b", "PartPath", "c"),
routes.match("svc", "GET", "/things/a/b/parts/c", null, true)
.map(IamRestRoutes.Match::labels).orElse(Map.of()));
assertEquals(Optional.of("ListParts"), routes.operation("svc", "GET", "/things/web/parts/list", null, true));
}
/**
* A literal segment matches itself alone, a dot in it no other character, and may end in a matrix parameter,
* which RESTEasy keeps in the path it matched.
@@ -1,5 +1,6 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.SignedServiceRoute;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.Test;
@@ -16,10 +17,10 @@ import static org.mockito.Mockito.when;
*
* <p>The key of a data-plane request may be named by its identifier or by its ARN, and an ARN
* carries a slash that a client sends encoded. The router rewrites the request onto the
* internal prefix, and the IAM rules are matched on the rewritten path, which is decoded. So
* the ARN arrives as a path that holds a real slash, and a rule that stops at the first slash
* would not match it: the request would resolve to no action, and a policy that denies the
* action would not be enforced.
* internal prefix, and the vendored routes, at the model's paths, are matched on the rewritten
* path without it, which is decoded. So the ARN arrives as a path that holds a real slash, and
* a route whose label stops at the first slash would not match it: the request would resolve to
* no action, and a policy that denies the action would not be enforced.
*
* <p>The rest of the repository holds a request to its standard: a request is refused however
* it is sent. This is that standard for the data plane.
@@ -122,6 +123,8 @@ class PaymentCryptographyDataPlaneIamRouteTest {
when(uriInfo.getBaseUri()).thenReturn(URI.create("http://localhost:4566/"));
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getMethod()).thenReturn("POST");
// The data plane's route filter records the prefix it moves the request under.
when(ctx.getProperty(SignedServiceRoute.INTERNAL_PREFIX_PROPERTY)).thenReturn("/_paymentcryptography");
return registry.resolveRoute(scope, ctx);
}
}
@@ -59,12 +59,11 @@ class PaymentCryptographyDataPlaneRouteFilterTest {
Set<String> prefixes = new LinkedHashSet<>();
routes.fields().forEachRemaining(entry -> {
String uri = entry.getValue().path("requestUri").asText();
// The route is the model's path under the internal prefix.
assertTrue(uri.startsWith(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX),
"the route " + uri + " does not sit under the internal prefix");
String rest = uri.substring(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX.length());
assertTrue(rest.startsWith("/"), "the route " + uri + " names no path");
rest = rest.substring(1);
// The route is the model's path: IAM matches a moved request on its path without the prefix.
assertTrue(!uri.startsWith(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX),
"the route " + uri + " sits under the internal prefix, not at the model's path");
assertTrue(uri.startsWith("/"), "the route " + uri + " names no path");
String rest = uri.substring(1);
int slash = rest.indexOf('/');
assertTrue(slash > 0, "the route " + uri + " names no path");
prefixes.add("/" + rest.substring(0, slash));