fix(core): preserve error responses for blank content types (#9703)

Use the same request media-type reader in role inspection and error responses so an empty Content-Type does not hide the original error.

Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-11-28 21:34:43 -04:00
1 parent 18efc30c73
commit d8b5dc53c5
8 files changed
+54 -13

No files matched your search

@@ -270,7 +270,7 @@ public class AccountContextFilter implements ContainerRequestFilter {
CborErrorResponses.mediaTypeFor(contentType)));
return;
}
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ctx.getMediaType())) {
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ResteasyRequests.mediaType(ctx))) {
ctx.abortWith(AwsQueryResponse.error("InvalidSignatureException", message, null, 400));
return;
}
@@ -324,7 +324,7 @@ public class AccountContextFilter implements ContainerRequestFilter {
String message = "Service " + signingName + " has no endpoint in partition " + partitionId
+ ": on AWS the host does not resolve there (the SDKs report an UnknownHostException). "
+ "Floci is running with floci.partitions.strict=true.";
if (isFormEncoded(ctx.getMediaType())) {
if (isFormEncoded(ResteasyRequests.mediaType(ctx))) {
ctx.abortWith(AwsQueryResponse.error("UnknownOperationException", message, null, 404));
return;
}
@@ -1933,7 +1933,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
boolean restXml = catalog.byCredentialScope(credentialScope)
.filter(descriptor -> descriptor.supportsProtocol(ServiceProtocol.REST_XML)).isPresent();
return accessDeniedResponse(action, credentialScope, ctx.getMediaType(), resourceArn, protocol, restXml);
return accessDeniedResponse(action, credentialScope, ResteasyRequests.mediaType(ctx), resourceArn, protocol, restXml);
}
private static String accessDeniedMessage(String action) {
@@ -1144,8 +1144,7 @@ final class PassedRoles {
if (encoding != null && AwsJsonCborController.isGZipped(List.of(encoding))) {
body = AwsJsonCborController.decodeBody(body);
}
String contentType = ctx.getHeaderString(HttpHeaders.CONTENT_TYPE);
MediaType mediaType = contentType != null && contentType.isBlank() ? null : ctx.getMediaType();
MediaType mediaType = ResteasyRequests.mediaType(ctx);
JsonNode node = (isCbor(mediaType) ? CBOR_MAPPER : MAPPER).readTree(body);
ctx.setProperty(IamConditionContextResolver.BUFFERED_JSON_BODY, node);
return node;
@@ -25,7 +25,7 @@ public final class ProtocolErrorResponses {
if (isCbor(protocol)) {
return cbor(ctx, code, message, status);
}
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ctx.getMediaType())) {
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ResteasyRequests.mediaType(ctx))) {
return queryXml(queryCode, message, status);
}
return json(code, message, status);
@@ -1,6 +1,8 @@
package io.floci.aws.core.common;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriInfo;
@@ -23,6 +25,12 @@ public final class ResteasyRequests {
private ResteasyRequests() {
}
/** A blank Content-Type has no media type. RESTEasy would try to parse the empty value. */
public static MediaType mediaType(ContainerRequestContext ctx) {
String contentType = ctx.getHeaderString(HttpHeaders.CONTENT_TYPE);
return contentType != null && contentType.isBlank() ? null : ctx.getMediaType();
}
/** The RESTEasy Reactive context under a JAX-RS one, or null for a synthetic context. */
public static ResteasyReactiveRequestContext of(ContainerRequestContext ctx) {
return ctx instanceof ResteasyReactiveContainerRequestContext container
@@ -13,6 +13,7 @@ import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
@@ -241,12 +242,18 @@ class AccountContextFilterTest {
}
/** A label no partition publishes or admits is refused; the context still names it for the mappers. */
@Test
void anUnknownScopeRegionIsRefusedWithTheJsonSignatureError() {
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " \t")
void anUnknownScopeRegionIsRefusedWithTheJsonSignatureError(String contentType) {
ContainerRequestContext ctx = mockContext(
"AWS4-HMAC-SHA256 Credential=AKID/20260617/polygondwanaland-west-1/sqs/aws4_request, "
+ "SignedHeaders=host, Signature=abc",
null);
when(ctx.getHeaderString("Content-Type")).thenReturn(contentType);
if (contentType != null) {
when(ctx.getMediaType()).thenAnswer(invocation -> MediaType.valueOf(contentType));
}
filter.filter(ctx);
ArgumentCaptor<Response> aborted = ArgumentCaptor.forClass(Response.class);
verify(ctx).abortWith(aborted.capture());
@@ -336,7 +336,8 @@ class IamEnforcementFilterTest {
@CsvSource({
"AWS_JSON_1_0, application/json, 400",
"AWS_JSON_1_1, application/json, 400",
"REST, application/x-amz-json-1.1, 403"
"REST, application/x-amz-json-1.1, 403",
"REST, '', 403"
})
void denialStatusUsesTheClaimedProtocolRatherThanContentType(WireProtocol protocol, String contentType,
int expectedStatus) {
@@ -346,7 +347,8 @@ class IamEnforcementFilterTest {
requestContext.setAccountId("000000000000");
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIAUSER");
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
when(containerRequest.getMediaType()).thenReturn(MediaType.valueOf(contentType));
when(containerRequest.getMediaType()).thenAnswer(invocation -> MediaType.valueOf(contentType));
when(containerRequest.getHeaderString("Content-Type")).thenReturn(contentType);
stubClaim(containerRequest, protocol, dynamoDbDescriptor());
when(iamService.resolveCallerContext(eq("AKIAUSER"), any())).thenReturn(CallerContext.of(List.of()));
// A REST-claimed request resolves from its route, the others from the registry's full rules.
@@ -2312,6 +2314,19 @@ class IamEnforcementFilterTest {
}
}
@Test
void blankContentTypePreservesTheUnrecognizedClientError() {
ContainerRequestContext request = mock(ContainerRequestContext.class);
when(request.getHeaderString("Content-Type")).thenReturn(" ");
when(request.getMediaType()).thenAnswer(invocation -> MediaType.valueOf(" "));
Response response = IamEnforcementFilter.unrecognizedClientResponse("logs", request);
assertEquals(403, response.getStatus());
assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getMediaType());
assertEquals("UnrecognizedClientException", ((AwsErrorResponse) response.getEntity()).type());
}
@ParameterizedTest
@CsvSource({"ec2,AuthFailure", "iam,InvalidClientTokenId", "sts,InvalidClientTokenId"})
void unknownQueryAccessKeysUseTheServiceErrorEnvelope(String scope, String code) {
@@ -5,9 +5,14 @@ import io.floci.aws.testing.RestAssuredJsonUtils;
import io.floci.aws.testing.ScopedServiceSettingsProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import io.restassured.config.EncoderConfig;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import java.util.TreeSet;
@@ -34,9 +39,16 @@ class PartitionStrictModeIntegrationTest {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void cloudFrontIsRefusedInGovCloud() {
given()
@ParameterizedTest
@NullAndEmptySource
void cloudFrontIsRefusedInGovCloud(String contentType) {
RequestSpecification request = given();
if (contentType != null) {
request.config(RestAssured.config().encoderConfig(
EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)));
request.header("Content-Type", contentType);
}
request
.header("Authorization", PartitionMatrix.sigV4Auth("us-gov-west-1", "cloudfront"))
.when().get("/2020-05-31/distribution").then().statusCode(404)
.header("X-Amzn-Errortype", "UnknownOperationException")