fix(iam): authorize a Smithy RPC v2 call as the operation its route runs (#5620)

A Smithy RPC v2 call names its operation in the path, /service/<service>/operation/<operation>,
not in a Query Action or an X-Amz-Target header. Enforcement found no action for it, and an
unknown action is allowed, so under enforcement any known key could run any operation of SQS, SNS,
DynamoDB, Kinesis, Step Functions, CloudWatch or Marketplace over RPC v2, explicit denies included.

Enforcement also took the call's shape from the protocol claim, which the claim filter reads before
routing, while the CBOR endpoint dispatches whatever its route matched. The two disagree in
several ways:
- An unrecognised smithy-protocol value leaves the call unclaimed. With strict claiming off, the
  default, it is still served, so an unsigned call was not refused.
- An X-Amz-Target header on the call named the operation it was checked as, while the path named
  the one that ran.
- A path with a prefix before /service/ is claimed as RPC v2 while another route serves it.

The filter now reads the service and operation from the route the call reached, the values the
endpoint dispatches. It authorizes that operation as the action AWS checks for it, under the
serving service's namespace, and records the call's claim, so the unsigned refusal and the CBOR
error format apply whatever its smithy-protocol header says. A call on any other route is handled
as before.

The resources and condition keys a request names in its body were read only from JSON, and a CBOR
body fell back to a wildcard resource: a deny on one queue, table or stream did not apply, a grant
on one did not match, and a ForAllValues dynamodb:LeadingKeys condition granted every key. The
body is now read as CBOR when its content type says so, on the RPC v2 path and on the
x-amz-cbor-1.1 one, which had the same gap.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-14 17:33:39 -05:00
1 parent 46f4977b23
commit 29ead624f8
5 files changed
+429 -8

No files matched your search

@@ -48,6 +48,10 @@ public class AwsJsonCborController {
private static final String GENERIC_CBOR_MEDIA_TYPE = "application/cbor";
private static final String AWS_CBOR_1_1_MEDIA_TYPE = "application/x-amz-cbor-1.1";
/** The RPC v2 path parameters {@link IamEnforcementFilter} authorizes, as this endpoint dispatches them. */
static final String RPC_V2_SERVICE_PARAMETER = "serviceId";
static final String RPC_V2_OPERATION_PARAMETER = "operation";
private final ObjectMapper objectMapper;
private final ResolvedServiceCatalog catalog;
private final RegionResolver regionResolver;
@@ -289,8 +293,8 @@ public class AwsJsonCborController {
@Consumes({GENERIC_CBOR_MEDIA_TYPE, AWS_CBOR_1_1_MEDIA_TYPE})
@Produces({GENERIC_CBOR_MEDIA_TYPE, AWS_CBOR_1_1_MEDIA_TYPE})
public Response handleSmithyRpcV2Cbor(
@PathParam("serviceId") String serviceId,
@PathParam("operation") String operation,
@PathParam(RPC_V2_SERVICE_PARAMETER) String serviceId,
@PathParam(RPC_V2_OPERATION_PARAMETER) String operation,
@Context HttpHeaders httpHeaders,
byte[] body) {
@@ -288,6 +288,13 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if (!config.services().iam().enforcementEnabled()) {
return;
}
ProtocolClaim rpcV2Route = rpcV2RouteClaim(ctx);
if (rpcV2Route != null) {
// The claim filter runs before routing and can leave a call this endpoint serves
// unclaimed, as an unrecognised smithy-protocol value does. Every check below that reads
// the claim has to see the call that runs: unclaimed, an unsigned one would pass as REST.
ctx.setProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY, rpcV2Route);
}
String auth = ctx.getHeaderString("Authorization");
boolean emptyHeader = auth != null && auth.isBlank();
@@ -547,8 +554,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
/**
* Resolves both the IAM namespace and action from the service that will actually handle the
* request. Query traffic shares the dispatch resolver with {@link AwsQueryController}; REST
* traffic uses the JAX-RS resource class that won route matching.
* request. Query traffic shares the dispatch resolver with {@link AwsQueryController}; an RPC v2
* call takes the service and operation its route dispatches; REST traffic uses the JAX-RS
* resource class that won route matching.
*/
private ResolvedAuthorization resolveAuthorization(String auth, String claimedScope,
ContainerRequestContext ctx) {
@@ -567,6 +575,18 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
IamAuthorizedActions.actionFor(servingScope, queryAction));
}
// Read from the route rather than the claim: a path the claim filter takes for RPC v2 can
// reach another route, and an X-Amz-Target header names an operation this endpoint ignores.
ProtocolClaim rpcV2Route = rpcV2RouteClaim(ctx);
if (rpcV2Route != null) {
if (rpcV2Route.service() == null) {
return new ResolvedAuthorization(claimedScope, null); // no service: the endpoint answers 404
}
String servingScope = iamServiceScope(rpcV2Route.service(), claimedScope);
return new ResolvedAuthorization(servingScope,
IamAuthorizedActions.actionFor(servingScope, rpcV2Route.operation()));
}
ResolvedAuthorization routeAuthorization = restRouteAuthorization(ctx);
if (routeAuthorization != null) {
return routeAuthorization;
@@ -580,6 +600,25 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
: actionRegistry.resolve(servingScope, ctx));
}
/**
* The claim of a call JAX-RS routed to the Smithy RPC v2 endpoint, made from the service and
* operation in the route's path parameters, which are what that endpoint dispatches. Null for
* any other request.
*/
private ProtocolClaim rpcV2RouteClaim(ContainerRequestContext ctx) {
if (resourceInfo == null || !AwsJsonCborController.class.equals(resourceInfo.getResourceClass())) {
return null;
}
MultivaluedMap<String, String> parameters = ctx.getUriInfo().getPathParameters();
String serviceId = parameters.getFirst(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER);
String operation = parameters.getFirst(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER);
if (serviceId == null || operation == null) {
return null;
}
return new ProtocolClaim(WireProtocol.RPCV2_CBOR, catalog.byCborSdkServiceId(serviceId).orElse(null),
operation, null);
}
/**
* The action a REST request's matched route maps to, read from the route alone. Null for any
* other request, and for a route no rule maps.
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
@@ -14,6 +15,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MediaType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -44,6 +46,9 @@ public class ResourceArnBuilder {
/** What a service-linked role deletion task identifier starts with; see its resolver. */
private static final String DELETION_TASK_PREFIX = "task" + ServiceLinkedRoles.PATH;
/** Reads the CBOR bodies the RPC v2 and {@code x-amz-cbor-1.1} paths carry. */
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
private final ObjectMapper objectMapper;
/**
* Looked up lazily through a provider: this builder is constructed by the enforcement filter,
@@ -929,7 +934,7 @@ public class ResourceArnBuilder {
return null;
}
try {
JsonNode node = objectMapper.readTree(body);
JsonNode node = (isCbor(ctx.getMediaType()) ? CBOR_MAPPER : objectMapper).readTree(body);
ctx.setProperty("floci.bufferedJsonBody", node);
return node;
} catch (Exception e) {
@@ -937,6 +942,18 @@ public class ResourceArnBuilder {
}
}
/**
* A CBOR body, which names its resources in the same members a JSON body does. Both CBOR
* content types arrive as {@code application/cbor}: {@code AwsCborContentTypeFilter} rewrites
* {@code x-amz-cbor-1.1} before routing.
*/
private static boolean isCbor(MediaType mediaType) {
return mediaType != null
&& "application".equalsIgnoreCase(mediaType.getType())
&& ("cbor".equalsIgnoreCase(mediaType.getSubtype())
|| "x-amz-cbor-1.1".equalsIgnoreCase(mediaType.getSubtype()));
}
private String extractSegmentAfter(String path, String segment) {
String marker = "/" + segment + "/";
int idx = path.indexOf(marker);