fix(iam): refuse an unsigned management call under enforcement (#4489)

A request carrying no Authorization header returned before any policy check, so with enforcement on an unauthenticated caller reached every emulated API by simply omitting the header.

Refuse one whose wire shape names a management operation: a JSON, CBOR or Query claim exists only for a request carrying X-Amz-Target, an rpcv2 path or an Action parameter. AWS answers a credential-less request with MissingAuthenticationToken at 403, returned here in the encoding the request used.

Operations AWS itself serves without credentials stay allowed, taken from the authtype: none trait in its service models: the Cognito user-pool and identity flows and the two web-identity and SAML AssumeRole calls. The list is keyed by service because GetUser needs no credentials on cognito-idp and requires them on iam.

REST is left alone. This filter also sees the API Gateway execute path, Lambda function URLs, CloudFront serving and the Cognito OIDC endpoints, which are unsigned by design, and a REST claim does not say which service will serve the request.

The CBOR error shape moves into CborErrorResponses, shared with the rpcv2 controller so the two cannot drift into answering the same failure differently.

EksIrsaDockerIntegrationTest set up its IAM fixtures over unsigned Query calls and so began failing: CreateRole is not an operation AWS serves without credentials. Its fixture calls now carry the root stand-in credential, which is what every other test does under enforcement. The AssumeRoleWithWebIdentity calls it actually tests stay unsigned, as AWS serves those without credentials.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-27 22:59:34 -05:00
1 parent c55ff5362b
commit c26f782ea8
7 files changed
+338 -14

No files matched your search

@@ -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";
@@ -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) {
@@ -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();
}
}
}
@@ -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}.
*
* <p>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.
*
* <p>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<String, Set<String>> NO_AUTH_OPERATIONS = Map.of(
"cognito-idp", Set.of(
"AssociateSoftwareToken",
"ChangePassword",
"CompleteWebAuthnRegistration",
"ConfirmDevice",
"ConfirmForgotPassword",
"ConfirmSignUp",
"DeleteUser",
"DeleteUserAttributes",
"DeleteWebAuthnCredential",
"ForgetDevice",
"ForgotPassword",
"GetDevice",
"GetTokensFromRefreshToken",
"GetUser",
"GetUserAttributeVerificationCode",
"GetUserAuthFactors",
"GlobalSignOut",
"InitiateAuth",
"ListDevices",
"ListWebAuthnCredentials",
"ResendConfirmationCode",
"RespondToAuthChallenge",
"RevokeToken",
"SetUserMFAPreference",
"SetUserSettings",
"SignUp",
"StartWebAuthnRegistration",
"UpdateAuthEventFeedback",
"UpdateDeviceStatus",
"UpdateUserAttributes",
"VerifySoftwareToken",
"VerifyUserAttribute"),
"cognito-identity", Set.of(
"GetCredentialsForIdentity",
"GetId",
"GetOpenIdToken",
"UnlinkIdentity"));
/**
* The same for the Query protocol, where the claim carries no service: an unsigned Query
* request has no credential scope to derive one from. Both names are unique across the Query
* services Floci serves, so the operation alone identifies them.
*/
private static final Set<String> NO_AUTH_QUERY_OPERATIONS = Set.of(
"AssumeRoleWithSAML",
"AssumeRoleWithWebIdentity");
/**
* Implicit identity policy for the account-root principal: full access, bounded only by SCPs.
* The account root is not a registered IAM identity, so it has no stored identity policy.
@@ -182,6 +253,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
auth = presignedCredentialAsAuthorization(ctx);
}
if (auth == null) {
refuseUnsignedManagementCall(ctx);
return;
}
@@ -906,6 +978,80 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
return Response.status(403).type(MediaType.APPLICATION_JSON).entity(body).build();
}
/**
* Refuses an unsigned request whose wire shape makes it a management API call. A JSON, CBOR or
* Query claim exists only for a request carrying {@code X-Amz-Target}, an rpcv2 path or an
* {@code Action} parameter, and no public data plane speaks any of those, so an unsigned one is
* an unauthenticated call on a control-plane API.
*
* <p>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 <scope>:<Action>} and only the suffix is wanted.
*/
private String queryOperation(ContainerRequestContext ctx) {
String resolved = actionRegistry.resolve("", ctx);
return resolved == null ? null : resolved.substring(resolved.indexOf(':') + 1);
}
/**
* AWS answers a request carrying no credentials with {@code MissingAuthenticationToken} at 403,
* "The request must contain a valid AWS access key ID or X.509 certificate." The JSON form of
* the name carries the {@code Exception} suffix, as {@code ApiGatewayExecuteController} already
* returns for the same failure on its own path.
*/
static Response missingAuthenticationTokenResponse(WireProtocol protocol, ContainerRequestContext ctx) {
if (protocol == WireProtocol.RPCV2_CBOR || protocol == WireProtocol.AWS_CBOR_TARGET) {
// A CBOR client cannot read a JSON body, so the rejection has to arrive in the encoding
// the request used, the same shape the rpcv2 controller returns for its own errors.
String requestContentType = ctx.getHeaderString(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER);
if (requestContentType == null) {
requestContentType = ctx.getHeaderString("Content-Type");
}
return CborErrorResponses.of(
new AwsException("MissingAuthenticationTokenException", MISSING_AUTHENTICATION_TOKEN, 403),
CborErrorResponses.mediaTypeFor(requestContentType));
}
if (isFormEncoded(ctx.getMediaType())) {
return queryXmlError("MissingAuthenticationToken", MISSING_AUTHENTICATION_TOKEN);
}
String body = "{\"__type\":\"MissingAuthenticationTokenException\",\"message\":\""
+ MISSING_AUTHENTICATION_TOKEN + "\"}";
return Response.status(403).type(MediaType.APPLICATION_JSON).entity(body).build();
}
private static Response jsonAccessDenied(String message) {
String body = "{\"__type\":\"AccessDeniedException\",\"message\":\"" + message + "\"}";
return Response.status(403).type(MediaType.APPLICATION_JSON).entity(body).build();