fix(cloudwatch): bound inflated metric request bodies (#8490)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-15 14:51:29 -04:00
1 parent b13db563c9
commit dbfe6e0769
7 files changed
+292 -21

No files matched your search

@@ -473,17 +473,20 @@ public class AwsJsonCborController {
}
static byte[] decodeBody(byte[] compressed) {
return decodeBody(compressed, 10 * 1024 * 1024, "PayloadTooLargeException");
}
static byte[] decodeBody(byte[] compressed, int maxRead, String tooLargeErrorCode) {
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 ByteArrayInputStream(compressed))) {
int read;
while ((read = gis.read(buffer)) != -1) {
totalRead += read;
if (totalRead > maxRead) {
throw new AwsException("PayloadTooLargeException", "Payload Too Large", 413);
throw new AwsException(tooLargeErrorCode, "Payload Too Large", 413);
}
baos.write(buffer, 0, read);
}
@@ -27,8 +27,7 @@ import java.io.IOException;
* {@code *}. A condition key read from the body, such as {@code cloudwatch:namespace}, would need
* the body decompressed before IAM.
*
* <p>The decompressed body is bounded as the CBOR protocol bounds it
* ({@link AwsJsonCborController#decodeBody}), and a body that is too large or cannot be
* <p>The decompressed metric publication is bounded at 5 MiB, and a body that is too large or cannot be
* decompressed is refused in the controller's JSON error format. Every other request reads its
* body as sent.
*/
@@ -37,6 +36,8 @@ import java.io.IOException;
@Priority(Priorities.ENTITY_CODER)
public class CloudWatchGzipReaderInterceptor implements ReaderInterceptor {
private static final int DECOMPRESSED_LIMIT = 5 * 1024 * 1024;
private static final MediaType AWS_JSON_1_0 =
MediaType.valueOf(AwsJsonController.CONTENT_TYPE_AWS_JSON_1_0);
@@ -59,7 +60,7 @@ public class CloudWatchGzipReaderInterceptor implements ReaderInterceptor {
// No payload is an operation without input, as on the CBOR protocol, not a malformed gzip stream.
if (body.length > 0) {
try {
body = AwsJsonCborController.decodeBody(body);
body = AwsJsonCborController.decodeBody(body, DECOMPRESSED_LIMIT, "RequestEntityTooLargeException");
} catch (AwsException e) {
throw new WebApplicationException(JsonErrorResponseUtils.createErrorResponse(e));
}