mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(core): treat a request no protocol claims as the call its route runs (#6200)
With floci.protocols.strict-claiming off, the default, a request that no wire protocol claims is logged and served by whatever route it matches. The checks after routing read the claim, so they saw a request of no protocol: - IAM enforcement served it unsigned. The MissingAuthenticationToken refusal applies only to a JSON, CBOR or Query claim. An unsigned SQS ListQueues over JSON or Query, or a Kinesis ListStreams over CBOR, was served when it carried a Smithy-Protocol header Floci does not serve that path by: an unknown value, or rpc-v2-cbor outside the rpcv2 path. An X-Amz-Target call with no Content-Type reached the CBOR endpoint the same way. - IAM enforcement checked a signed REST request as the action its X-Amz-Target header named, not its route's. A user allowed only s3:GetObject deleted an object by sending the DELETE with X-Amz-Target: AmazonS3.GetObject and any Smithy-Protocol header. A signed JSON call it refused got the REST status, 403, rather than JSON's 400 AccessDeniedException. - A call to a disabled service was served when only its claim would have named the service, as for an unsigned JSON or rpcv2 call. - A REST JSON service request that fell through to S3's wildcard routes was not rejected, and a REST request signed for a service Floci lacks skipped the guard that refuses it before routing, so S3's routes answered it with a misleading error. The new AwsRouteClaimFilter runs after route matching and before every filter that reads the claim, AccountContextFilter's unknown-Region refusal and the signature check among them. It claims such a request by the route it reached, through ProtocolClaimer.routeClaim: - the JSON 1.0, JSON 1.1 and CBOR endpoints by X-Amz-Target; - the rpcv2 route by its path; - the Query endpoint's POST by the form; - every other route as REST. Those routes serve the request the same way whatever Smithy-Protocol header it carries, so the claim names the call that runs. A request claimed as REST also gets the unknown-service-scope check a claimed one gets before routing, which AwsProtocolClaimFilter now exposes for it. A claimed request is left as it was, and strict claiming still rejects an unclaimed one before routing. An X-Amz-Target post with a content type no protocol uses, the third kind of request the strict-claiming docs list, matches no route at all, and got a 500 rather than its 415, as any root POST in a content type no endpoint consumes did. Several resource methods share POST /, so RESTEasy Reactive routes it to a placeholder with no method of its own that picks one by content type, and answers 415 from there when none consumes it. The ResourceInfo producer then returns the placeholder's missing method, and CDI refuses the null with IllegalProductException. AwsRestJsonErrorHeaderFilter and S3CorsFilter read ResourceInfo on every response, so each failed on it. Both now read it through the new RoutedResource, which gives null when routing chose no method. docs/services/iam.md and the strict-claiming setting's docs say how such a request is handled. IamEnforcementFilter builds its rpcv2 route claim through the same ProtocolClaimer helper.
This commit is contained in:
1 parent
64718c4034
commit
2f57441b84
19 files changed
+644
-42
No files matched your search
@@ -156,7 +156,8 @@ public interface EmulatorConfig {
|
||||
* wire-protocol-selection guide (e.g. an unknown Smithy-Protocol header
|
||||
* value, a recognized-but-unimplemented rpc-v2-json request, or an
|
||||
* X-Amz-Target post with a foreign content type). When disabled such
|
||||
* requests are only logged and pass through to JAX-RS matching.
|
||||
* requests are logged and pass through to JAX-RS matching, and are then
|
||||
* handled as the protocol of the route that serves them.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean strictClaiming();
|
||||
|
||||
@@ -68,6 +68,7 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
ctx.abortWith(unknownOperationResponse(404,
|
||||
"Unable to determine the wire protocol for this request"));
|
||||
}
|
||||
// Otherwise the route it reaches claims it: see AwsRouteClaimFilter.
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -78,22 +79,8 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
.replacePath(AwsQueryController.QUERY_GET_PATH).build());
|
||||
}
|
||||
if (resolved.protocol() == WireProtocol.REST) {
|
||||
Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());
|
||||
if (scope.isPresent() && catalog.byCredentialScope(scope.get()).isEmpty()) {
|
||||
// Gated like the other rejections in this filter. The guard is only as good as
|
||||
// the catalog's scope list: if Floci serves a route whose signing scope is not
|
||||
// enumerated, rejecting is a 404 with no workaround, so leave users a switch.
|
||||
if (!configProvider.get().protocols().rejectUnknownServiceScope()) {
|
||||
LOG.debugv("Unsupported service scope {0} on {1} {2}, rejection disabled by config",
|
||||
scope.get(), signals.method(), signals.path());
|
||||
return;
|
||||
}
|
||||
LOG.infov("Rejecting request signed for unsupported service scope {0}: {1} {2}",
|
||||
scope.get(), signals.method(), signals.path());
|
||||
ctx.abortWith(unknownOperationResponse(404,
|
||||
"Unknown operation: " + signals.method() + " " + signals.path()));
|
||||
return;
|
||||
}
|
||||
refuseUnknownServiceScope(ctx, signals, catalog, configProvider.get());
|
||||
return;
|
||||
}
|
||||
if (resolved.protocol() == WireProtocol.RPCV2_JSON) {
|
||||
LOG.warnv("Received rpc-v2-json request for service {0} operation {1} — protocol not implemented",
|
||||
@@ -111,6 +98,32 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a REST request whose SigV4 credential scope names a service absent from the catalog,
|
||||
* which would otherwise fall through to S3's path-style routes. {@link AwsRouteClaimFilter} also
|
||||
* applies it to a request it claims as REST. Returns whether it refused the request.
|
||||
*/
|
||||
static boolean refuseUnknownServiceScope(ContainerRequestContext ctx, ClaimSignals signals,
|
||||
ResolvedServiceCatalog catalog, EmulatorConfig config) {
|
||||
Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());
|
||||
if (scope.isEmpty() || catalog.byCredentialScope(scope.get()).isPresent()) {
|
||||
return false;
|
||||
}
|
||||
// Gated like the other rejections in this filter. The guard is only as good as the
|
||||
// catalog's scope list: if Floci serves a route whose signing scope is not enumerated,
|
||||
// rejecting is a 404 with no workaround, so leave users a switch.
|
||||
if (!config.protocols().rejectUnknownServiceScope()) {
|
||||
LOG.debugv("Unsupported service scope {0} on {1} {2}, rejection disabled by config",
|
||||
scope.get(), signals.method(), signals.path());
|
||||
return false;
|
||||
}
|
||||
LOG.infov("Rejecting request signed for unsupported service scope {0}: {1} {2}",
|
||||
scope.get(), signals.method(), signals.path());
|
||||
ctx.abortWith(unknownOperationResponse(404,
|
||||
"Unknown operation: " + signals.method() + " " + signals.path()));
|
||||
return true;
|
||||
}
|
||||
|
||||
static Response unknownOperationResponse(int status, String message) {
|
||||
return errorResponse(status, "UnknownOperationException", message);
|
||||
}
|
||||
|
||||
+3
-2
@@ -51,8 +51,9 @@ public class AwsRestJsonErrorHeaderFilter implements ContainerResponseFilter {
|
||||
if (claimValue instanceof ProtocolClaim claim && claim.service() != null) {
|
||||
return Optional.of(claim.service());
|
||||
}
|
||||
if (resourceInfo != null && resourceInfo.getResourceClass() != null) {
|
||||
Optional<ServiceDescriptor> descriptor = catalog.byResourceClass(resourceInfo.getResourceClass());
|
||||
Class<?> resourceClass = RoutedResource.resourceClass(resourceInfo);
|
||||
if (resourceClass != null) {
|
||||
Optional<ServiceDescriptor> descriptor = catalog.byResourceClass(resourceClass);
|
||||
if (descriptor.isPresent()) {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Priorities;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Claims, by its route, a request {@link AwsProtocolClaimFilter} served without a claim. With
|
||||
* {@code floci.protocols.strict-claiming} off, a request no protocol claims, such as one carrying an
|
||||
* unrecognised {@code Smithy-Protocol} value or an {@code X-Amz-Target} with a content type no
|
||||
* protocol uses, is logged and passes on to route matching, which serves it. Every check that reads
|
||||
* the claim would otherwise see a request of no protocol: the unsigned refusal let it through, and
|
||||
* IAM enforcement checked a REST request as the action its {@code X-Amz-Target} named rather than
|
||||
* its route's. A request claimed as REST also gets the check a claimed one gets before routing: one
|
||||
* signed for a service Floci does not serve is refused rather than answered by S3's routes.
|
||||
* <p>
|
||||
* Runs after route matching, so {@link ResourceInfo} names the route, and before every filter that
|
||||
* reads the claim: {@link AccountContextFilter}, which answers a request signed for an unknown Region
|
||||
* in the claim's error format, runs at {@code AUTHENTICATION - 100}, and the signature check at
|
||||
* {@link Priorities#AUTHENTICATION}.
|
||||
*/
|
||||
@Provider
|
||||
@Priority(Priorities.AUTHENTICATION - 200)
|
||||
public class AwsRouteClaimFilter implements ContainerRequestFilter {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AwsRouteClaimFilter.class);
|
||||
|
||||
@Context
|
||||
ResourceInfo resourceInfo;
|
||||
|
||||
private final ProtocolClaimer claimer;
|
||||
private final ResolvedServiceCatalog catalog;
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist.
|
||||
private final jakarta.inject.Provider<EmulatorConfig> configProvider;
|
||||
|
||||
@Inject
|
||||
public AwsRouteClaimFilter(ProtocolClaimer claimer, ResolvedServiceCatalog catalog,
|
||||
jakarta.inject.Provider<EmulatorConfig> configProvider) {
|
||||
this.claimer = claimer;
|
||||
this.catalog = catalog;
|
||||
this.configProvider = configProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
Class<?> resourceClass = RoutedResource.resourceClass(resourceInfo);
|
||||
if (ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim || resourceClass == null) {
|
||||
return;
|
||||
}
|
||||
ClaimSignals signals = ClaimSignals.from(ctx);
|
||||
ProtocolClaim claim = claimer.routeClaim(resourceClass, signals, ctx.getUriInfo().getPathParameters());
|
||||
LOG.debugv("Claimed {0} {1} by its route: {2} service={3} operation={4}", ctx.getMethod(),
|
||||
ctx.getUriInfo().getPath(), claim.protocol(),
|
||||
claim.service() != null ? claim.service().externalKey() : null, claim.operation());
|
||||
ctx.setProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY, claim);
|
||||
if (claim.protocol() == WireProtocol.REST) {
|
||||
AwsProtocolClaimFilter.refuseUnknownServiceScope(ctx, signals, catalog, configProvider.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -770,14 +770,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
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);
|
||||
return ProtocolClaimer.rpcV2RouteClaim(catalog, ctx.getUriInfo().getPathParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
@@ -28,7 +29,8 @@ import java.util.regex.Pattern;
|
||||
* <p>
|
||||
* An empty result means the request carries an RPC signal that no supported
|
||||
* protocol claims — per the guide such input must be rejected (enforced only
|
||||
* when {@code floci.protocols.strict-claiming} is enabled).
|
||||
* when {@code floci.protocols.strict-claiming} is enabled). With it disabled the
|
||||
* request is served, and {@link #routeClaim} claims it by the route it reaches.
|
||||
* <p>
|
||||
* Intentional deviation from pure precedence iteration: a present
|
||||
* Smithy-Protocol header is treated as an exclusive rpcv2 signal. If its value
|
||||
@@ -123,29 +125,78 @@ public class ProtocolClaimer {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (CONTENT_TYPE_FORM_URLENCODED.equals(mediaType)) {
|
||||
ServiceDescriptor service = SigV4CredentialScope.serviceName(signals.authorization())
|
||||
.flatMap(catalog::byCredentialScope)
|
||||
.orElse(null);
|
||||
return Optional.of(new ProtocolClaim(WireProtocol.AWS_QUERY, service, null, null));
|
||||
return Optional.of(queryClaim(signals));
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.of(ProtocolClaim.rest());
|
||||
}
|
||||
|
||||
private ProtocolClaim rpcV2Claim(WireProtocol protocol, Matcher rpcV2Path) {
|
||||
String serviceName = rpcV2Path.group(1);
|
||||
String operation = rpcV2Path.group(2);
|
||||
/**
|
||||
* The claim of a request {@link #claim} could not claim, made from the route JAX-RS matched it
|
||||
* to. With strict claiming off such a request is still served, by a route that dispatches it the
|
||||
* same way whatever its {@code Smithy-Protocol} header or content type says: the JSON, CBOR and
|
||||
* Query endpoints on {@code X-Amz-Target}, the rpcv2 path and the form, and every other route as
|
||||
* its REST operation. Claimed this way, every check that reads the claim sees the call that runs.
|
||||
*/
|
||||
public ProtocolClaim routeClaim(Class<?> resourceClass, ClaimSignals signals,
|
||||
MultivaluedMap<String, String> pathParameters) {
|
||||
if (resourceClass == AwsJsonController.class) {
|
||||
return targetClaim(WireProtocol.AWS_JSON_1_0, signals.xAmzTarget());
|
||||
}
|
||||
if (resourceClass == AwsJson11Controller.class) {
|
||||
return targetClaim(WireProtocol.AWS_JSON_1_1, signals.xAmzTarget());
|
||||
}
|
||||
if (resourceClass == AwsJsonCborController.class) {
|
||||
ProtocolClaim rpcV2 = rpcV2RouteClaim(catalog, pathParameters);
|
||||
return rpcV2 != null ? rpcV2 : targetClaim(WireProtocol.AWS_CBOR_TARGET, signals.xAmzTarget());
|
||||
}
|
||||
if (resourceClass == AwsQueryController.class && "POST".equalsIgnoreCase(signals.method())) {
|
||||
return queryClaim(signals);
|
||||
}
|
||||
// The Query endpoint's GET route serves only the Query GET that claim() rewrites onto it;
|
||||
// reached directly it is no Query call, as claim() would say without the header.
|
||||
return ProtocolClaim.rest();
|
||||
}
|
||||
|
||||
/**
|
||||
* The claim of a call 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 when the
|
||||
* parameters do not name both, as on the endpoint's {@code X-Amz-Target} route.
|
||||
*/
|
||||
static ProtocolClaim rpcV2RouteClaim(ResolvedServiceCatalog catalog,
|
||||
MultivaluedMap<String, String> pathParameters) {
|
||||
String serviceId = pathParameters.getFirst(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER);
|
||||
String operation = pathParameters.getFirst(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER);
|
||||
if (serviceId == null || operation == null) {
|
||||
return null;
|
||||
}
|
||||
return rpcV2Claim(catalog, WireProtocol.RPCV2_CBOR, serviceId, operation);
|
||||
}
|
||||
|
||||
private static ProtocolClaim rpcV2Claim(ResolvedServiceCatalog catalog, WireProtocol protocol,
|
||||
String serviceName, String operation) {
|
||||
ServiceDescriptor service = catalog.byCborSdkServiceId(serviceName).orElse(null);
|
||||
return new ProtocolClaim(protocol, service, operation, null);
|
||||
}
|
||||
|
||||
private ProtocolClaim rpcV2Claim(WireProtocol protocol, Matcher rpcV2Path) {
|
||||
return rpcV2Claim(catalog, protocol, rpcV2Path.group(1), rpcV2Path.group(2));
|
||||
}
|
||||
|
||||
private ProtocolClaim targetClaim(WireProtocol protocol, String target) {
|
||||
return catalog.matchTarget(target)
|
||||
.map(match -> new ProtocolClaim(protocol, match.descriptor(), match.action(), target))
|
||||
.orElseGet(() -> new ProtocolClaim(protocol, null, null, target));
|
||||
}
|
||||
|
||||
private ProtocolClaim queryClaim(ClaimSignals signals) {
|
||||
ServiceDescriptor service = SigV4CredentialScope.serviceName(signals.authorization())
|
||||
.flatMap(catalog::byCredentialScope)
|
||||
.orElse(null);
|
||||
return new ProtocolClaim(WireProtocol.AWS_QUERY, service, null, null);
|
||||
}
|
||||
|
||||
static boolean isRpcV2Path(String path) {
|
||||
return path != null && RPC_V2_PATH.matcher(path).matches();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import jakarta.enterprise.inject.IllegalProductException;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
|
||||
/**
|
||||
* Reads the resource a request was routed to from a response filter, which also runs for requests
|
||||
* routing turned away. For most of those {@link ResourceInfo} names no class, but not for all: when
|
||||
* several resource methods share a path and HTTP method and differ only in what they consume,
|
||||
* RESTEasy Reactive routes to a placeholder with no method of its own, which picks one by content
|
||||
* type. When none consumes the request's content type it answers 415 from that placeholder, the
|
||||
* {@code ResourceInfo} producer returns its missing method, and CDI refuses the null with
|
||||
* {@link IllegalProductException} on the first call, turning the 415 into a 500.
|
||||
*/
|
||||
public final class RoutedResource {
|
||||
|
||||
private RoutedResource() {
|
||||
}
|
||||
|
||||
/** The class of the resource method the request was routed to, or null when routing chose none. */
|
||||
public static Class<?> resourceClass(ResourceInfo resourceInfo) {
|
||||
if (resourceInfo == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return resourceInfo.getResourceClass();
|
||||
} catch (IllegalProductException noMethodChosen) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RoutedResource;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
|
||||
final class S3SignatureFilterScope {
|
||||
@@ -12,7 +13,8 @@ final class S3SignatureFilterScope {
|
||||
|| (presignGenerator != null && presignGenerator.shouldValidateSignatures());
|
||||
}
|
||||
|
||||
/** Safe from a response filter too, which also sees requests routing turned away. */
|
||||
static boolean routedToS3(ResourceInfo resourceInfo) {
|
||||
return resourceInfo != null && S3Controller.class.equals(resourceInfo.getResourceClass());
|
||||
return S3Controller.class.equals(RoutedResource.resourceClass(resourceInfo));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user