diff --git a/src/main/java/io/floci/aws/core/common/AwsRestRouteScopeFilter.java b/src/main/java/io/floci/aws/core/common/AwsRestRouteScopeFilter.java index 6d1cb07506..659e995102 100644 --- a/src/main/java/io/floci/aws/core/common/AwsRestRouteScopeFilter.java +++ b/src/main/java/io/floci/aws/core/common/AwsRestRouteScopeFilter.java @@ -1,6 +1,15 @@ package io.floci.aws.core.common; import io.floci.aws.config.EmulatorConfig; +import io.floci.aws.services.apigateway.ApiGatewayController; +import io.floci.aws.services.appconfig.AppConfigController; +import io.floci.aws.services.aps.ApsController; +import io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreResourcePolicyController; +import io.floci.aws.services.codegurureviewer.CodeGuruReviewerController; +import io.floci.aws.services.dsql.DsqlController; +import io.floci.aws.services.iot.IotController; +import io.floci.aws.services.msk.MskController; +import io.floci.aws.services.rdsdata.RdsDataController; import io.floci.aws.services.s3.S3Controller; import jakarta.annotation.Priority; import jakarta.inject.Inject; @@ -12,9 +21,15 @@ import jakarta.ws.rs.core.Context; import jakarta.ws.rs.ext.Provider; import org.jboss.logging.Logger; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; + /** - * Stops a known REST JSON service request from being handled by S3's path-style wildcard routes. - * This runs after route matching so {@link ResourceInfo} identifies a genuine S3 endpoint match, + * Stops a known REST JSON service request from being handled by S3's path-style wildcard routes + * or by another service controller sharing the path template (Issue #6426). + * This runs after route matching so {@link ResourceInfo} identifies a genuine endpoint match, * complementing the pre-matching unknown-scope guard in {@link AwsProtocolClaimFilter}. */ @Provider @@ -26,6 +41,29 @@ public class AwsRestRouteScopeFilter implements ContainerRequestFilter { @Context ResourceInfo resourceInfo; + record RouteCollision(Class controllerClass, Predicate paths, Set conflictingScopes) { + RouteCollision(Class controllerClass, String pathPrefix, Set conflictingScopes) { + this(controllerClass, SignedServiceRoute.under(pathPrefix), conflictingScopes); + } + } + + // Issue #6426: Map of controllers, path prefixes, and foreign signing service scopes that collide + // on shared JAX-RS path templates when operations are unimplemented for the signed service. + private static final List ROUTE_COLLISIONS = List.of( + new RouteCollision(ApiGatewayController.class, "/v2/apis", Set.of("appsync")), + new RouteCollision(AppConfigController.class, "/applications", + Set.of("app-integrations", "servicecatalog")), + new RouteCollision(MskController.class, "/v1/configurations", Set.of("mq")), + new RouteCollision(CodeGuruReviewerController.class, "/associations", Set.of("securityhub")), + new RouteCollision(IotController.class, "/jobs", Set.of("macie2")), + new RouteCollision(IotController.class, "/rules", Set.of("connect")), + new RouteCollision(ApsController.class, "/workspaces", Set.of("connect")), + new RouteCollision(DsqlController.class, "/cluster/get", Set.of("inspector2")), + new RouteCollision(RdsDataController.class, "/CommitTransaction", Set.of("lakeformation")), + new RouteCollision(BedrockAgentCoreResourcePolicyController.class, "/resourcepolicy", + Set.of("bedrock")) + ); + private final ResolvedServiceCatalog catalog; private final jakarta.inject.Provider configProvider; @@ -40,27 +78,52 @@ public class AwsRestRouteScopeFilter implements ContainerRequestFilter { public void filter(ContainerRequestContext ctx) { Object claimValue = ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY); if (!(claimValue instanceof ProtocolClaim claim) || claim.protocol() != WireProtocol.REST - || resourceInfo == null || resourceInfo.getResourceClass() != S3Controller.class) { + || resourceInfo == null) { return; } - SigV4CredentialScope.serviceName(ctx.getHeaderString("Authorization")) - .flatMap(catalog::byCredentialScope) - .filter(descriptor -> descriptor.supportsProtocol(ServiceProtocol.REST_JSON)) - .filter(descriptor -> !"s3".equals(descriptor.externalKey())) - .ifPresent(descriptor -> rejectS3Fallthrough(ctx, descriptor)); + Class resourceClass = RoutedResource.resourceClass(resourceInfo); + if (resourceClass == null) { + return; + } + + if (resourceClass == S3Controller.class) { + SigV4CredentialScope.serviceName(ctx.getHeaderString("Authorization")) + .flatMap(catalog::byCredentialScope) + .filter(descriptor -> descriptor.supportsProtocol(ServiceProtocol.REST_JSON)) + .filter(descriptor -> !"s3".equals(descriptor.externalKey())) + .ifPresent(descriptor -> reject(ctx, descriptor.externalKey(), "an S3 wildcard route")); + return; + } + + // Issue #6426: Reject cross-service REST calls matching another service's controller route + String rawPath = ctx.getUriInfo().getPath(); + String normalizedPath = rawPath.startsWith("/") ? rawPath : "/" + rawPath; + Optional scope = SignedServiceRoute.signingService(ctx); + if (scope.isEmpty()) { + return; + } + + for (RouteCollision collision : ROUTE_COLLISIONS) { + if (collision.controllerClass() == resourceClass + && collision.paths().test(normalizedPath) + && collision.conflictingScopes().contains(scope.get())) { + reject(ctx, scope.get(), "a colliding route on another service"); + return; + } + } } - private void rejectS3Fallthrough(ContainerRequestContext ctx, ServiceDescriptor descriptor) { + private void reject(ContainerRequestContext ctx, String service, String matchedRoute) { if (!configProvider.get().protocols().rejectUnknownServiceScope()) { - LOG.debugv("Known REST JSON service {0} matched an S3 wildcard route; rejection disabled by config", - descriptor.externalKey()); + LOG.debugv("Known REST JSON service {0} matched {1}; rejection disabled by config", + service, matchedRoute); return; } String method = ctx.getMethod(); String path = ctx.getUriInfo().getPath(); - LOG.infov("Rejecting {0} request that matched an S3 wildcard route: {1} {2}", - descriptor.externalKey(), method, path); + LOG.infov("Rejecting {0} request that matched {1}: {2} {3}", + service, matchedRoute, method, path); ctx.abortWith(AwsProtocolClaimFilter.unknownOperationResponse(404, "Unknown operation: " + method + " " + path)); } diff --git a/src/test/java/io/floci/aws/core/common/RestCrossServiceRouteCollisionIntegrationTest.java b/src/test/java/io/floci/aws/core/common/RestCrossServiceRouteCollisionIntegrationTest.java new file mode 100644 index 0000000000..cab3cf1d17 --- /dev/null +++ b/src/test/java/io/floci/aws/core/common/RestCrossServiceRouteCollisionIntegrationTest.java @@ -0,0 +1,137 @@ +package io.floci.aws.core.common; + +import io.floci.aws.testing.PartitionMatrix; +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.NullAndEmptySource; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +/** + * Verifies that REST calls for unimplemented operations signed for a service sharing a path + * with another service's controller are rejected with 404 UnknownOperationException before + * reaching the unrelated controller (issue #6426). + */ +@QuarkusTest +class RestCrossServiceRouteCollisionIntegrationTest { + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @ParameterizedTest + @NullAndEmptySource + void querySignedCollidingRestCallIsSafelyRejectedWith404(String authorization) { + RequestSpecification spec = given() + .queryParam("X-Amz-Credential", "test/20260707/us-east-1/appsync/aws4_request") + .contentType("application/json") + .body("{}"); + if (authorization != null) { + spec.header("Authorization", authorization); + } + spec.when() + .post("/v2/apis") + .then() + .statusCode(404) + .contentType(containsString("application/json")) + .header("X-Amzn-Errortype", "UnknownOperationException") + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Unknown operation: POST /v2/apis")); + } + + @ParameterizedTest + @CsvSource({ + "appsync, POST, /v2/apis", + "appsync, GET, /v2/apis", + "appsync, GET, /v2/apis/api-123", + "appsync, DELETE, /v2/apis/api-123", + "app-integrations, POST, /applications", + "app-integrations, GET, /applications", + "app-integrations, GET, /applications/app-123", + "app-integrations, DELETE, /applications/app-123", + "servicecatalog, POST, /applications", + "servicecatalog, GET, /applications", + "servicecatalog, GET, /applications/app-123", + "servicecatalog, DELETE, /applications/app-123", + "mq, POST, /v1/configurations", + "mq, GET, /v1/configurations", + "mq, GET, /v1/configurations/c-123", + "mq, PUT, /v1/configurations/c-123", + "mq, DELETE, /v1/configurations/c-123", + "securityhub, GET, /associations", + "securityhub, POST, /associations", + "macie2, GET, /jobs/job-123", + "connect, POST, /rules/inst-123", + "connect, GET, /rules/inst-123", + "connect, GET, /workspaces/inst-123", + "inspector2, POST, /cluster/get", + "lakeformation, POST, /CommitTransaction", + "bedrock, GET, /resourcepolicy/arn:aws:bedrock:us-east-1:123456789012:probe", + "bedrock, PUT, /resourcepolicy/arn:aws:bedrock:us-east-1:123456789012:probe", + "bedrock, DELETE, /resourcepolicy/arn:aws:bedrock:us-east-1:123456789012:probe" + }) + void collidingRestCallsAreSafelyRejectedWith404(String service, String method, String path) { + given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", service)) + .contentType("application/json") + .body("{}") + .when() + .request(method, path) + .then() + .statusCode(404) + .contentType(containsString("application/json")) + .header("X-Amzn-Errortype", "UnknownOperationException") + .header("x-amzn-query-error", "UnknownOperationException;Sender") + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Unknown operation: " + method + " " + path)); + } + + @Test + void legitimateAppConfigCallIsNotRejectedByCollisionGuard() { + // AppConfig owns /applications, so signed as appconfig it must not be rejected with UnknownOperationException + given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", "appconfig")) + .contentType("application/json") + .body("{\"Name\":\"test-app\"}") + .when() + .post("/applications") + .then() + .statusCode(201); + } + + @Test + void legitimateBedrockResourcePolicyWithHyphenIsNotRejectedByCollisionGuard() { + // Bedrock owns /resource-policy (with hyphen), must not collide with /resourcepolicy + given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", "bedrock")) + .contentType("application/json") + .when() + .get("/resource-policy/arn:aws:bedrock:us-east-1:123456789012:test-resource") + .then() + .statusCode(404) + .header("X-Amzn-Errortype", equalTo("ResourceNotFoundException")); + } + + @Test + void legitimateAppSyncChannelNamespaceCallIsNotRejectedByCollisionGuard() { + // AppSync owns /v2/apis/{apiId}/channelNamespaces, so signed as appsync it reaches AppSync + // (returning AppSync's NotFoundException for an unknown apiId, NOT UnknownOperationException) + given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", "appsync")) + .contentType("application/json") + .body("{\"name\":\"test-ns\"}") + .when() + .post("/v2/apis/api-123/channelNamespaces") + .then() + .statusCode(404) + .header("X-Amzn-Errortype", equalTo("NotFoundException")); + } +}