diff --git a/docs/services/iam.md b/docs/services/iam.md index 0ced23f51c..d2ba1633db 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -765,6 +765,28 @@ request itself still passes through the filter). Both are deliberate non-goals would require IAM to resolve the caller's organization directly, which is the dependency the lazily-resolved `ScpProvider` exists to avoid. +## Unsigned requests + +With enforcement on, a request carrying no `Authorization` header is refused with +`403 MissingAuthenticationToken` when its wire shape names a management operation: a JSON, CBOR or +Query request, identified by `X-Amz-Target`, an rpcv2 path or an `Action` parameter. + +Two carve-outs, both deliberate. + +**Operations AWS serves without credentials are still allowed.** Signing up or signing in to a +Cognito user pool, and `AssumeRoleWithWebIdentity`, happen before the caller has any AWS +credentials, so AWS marks them as needing none and Floci does the same. The list is taken from the +`authtype: none` trait in AWS's own service models and covers the Cognito user-pool and identity +flows and the two web-identity and SAML `AssumeRole` calls. A Cognito `Admin*` operation is not in that set and does require a signature. + +The refusal arrives in the encoding the request used: XML for Query, CBOR for a CBOR request, JSON +otherwise. + +**REST requests are not checked.** The same filter sees the API Gateway execute path, Lambda +function URLs, CloudFront serving, the Cognito OIDC endpoints and Floci's own health endpoint, all +of which are unsigned by design, and a REST request does not say which service will serve it. An +unsigned REST call therefore still reaches the service, including an unsigned S3 call. + ## Bypass rules Enforcement is deliberately permissive in a few cases, so that enabling it does not break workloads @@ -773,6 +795,8 @@ the emulator cannot reason about: | Case | Behaviour | | --- | --- | | Unresolvable action | Allowed. An action the registry cannot resolve is not evaluated. | +| No `Authorization` header, RPC protocol | **Rejected** with `403 MissingAuthenticationToken`, unless the operation is one AWS itself serves without credentials. | +| No `Authorization` header, REST protocol | Allowed. This filter also sees the API Gateway execute path, Lambda function URLs, CloudFront serving and the Cognito OIDC endpoints, which are unsigned by design. | | `sts:GetCallerIdentity` | Always allowed — AWS returns caller identity even when a policy denies it. | | Access key that exists nowhere | **Rejected** with `403`, in each protocol's own vocabulary: `InvalidAccessKeyId` for S3, `InvalidClientTokenId` for Query services, `UnrecognizedClientException` for JSON services. Allowing it would let any string authorize the request. | | Known credential with no mappable caller context | Allowed. A stored session carrying no role ARN is a real credential, so it is not treated as unauthenticated. | diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsCborContentTypeFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsCborContentTypeFilter.java index e4478d7bad..87c206c22d 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsCborContentTypeFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsCborContentTypeFilter.java @@ -12,7 +12,7 @@ import jakarta.ws.rs.ext.Provider; @Priority(Priorities.HEADER_DECORATOR) public class AwsCborContentTypeFilter implements ContainerRequestFilter { - static final String ORIGINAL_CONTENT_TYPE_HEADER = "X-Floci-Original-Content-Type"; + public static final String ORIGINAL_CONTENT_TYPE_HEADER = "X-Floci-Original-Content-Type"; private static final String AWS_CBOR_1_1_MEDIA_TYPE = "application/x-amz-cbor-1.1"; private static final String GENERIC_CBOR_MEDIA_TYPE = "application/cbor"; diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java index b7d97a00a7..8b6073519d 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java @@ -497,19 +497,7 @@ public class AwsJsonCborController { } private Response cborErrorResponse(AwsException e, String protocolHeader, String mediaType) { - try { - byte[] errBytes = CBOR_MAPPER.writeValueAsBytes( - new AwsErrorResponse(e.jsonType(), e.getMessage())); - String queryErrorFault = (e.getHttpStatus() < 500) ? "Sender" : "Receiver"; - return Response.status(e.getHttpStatus()) - .header(protocolHeader, "rpc-v2-cbor") - .header("x-amzn-query-error", e.getErrorCode() + ";" + queryErrorFault) - .type(mediaType) - .entity(errBytes) - .build(); - } catch (Exception ex) { - return Response.status(e.getHttpStatus()).build(); - } + return CborErrorResponses.of(e, mediaType); } private String responseContentType(HttpHeaders httpHeaders) { diff --git a/src/main/java/io/github/hectorvent/floci/core/common/CborErrorResponses.java b/src/main/java/io/github/hectorvent/floci/core/common/CborErrorResponses.java new file mode 100644 index 0000000000..1328e1ce9d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/CborErrorResponses.java @@ -0,0 +1,44 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.cbor.CBORFactory; +import jakarta.ws.rs.core.Response; + +/** + * The one shape a CBOR error takes: a CBOR-encoded {@link AwsErrorResponse}, the + * {@code smithy-protocol} header, and the query-error header AWS sends alongside it. Shared so the + * rpcv2 controller and {@link IamEnforcementFilter} cannot drift into answering the same failure + * two different ways, which is how a CBOR client ends up unable to decode a rejection. + */ +public final class CborErrorResponses { + + public static final String AWS_CBOR_1_1_MEDIA_TYPE = "application/x-amz-cbor-1.1"; + public static final String GENERIC_CBOR_MEDIA_TYPE = "application/cbor"; + + private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory()); + + private CborErrorResponses() { + } + + /** The CBOR media type matching what the request sent, defaulting to the generic one. */ + public static String mediaTypeFor(String requestContentType) { + return requestContentType != null && requestContentType.contains("x-amz-cbor") + ? AWS_CBOR_1_1_MEDIA_TYPE + : GENERIC_CBOR_MEDIA_TYPE; + } + + public static Response of(AwsException e, String mediaType) { + try { + byte[] body = CBOR_MAPPER.writeValueAsBytes(new AwsErrorResponse(e.jsonType(), e.getMessage())); + String fault = e.getHttpStatus() < 500 ? "Sender" : "Receiver"; + return Response.status(e.getHttpStatus()) + .header("smithy-protocol", "rpc-v2-cbor") + .header("x-amzn-query-error", e.getErrorCode() + ";" + fault) + .type(mediaType) + .entity(body) + .build(); + } catch (Exception ex) { + return Response.status(e.getHttpStatus()).build(); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index 4c65547222..ce36a14709 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -71,6 +72,76 @@ public class IamEnforcementFilter implements ContainerRequestFilter { /** AWS's wording for a credential it does not recognise. */ private static final String INVALID_SECURITY_TOKEN = "The security token included in the request is invalid."; + /** AWS's wording for a request that carries no credentials at all. */ + private static final String MISSING_AUTHENTICATION_TOKEN = + "The request must contain a valid AWS access key ID or X.509 certificate."; + + /** + * Operations AWS itself serves without credentials, so an unsigned call on one is a normal + * client rather than an unauthenticated caller. Taken from the {@code authtype: none} trait in + * botocore's service models (Smithy's {@code noAuth}), restricted to the operations Floci + * serves. Keyed by service because the name alone is not enough: {@code GetUser} needs no + * credentials on {@code cognito-idp} and requires them on {@code iam}. + * + *
Add to this when Floci starts serving another operation AWS marks {@code noAuth}. + * Omitting one makes enforcement refuse a call AWS accepts; adding one that AWS does sign + * leaves that operation unauthenticated. + * + *
Only an operation reachable over a checked protocol needs an entry, since
+ * {@link WireProtocol#REST} is not checked at all. {@code signin}, {@code sso} and
+ * {@code sso-oidc} also carry {@code noAuth} operations and are absent for that reason rather
+ * than by oversight: the SSO portal calls are served on REST routes, and the {@code sso} JSON
+ * target reaches SSO Admin, whose operations AWS does sign.
+ */
+ private static final Map REST is deliberately left alone. This single filter also sees the API Gateway execute
+ * path, Lambda function URLs, CloudFront serving, the Cognito OIDC endpoints and Floci's own
+ * health endpoint, all unsigned by design, and a {@code rest()} claim cannot tell those from an
+ * unsigned S3 call. Separating them needs the route to name its service, which the catalog does
+ * not yet carry for the REST services; that half is tracked with the other route-derived gap.
+ */
+ private void refuseUnsignedManagementCall(ContainerRequestContext ctx) {
+ if (!(ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim)
+ || claim.protocol() == WireProtocol.REST
+ || servesWithoutCredentials(claim, ctx)) {
+ return;
+ }
+ LOG.debugv("Refusing unsigned {0} request under IAM enforcement", claim.protocol());
+ ctx.abortWith(missingAuthenticationTokenResponse(claim.protocol(), ctx));
+ }
+
+ /** Whether the operation this request names is one AWS serves without credentials. */
+ private boolean servesWithoutCredentials(ProtocolClaim claim, ContainerRequestContext ctx) {
+ if (claim.protocol() == WireProtocol.AWS_QUERY) {
+ return NO_AUTH_QUERY_OPERATIONS.contains(queryOperation(ctx));
+ }
+ if (claim.service() == null || claim.operation() == null) {
+ // Nothing names the operation, so there is nothing to match against the list. Refusing
+ // is the safe side of that: an unsigned request whose shape cannot even be read is not
+ // one of the public flows.
+ return false;
+ }
+ return NO_AUTH_OPERATIONS
+ .getOrDefault(claim.service().externalKey(), Set.of())
+ .contains(claim.operation());
+ }
+
+ /**
+ * The {@code Action} a Query request names, via the registry so the form body is read and put
+ * back the one way this codebase already does it. The scope passed in is a placeholder: for a
+ * Query request the registry returns {@code