fix(emr-serverless): check EMR Serverless's requests under IAM enforcement (#8143)

Under IAM enforcement no EMR Serverless request was checked: its model
was not among the vendored REST routes and its namespace's request
resources were not read, so no request was mapped to an action and the
filter let it through. A caller with no permissions could create, read,
update and delete applications and start and cancel job runs. A request
sent straight to /_emrserverless, the prefix its route filter moves
requests onto, reached the controller too, whatever it was signed for.

EmrServerlessRouteFilter now goes through SignedServiceRoute, as the
other filters that move a service's routes do: a request signed for
EMR Serverless is moved onto /_emrserverless, one sent to the prefix
signed for another service is refused, and the prefix it records lets
IAM match the vendored routes at the model's paths. EMR Serverless's
routes are vendored and its requests read, so an application call is
checked on the application its path names. Its namespace also leaves
the exclusions of regen_iam_tag_on_create.py and regen_iam_passed_roles.py:
tags a create sends are checked as emr-serverless:TagResource, and
StartJobRun and StartSession check iam:PassRole on the execution role.

Co-authored-by: mrfelek <17274927+mrfelek@users.noreply.github.com>
This commit is contained in:
mrfelekandmrfelek authored and GitHub committed 2026-11-12 16:46:38 -04:00
1 parent f3c6d8e809
commit 0b6a6003b0
13 files changed
+242 -52

No files matched your search

@@ -1,51 +1,38 @@
package io.floci.aws.services.emrserverless;
import io.floci.aws.core.common.RequestHost;
import io.floci.aws.core.common.ResolvedServiceCatalog;
import io.floci.aws.core.common.SignedServiceRoute;
import jakarta.annotation.Priority;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.Provider;
import java.net.URI;
/**
* Pre-matching filter that disambiguates REST JSON routes for EMR Serverless.
* EMR Serverless and AppConfig both use `/applications` in their REST API, which
* causes a JAX-RS routing collision. This filter rewrites EMR Serverless requests
* to an internal path.
* Rewrites EMR Serverless requests to an internal prefix before JAX-RS route matching. EMR
* Serverless and AppConfig both serve {@code /applications}, so registered directly the two would
* collide. Only a request signed for EMR Serverless is rewritten; anything else keeps its path and
* reaches AppConfig.
*/
@Provider
@PreMatching
@Priority(5000)
public class EmrServerlessRouteFilter implements ContainerRequestFilter {
static final String INTERNAL_PATH = "/_emrserverless/applications";
static final String INTERNAL_PREFIX = "/_emrserverless";
static final String APPLICATIONS_PATH = "/applications";
private final SignedServiceRoute route;
@Inject
public EmrServerlessRouteFilter(ResolvedServiceCatalog catalog) {
this.route = new SignedServiceRoute(catalog, "emr-serverless", INTERNAL_PREFIX,
SignedServiceRoute.under(APPLICATIONS_PATH));
}
@Override
public void filter(ContainerRequestContext ctx) {
UriInfo uriInfo = ctx.getUriInfo();
String path = uriInfo.getPath();
if (path.startsWith("/")) {
path = path.substring(1);
}
if (!path.startsWith("applications")) {
return;
}
String host = RequestHost.of(ctx);
boolean isEmrServerlessHost = host != null && host.startsWith("emr-serverless.");
String auth = ctx.getHeaderString("Authorization");
boolean isEmrServerlessAuth = auth != null && auth.contains("/emr-serverless/aws4_request");
if (isEmrServerlessHost || isEmrServerlessAuth) {
URI rewritten = uriInfo.getRequestUriBuilder()
.replacePath(INTERNAL_PATH + (path.length() > 12 ? path.substring(12) : ""))
.build();
ctx.setRequestUri(rewritten);
}
route.apply(ctx);
}
}
@@ -63,7 +63,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
"execute-api", // ApiGatewayExecuteApiHostFilter: <api-id>.execute-api[.<region>].<host>
"lambda-url", // LambdaUrlRoutingFilter: <url-id>.lambda-url.<region>.<host>
"lambda-web", // LambdaWebDataPlane: <id>.<shard>.lambda-web.<region>.<host>, <id>.lambda-web.global.<host>
"emr-serverless", // EmrServerlessRouteFilter: emr-serverless.<host>
"emr-serverless", // emr-serverless.<host>, an EMR Serverless endpoint, never a bucket
"cloudfront"); // CloudFrontDistributionFilter: <dist-id>.cloudfront.net
/**