mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
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:
1 parent
4c156df10b
commit
24bbd81988
6 files changed
+138
-5
No files matched your search
@@ -339,6 +339,11 @@ public class SqsJsonHandler {
|
||||
ArrayNode successful = objectMapper.createArrayNode();
|
||||
ArrayNode failed = objectMapper.createArrayNode();
|
||||
|
||||
record ParsedEntry(String id, String body, int delay, String groupId, String dedupId,
|
||||
Map<String, MessageAttributeValue> attributes) {}
|
||||
|
||||
List<ParsedEntry> parsedEntries = new ArrayList<>();
|
||||
int totalSize = 0;
|
||||
if (entries.isArray()) {
|
||||
for (JsonNode entry : entries) {
|
||||
String id = entry.path("Id").asText();
|
||||
@@ -366,9 +371,19 @@ public class SqsJsonHandler {
|
||||
});
|
||||
}
|
||||
|
||||
totalSize += SqsService.computeMessageSize(messageBody, messageAttributes);
|
||||
parsedEntries.add(new ParsedEntry(id, messageBody, delaySeconds,
|
||||
messageGroupId, messageDeduplicationId, messageAttributes));
|
||||
}
|
||||
}
|
||||
|
||||
sqsService.validateBatchPayloadSize(queueUrl, region, totalSize);
|
||||
|
||||
for (ParsedEntry parsed : parsedEntries) {
|
||||
String id = parsed.id();
|
||||
try {
|
||||
Message msg = sqsService.sendMessage(queueUrl, messageBody, delaySeconds,
|
||||
messageGroupId, messageDeduplicationId, messageAttributes, region);
|
||||
Message msg = sqsService.sendMessage(queueUrl, parsed.body(), parsed.delay(),
|
||||
parsed.groupId(), parsed.dedupId(), parsed.attributes(), region);
|
||||
ObjectNode success = objectMapper.createObjectNode();
|
||||
success.put("Id", id);
|
||||
success.put("MessageId", msg.getMessageId());
|
||||
@@ -388,7 +403,6 @@ public class SqsJsonHandler {
|
||||
fail.put("SenderFault", true);
|
||||
failed.add(fail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
|
||||
@@ -314,6 +314,11 @@ public class SqsQueryHandler {
|
||||
String queueUrl = getParam(params, "QueueUrl");
|
||||
var xml = new XmlBuilder();
|
||||
|
||||
record ParsedEntry(String id, String body, int delay, String groupId, String dedupId,
|
||||
Map<String, MessageAttributeValue> attributes) {}
|
||||
|
||||
List<ParsedEntry> parsedEntries = new ArrayList<>();
|
||||
int totalSize = 0;
|
||||
for (int i = 1; ; i++) {
|
||||
String id = getParam(params, "SendMessageBatchRequestEntry." + i + ".Id");
|
||||
if (id == null) break;
|
||||
@@ -339,8 +344,18 @@ public class SqsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
totalSize += SqsService.computeMessageSize(body, messageAttributes);
|
||||
parsedEntries.add(new ParsedEntry(id, body, delaySeconds, messageGroupId,
|
||||
messageDeduplicationId, messageAttributes));
|
||||
}
|
||||
|
||||
sqsService.validateBatchPayloadSize(queueUrl, region, totalSize);
|
||||
|
||||
for (ParsedEntry parsed : parsedEntries) {
|
||||
String id = parsed.id();
|
||||
try {
|
||||
var msg = sqsService.sendMessage(queueUrl, body, delaySeconds, messageGroupId, messageDeduplicationId, messageAttributes, region);
|
||||
var msg = sqsService.sendMessage(queueUrl, parsed.body(), parsed.delay(),
|
||||
parsed.groupId(), parsed.dedupId(), parsed.attributes(), region);
|
||||
xml.start("SendMessageBatchResultEntry")
|
||||
.elem("Id", id)
|
||||
.elem("MessageId", msg.getMessageId())
|
||||
|
||||
@@ -505,11 +505,30 @@ public class SqsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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)
|
||||
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
"The specified queue does not exist.", 400));
|
||||
int max = parseMaxMessageSize(queue.getAttributes().get("MaximumMessageSize"));
|
||||
if (totalBytes > max) {
|
||||
throw new AwsException("BatchRequestTooLong",
|
||||
"Batch requests cannot be longer than " + max + " bytes. "
|
||||
+ "You have sent " + totalBytes + " bytes.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Total wire-level message size, in bytes, matching AWS SQS accounting:
|
||||
* UTF-8 body bytes + per-attribute (name UTF-8 + type UTF-8 + value bytes).
|
||||
*/
|
||||
private static int computeMessageSize(String body, Map<String, MessageAttributeValue> attributes) {
|
||||
public static int computeMessageSize(String body, Map<String, MessageAttributeValue> attributes) {
|
||||
int total = body == null ? 0 : body.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
|
||||
if (attributes == null || attributes.isEmpty()) {
|
||||
return total;
|
||||
|
||||
Reference in new issue
Block a user