fix(sqs): reject SendMessageBatch when combined payload exceeds MaximumMessageSize (#907)

* fix(sqs): validate SendMessageBatch total payload size up-front

The batch handlers processed entries one at a time and only caught
per-entry size errors, so a batch whose combined payload exceeded the
queue's MaximumMessageSize landed in the response Failed collection
instead of triggering the batch-level BatchRequestTooLong error the
AWS SDK expects (and the BatchRequestTooLongException it surfaces).

Sum each entry's size up-front (using the same body + attribute
accounting as SendMessage), then call a new
SqsService.validateBatchPayloadSize that throws BatchRequestTooLong
when the total exceeds the queue's configured MaximumMessageSize.
Per-entry processing is unchanged -- individual entries that violate
other rules still surface as Failed entries.

* test(sqs): protocol-level coverage for oversized SendMessageBatch

Review feedback on #907: the new validateBatchPayloadSize path was only
exercised by direct SqsService unit tests. Add wire-level tests against
both protocols so the parse/sum/error path can't regress:

- SqsJsonProtocolTest: oversized JSON 1.0 batch returns 400 with
  __type containing BatchRequestTooLong.
- SqsIntegrationTest: oversized Query/form batch returns 400 with
  the BatchRequestTooLong code in the body.

Both confirm the top-level error is emitted before any per-entry
Successful results.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-19 21:49:38 -05:00
1 parent 4c156df10b
commit 24bbd81988
6 files changed
+138 -5

No files matched your search

@@ -383,6 +383,42 @@ class SqsIntegrationTest {
.body(containsString("UnsupportedOperation"));
}
@Test
void sendMessageBatch_queryProtocol_oversizedBatchReturnsBatchRequestTooLong() {
String queueName = "batch-oversize-query-queue";
String queueUrl = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateQueue")
.formParam("QueueName", queueName)
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
try {
String bigBody = "x".repeat(100_000);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "SendMessageBatch")
.formParam("QueueUrl", queueUrl)
.formParam("SendMessageBatchRequestEntry.1.Id", "a")
.formParam("SendMessageBatchRequestEntry.1.MessageBody", bigBody)
.formParam("SendMessageBatchRequestEntry.2.Id", "b")
.formParam("SendMessageBatchRequestEntry.2.MessageBody", bigBody)
.formParam("SendMessageBatchRequestEntry.3.Id", "c")
.formParam("SendMessageBatchRequestEntry.3.MessageBody", bigBody)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("BatchRequestTooLong"));
} finally {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteQueue")
.formParam("QueueUrl", queueUrl)
.when().post("/");
}
}
@Test
void createQueue_idempotent_sameAttributes() {
String queueName = "idempotent-test-queue";
@@ -172,6 +172,30 @@ class SqsJsonProtocolTest {
.body("Successful[0].MD5OfMessageAttributes", notNullValue());
}
@Test
@Order(6)
void sendMessageBatchExceedingQueueMaximumMessageSizeReturnsBatchRequestTooLong() {
// Default queue MaximumMessageSize is 262144 bytes. Each entry is well under the
// per-message limit; the sum is what trips the batch-level check.
String bigBody = "x".repeat(100_000);
String body = "{\"QueueUrl\":\"" + queueUrl + "\","
+ "\"Entries\":["
+ "{\"Id\":\"a\",\"MessageBody\":\"" + bigBody + "\"},"
+ "{\"Id\":\"b\",\"MessageBody\":\"" + bigBody + "\"},"
+ "{\"Id\":\"c\",\"MessageBody\":\"" + bigBody + "\"}"
+ "]}";
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.SendMessageBatch")
.body(body)
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", containsString("BatchRequestTooLong"));
}
@Test
@Order(7)
void receiveMessageOnEmptyQueueOmitsMessagesField() {
@@ -767,6 +767,31 @@ class SqsServiceTest {
assertEquals(1, received.size());
}
@Test
void validateBatchPayloadSize_underQueueLimit_succeeds() {
Queue queue = sqsService.createQueue("batch-q", null);
sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 100_000);
}
@Test
void validateBatchPayloadSize_overQueueLimit_throwsBatchRequestTooLong() {
Queue queue = sqsService.createQueue("batch-q", null);
AwsException ex = assertThrows(AwsException.class, () ->
sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 300_000));
assertEquals("BatchRequestTooLong", ex.getErrorCode());
assertTrue(ex.getMessage().contains("262144"));
}
@Test
void validateBatchPayloadSize_respectsCustomMaximumMessageSize() {
Queue queue = sqsService.createQueue("batch-q",
Map.of("MaximumMessageSize", "1048576"));
sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 1_000_000);
AwsException ex = assertThrows(AwsException.class, () ->
sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 1_048_577));
assertEquals("BatchRequestTooLong", ex.getErrorCode());
}
@Test
void purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {
final var sns = mock(SnsService.class);