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 d5e40bff8a..9ca686e319 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 @@ -1,9 +1,11 @@ package io.github.hectorvent.floci.core.common; +import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import com.fasterxml.jackson.dataformat.cbor.CBORGenerator; +import com.google.gson.JsonParseException; import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsJsonHandler; import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler; import io.github.hectorvent.floci.services.dynamodb.DynamoDbStreamsJsonHandler; @@ -19,8 +21,11 @@ import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.zip.GZIPInputStream; /** * Generic dispatcher for all AWS services that use the application/cbor protocol, @@ -182,9 +187,7 @@ public class AwsJsonCborController { LOG.debugv("Smithy RPC v2 CBOR: service={0}, operation={1}", serviceId, operation); try { - JsonNode request = (body != null && body.length > 0) - ? CBOR_MAPPER.readTree(body) - : objectMapper.createObjectNode(); + JsonNode request = bodyToJson(httpHeaders, body); String region = regionResolver.resolveRegion(httpHeaders); Response delegated = dispatchCbor(serviceId, operation, request, region); @@ -204,12 +207,41 @@ public class AwsJsonCborController { .build(); } catch (AwsException e) { return cborErrorResponse(e, "smithy-protocol", responseContentType(httpHeaders)); + } catch (JacksonException e) { + return cborErrorResponse(new AwsException("SerializationException", e.getMessage(), 400), + "smithy-protocol", responseContentType(httpHeaders)); } catch (Exception e) { LOG.error("Error processing Smithy CBOR request: " + serviceId + "." + operation, e); return Response.status(500).build(); } } + JsonNode bodyToJson(HttpHeaders httpHeaders, byte[] body) throws IOException { + JsonNode request; + if (body != null && body.length > 0) { + if( httpHeaders.getRequestHeader("Content-encoding") != null && isGZipped(httpHeaders.getRequestHeader("Content-encoding"))) { + body = decodeBody(body); + } + request = CBOR_MAPPER.readTree(body); + } else { + request = objectMapper.createObjectNode(); + } + return request; + } + + private boolean isGZipped(List contentEncodingHeaders) { + if (contentEncodingHeaders == null) { + return false; + } + for (String header : contentEncodingHeaders) { + if (header != null && header.toLowerCase(java.util.Locale.ROOT).contains("gzip")) { + return true; + } + } + return false; + + } + /** * Handles AWS services that migrated to the smithy-rpc-v2-cbor protocol at root path. * Fallback handler for X-Amz-Target based routing with CBOR body. @@ -240,9 +272,7 @@ public class AwsJsonCborController { LOG.debugv("{0} CBOR action: {1}", serviceKey, action); try { - JsonNode request = (body != null && body.length > 0) - ? CBOR_MAPPER.readTree(body) - : objectMapper.createObjectNode(); + JsonNode request = bodyToJson(httpHeaders, body); String region = regionResolver.resolveRegion(httpHeaders); Response delegated = switch (serviceKey) { @@ -275,12 +305,37 @@ public class AwsJsonCborController { .build(); } catch (AwsException e) { return cborErrorResponse(e, "smithy-protocol", responseContentType(httpHeaders)); + } catch (JacksonException e) { + return cborErrorResponse(new AwsException("SerializationException", e.getMessage(), 400), + "smithy-protocol", responseContentType(httpHeaders)); } catch (Exception e) { LOG.error("Error processing CBOR request: " + serviceKey + "." + action, e); return Response.status(500).build(); } } + private byte[] decodeBody(byte[] compressed) { + int buffSize = 64 * 1024; + byte[] buffer = new byte[buffSize]; // 10 MB limit max over all aws services, to avoid OOM. AWS services should not send more than 10 MB in a single request. + int totalRead = 0; + int maxRead = 10 * 1024 * 1024; // 10 MB limit max over all aws services, to avoid OOM. AWS services should not send more than 10 MB in a single request. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (GZIPInputStream gis = new GZIPInputStream(new java.io.ByteArrayInputStream(compressed))) { + int read; + while ((read = gis.read(buffer)) != -1) { + totalRead += read; + if (totalRead > maxRead) { + throw new AwsException("PayloadTooLargeException", "Payload Too Large", 413); + } + baos.write(buffer, 0, read); + } + + return baos.toByteArray(); + } catch (IOException e) { + throw new AwsException("InvalidRequestException", "Failed to decode request body", 400); + } + } + /** * Dispatches a CBOR request to the appropriate service handler by SDK service ID. */ diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonCborIncomingRequestIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonCborIncomingRequestIntegrationTest.java new file mode 100644 index 0000000000..82e67dbde4 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonCborIncomingRequestIntegrationTest.java @@ -0,0 +1,98 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; + +import java.io.ByteArrayOutputStream; +import java.util.Map; +import java.util.zip.GZIPOutputStream; + +import org.jboss.resteasy.reactive.server.jaxrs.HttpHeadersImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +/** + * Integration test for {@link AwsJsonCborController#bodyToJson(HttpHeaders, byte[])}. + *

+ * Verifies handling of incoming smithy-rpc-v2-cbor request bodies: a gzip-encoded body + * (as signalled by a {@code Content-Encoding: gzip} header) must be transparently + * decompressed and decoded back into the original {@link JsonNode}, and a decompressed + * body that exceeds the 10 MB safety limit enforced in {@code decodeBody} must fail fast + * with an {@link AwsException} (413 Payload Too Large) rather than risk an OOM from an + * unbounded gzip bomb. + *

+ * Boots Quarkus ({@code @QuarkusTest}) since the controller is injected and exercises the + * real request-decoding path used by CloudWatch Metrics and other smithy-rpc-v2-cbor + * services. + */ +@QuarkusTest +class AwsJsonCborIncomingRequestIntegrationTest { + + @Inject AwsJsonCborController awsJsonCborController; + + @Test + void gzippedRequestIsDecompressed() throws Exception { + + ObjectMapper jsonMapper = new ObjectMapper(); + ObjectNode metrics = jsonMapper.createObjectNode(); + metrics.put("Namespace", "Test"); + ArrayNode metricData = metrics.putArray("MetricData"); + + ObjectNode o = metricData.addObject(); + o.put("MetricName", "SYSTEM_NORMALIZED_CPU_STEAL"); + ArrayNode dimensions = o.putArray("Dimensions"); + ObjectNode dimension = dimensions.addObject(); + dimension.put("Name","clusterName"); + dimension.put("Value", "test"); + o.put("Value", 9344221); + o.put("Unit", "Seconds"); + + // Encode the metrics payload to CBOR and gzip it, simulating what a real AWS SDK + // client sends when it compresses a smithy-rpc-v2-cbor request body. + byte[] compressedNode = AwsJsonCborController.nodeToSmithyCbor(metrics); + ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); + try (GZIPOutputStream gon = new GZIPOutputStream(gzipped)) { + gon.write(compressedNode); + } + + // bodyToJson must detect the Content-Encoding header, gunzip the body, and decode + // the resulting CBOR bytes back into the original JsonNode. + JsonNode node = + awsJsonCborController.bodyToJson( + new HttpHeadersImpl(Map.of("Content-Encoding", "gzip").entrySet()), + gzipped.toByteArray()); + + Assertions.assertEquals(metrics, node); + } + + @Test + void payloadTooLargeExceptionThrown() throws Exception { + + // No need for a valid CBOR structure here — decodeBody's 10 MB limit is enforced + // purely on decompressed byte count, so raw pseudo-random bytes are enough to + // exceed it. The varying pattern (instead of all-zero bytes) also keeps gzip from + // trivially collapsing the payload via run-length compression. + int elevenMB = 11 * 1024 * 1024; + byte[] rawBytes = new byte[elevenMB]; + for (int i = 0,j = 37; i < elevenMB; i++, j+=19) { + rawBytes[i] = (byte) (i+j & 0xFF); + } + ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); + try (GZIPOutputStream gon = new GZIPOutputStream(gzipped)) { + gon.write(rawBytes); + } + + // Exceeding the decompressed size limit must surface as a 413 Payload Too Large + // AwsException, not an OutOfMemoryError or a silently truncated body. + Assertions.assertThrows(AwsException.class, () -> { + awsJsonCborController.bodyToJson( + new HttpHeadersImpl(Map.of("Content-Encoding", "gzip").entrySet()), + gzipped.toByteArray()); + }); + } +}