From 0eeee0befc1ce01abc66aac65b4494a46e91d977 Mon Sep 17 00:00:00 2001 From: Mahmoud Khalifa <32329050+M-khalifa@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:33:40 -0600 Subject: [PATCH] fix(sqs): bound the SendMessageBatch total by the 1 MiB limit, not the queue's MaximumMessageSize (#9533) SendMessageBatch compared the batch total with the queue's MaximumMessageSize, so on a queue with a small MaximumMessageSize a batch whose entries each fit was refused whole with BatchRequestTooLong. AWS bounds the batch total by a fixed 1 MiB, while the queue attribute bounds each entry. The total is now compared with maxAllowedMessageSize, which is 1 MiB unless FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE raises it, so a raised ceiling still accepts a batch that SendMessage would accept. Each entry is still checked against the queue's MaximumMessageSize, and the Query and JSON paths share the check. --- .../io/floci/aws/services/sqs/SqsService.java | 10 ++-- .../aws/services/sqs/SqsIntegrationTest.java | 39 ++++++++++++++++ .../aws/services/sqs/SqsJsonProtocolTest.java | 46 +++++++++++++++++++ .../aws/services/sqs/SqsServiceTest.java | 21 +++++++++ 4 files changed, 111 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/floci/aws/services/sqs/SqsService.java b/src/main/java/io/floci/aws/services/sqs/SqsService.java index 43a31ccd47..364891cd7b 100644 --- a/src/main/java/io/floci/aws/services/sqs/SqsService.java +++ b/src/main/java/io/floci/aws/services/sqs/SqsService.java @@ -1359,19 +1359,19 @@ public class SqsService implements Resettable, ResourceProvider { /** * Validate that the combined payload of a {@code SendMessageBatch} call fits within - * the queue's {@code MaximumMessageSize}. AWS counts the batch limit as the sum of + * the 1 MiB batch limit (or the raised configured maximum), independent of the queue's + * {@code MaximumMessageSize}, which bounds each entry. AWS counts the batch limit as the sum of * all entries (body + attributes) and returns {@code BatchRequestTooLong} as a * top-level error -- not as per-entry failures -- when the total exceeds the limit. */ public void validateBatchPayloadSize(String queueUrl, String region, int totalBytes) { String storageKey = regionKey(region, queueUrl); - Queue queue = getQueueByUrl(storageKey, queueUrl) + getQueueByUrl(storageKey, queueUrl) .orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue", "The specified queue does not exist.", 400)); - int max = parseMaxMessageSize(queue.getAttributes().get("MaximumMessageSize")); - if (totalBytes > max) { + if (totalBytes > maxAllowedMessageSize) { throw new AwsException("BatchRequestTooLong", - "Batch requests cannot be longer than " + max + " bytes. " + "Batch requests cannot be longer than " + maxAllowedMessageSize + " bytes. " + "You have sent " + totalBytes + " bytes.", 400); } } diff --git a/src/test/java/io/floci/aws/services/sqs/SqsIntegrationTest.java b/src/test/java/io/floci/aws/services/sqs/SqsIntegrationTest.java index 8443c8cf3e..7560919e8c 100644 --- a/src/test/java/io/floci/aws/services/sqs/SqsIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/sqs/SqsIntegrationTest.java @@ -480,6 +480,45 @@ class SqsIntegrationTest { .body(containsString("UnsupportedOperation")); } + @Test + void sendMessageBatch_queryProtocol_smallMaximumMessageSizeQueueUsesFixedBatchLimit() { + String queueUrl = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", "batch-small-max-query-queue") + .formParam("Attribute.1.Name", "MaximumMessageSize") + .formParam("Attribute.1.Value", "1024") + .when().post("/").then().statusCode(200) + .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); + + try { + String body1000 = "x".repeat(1000); + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "SendMessageBatch") + .formParam("QueueUrl", queueUrl) + .formParam("SendMessageBatchRequestEntry.1.Id", "a") + .formParam("SendMessageBatchRequestEntry.1.MessageBody", body1000) + .formParam("SendMessageBatchRequestEntry.2.Id", "b") + .formParam("SendMessageBatchRequestEntry.2.MessageBody", body1000) + .formParam("SendMessageBatchRequestEntry.3.Id", "c") + .formParam("SendMessageBatchRequestEntry.3.MessageBody", body1000) + .when() + .post("/") + .then() + .statusCode(200) + .body("SendMessageBatchResponse.SendMessageBatchResult.SendMessageBatchResultEntry.size()", + equalTo(3)) + .body(not(containsString("BatchResultErrorEntry"))); + } finally { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", queueUrl) + .when().post("/"); + } + } + @Test void sendMessageBatch_queryProtocol_oversizedBatchReturnsBatchRequestTooLong() { String queueName = "batch-oversize-query-queue"; diff --git a/src/test/java/io/floci/aws/services/sqs/SqsJsonProtocolTest.java b/src/test/java/io/floci/aws/services/sqs/SqsJsonProtocolTest.java index d4860b01db..94e4989580 100644 --- a/src/test/java/io/floci/aws/services/sqs/SqsJsonProtocolTest.java +++ b/src/test/java/io/floci/aws/services/sqs/SqsJsonProtocolTest.java @@ -206,6 +206,52 @@ class SqsJsonProtocolTest { .body("__type", containsString("BatchRequestTooLong")); } + @Test + @Order(6) + void sendMessageBatchOnSmallMaximumMessageSizeQueueUsesFixedBatchLimit() { + String smallQueueUrl = given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body("{\"QueueName\":\"json-batch-small-max-queue\"," + + "\"Attributes\":{\"MaximumMessageSize\":\"1024\"}}") + .when().post("/").then().statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + + String body1000 = "x".repeat(1000); + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.SendMessageBatch") + .body("{\"QueueUrl\":\"" + smallQueueUrl + "\",\"Entries\":[" + + "{\"Id\":\"a\",\"MessageBody\":\"" + body1000 + "\"}," + + "{\"Id\":\"b\",\"MessageBody\":\"" + body1000 + "\"}," + + "{\"Id\":\"c\",\"MessageBody\":\"" + body1000 + "\"}]}") + .when().post("/").then() + .statusCode(200) + .body("Successful.Id", containsInAnyOrder("a", "b", "c")); + + String bigBody = "x".repeat(1024); + StringBuilder entries = new StringBuilder(); + for (int i = 0; i < 10; i++) { + if (i > 0) { + entries.append(','); + } + entries.append("{\"Id\":\"e").append(i).append("\",\"MessageBody\":\"").append(bigBody).append("\"}"); + } + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.SendMessageBatch") + .body("{\"QueueUrl\":\"" + smallQueueUrl + "\",\"Entries\":[" + entries + "]}") + .when().post("/").then() + .statusCode(200) + .body("Successful.size()", equalTo(10)); + + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.DeleteQueue") + .body("{\"QueueUrl\":\"" + smallQueueUrl + "\"}") + .when().post("/"); + } + @Test @Order(6) void sendMessageBatchPersistsAwsTraceHeaderPerEntry() { diff --git a/src/test/java/io/floci/aws/services/sqs/SqsServiceTest.java b/src/test/java/io/floci/aws/services/sqs/SqsServiceTest.java index 3417c94835..7c422d78df 100644 --- a/src/test/java/io/floci/aws/services/sqs/SqsServiceTest.java +++ b/src/test/java/io/floci/aws/services/sqs/SqsServiceTest.java @@ -2591,6 +2591,27 @@ class SqsServiceTest { assertEquals("BatchRequestTooLong", ex.getErrorCode()); } + @Test + void validateBatchPayloadSize_smallQueueMaximumMessageSize_usesFixedBatchLimit() { + Queue queue = sqsService.createQueue("batch-small-q", + Map.of("MaximumMessageSize", "1024"), "us-east-1"); + sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 3_000); + sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 1_048_576); + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 1_048_577)); + assertEquals("BatchRequestTooLong", ex.getErrorCode()); + } + + @Test + void validateBatchPayloadSize_raisedConfiguredMaxMessageSize_raisesBatchLimit() { + SqsService raised = new SqsService(new InMemoryStorage<>(), 30, 2_097_152, BASE_URL, clock); + Queue queue = raised.createQueue("batch-raised-q", null, "us-east-1"); + raised.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 1_572_864); + AwsException ex = assertThrows(AwsException.class, () -> + raised.validateBatchPayloadSize(queue.getQueueUrl(), "us-east-1", 2_097_153)); + assertEquals("BatchRequestTooLong", ex.getErrorCode()); + } + @Test void sendMessage_withAwsTraceHeader_isStoredAndReturnedOnReceive() { Queue q = sqsService.createQueue("traced", null, "us-east-1");