fix(core): tolerate blank content types during role inspection (#9694)

CloudFormation's cfn-response helper sends its ResponseURL callback
with an empty Content-Type, and the request context throws
IllegalArgumentException parsing an empty or whitespace one.
PassedRoles.jsonBody asked for it before reading the body, so role
inspection of such a request failed. It now reads a blank Content-Type
as none and parses the body as JSON, a supplied type still choosing
JSON or CBOR, and the body stays readable by the handler.

Co-authored-by: Hai Yan <8153134+oeyh@users.noreply.github.com>
This commit is contained in:
Jackson PriceandHai Yan authored and GitHub committed 2026-11-28 20:07:59 -04:00
1 parent 96ea43959c
commit 72983e8282
2 files changed
+30 -1

No files matched your search

@@ -1144,7 +1144,9 @@ final class PassedRoles {
if (encoding != null && AwsJsonCborController.isGZipped(List.of(encoding))) {
body = AwsJsonCborController.decodeBody(body);
}
JsonNode node = (isCbor(ctx.getMediaType()) ? CBOR_MAPPER : MAPPER).readTree(body);
String contentType = ctx.getHeaderString(HttpHeaders.CONTENT_TYPE);
MediaType mediaType = contentType != null && contentType.isBlank() ? null : ctx.getMediaType();
JsonNode node = (isCbor(mediaType) ? CBOR_MAPPER : MAPPER).readTree(body);
ctx.setProperty(IamConditionContextResolver.BUFFERED_JSON_BODY, node);
return node;
} catch (IOException | AwsException e) {
@@ -5,20 +5,28 @@ import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import io.floci.aws.core.common.PassedRoles.PassedRole;
import io.floci.aws.testing.MockQueryRequests;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.zip.GZIPOutputStream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -811,6 +819,25 @@ class PassedRolesTest {
xml("<ReplicationConfiguration><Role>" + ROLES + "replication"), LOOKUPS));
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " \t")
void absentOrBlankContentTypePreservesTheCallbackBody(String contentType) throws IOException {
String body = "{\"Status\":\"SUCCESS\",\"PhysicalResourceId\":\"test-resource\"}";
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
ContainerRequestContext request = mock(ContainerRequestContext.class);
when(request.getHeaderString(HttpHeaders.CONTENT_TYPE)).thenReturn(contentType);
if (contentType != null) {
when(request.getMediaType()).thenAnswer(invocation -> MediaType.valueOf(contentType));
}
when(request.getEntityStream()).thenReturn(new ByteArrayInputStream(bytes));
assertEquals(new ObjectMapper().readTree(body), PassedRoles.jsonBody(request));
ArgumentCaptor<InputStream> replay = ArgumentCaptor.forClass(InputStream.class);
verify(request).setEntityStream(replay.capture());
assertArrayEquals(bytes, replay.getValue().readAllBytes());
}
private static String constraint(String type, String parameters) {
return "{\"PortfolioId\":\"port\",\"ProductId\":\"prod\",\"Type\":\"" + type + "\",\"Parameters\":\""
+ parameters + "\"}";