fix(sqs): honor MaximumMessageSize attribute on SendMessage (#782)

* fix(sqs): honor MaximumMessageSize attribute on SendMessage

SendMessage previously enforced a hard-coded 256 KiB limit regardless
of the queue's MaximumMessageSize attribute, and the error text always
referenced 262144 bytes. Two behavioral problems:

- Queues created with MaximumMessageSize=1048576 still rejected
  payloads over 256 KiB.
- The size check only inspected the message body (in chars), not body
  bytes plus message attribute name/type/value bytes, the formula AWS
  uses to compute total message size.

Both are now fixed: SendMessage reads the queue's MaximumMessageSize
attribute (clamped to AWS's 1024..1048576 range), computes total size
as UTF-8 body bytes + per-attribute (name + type + value) bytes, and
reports the queue's configured limit in the error message.

Refs #768.

* fix(sqs): clamp MaximumMessageSize to AWS range, rename test var

Address PR review feedback:
- parseMaxMessageSize now clamps out-of-range values to [1024, 1048576]
  instead of silently falling back to the service default, matching the
  PR description and avoiding surprising behavior for queues configured
  with values just outside the AWS range.
- Rename misleading test variable halfMeg (300 KiB body) to body.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-11 23:52:58 -05:00
1 parent 6f9782d093
commit ec9631e8c5
2 files changed
+76 -2

No files matched your search

@@ -355,9 +355,12 @@ public class SqsService {
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
"The specified queue does not exist.", 400));
if (body.length() > maxMessageSize) {
int queueMaxMessageSize = parseMaxMessageSize(queue.getAttributes().get("MaximumMessageSize"));
int totalSize = computeMessageSize(body, messageAttributes);
if (totalSize > queueMaxMessageSize) {
throw new AwsException("InvalidParameterValue",
"Message body must be shorter than " + maxMessageSize + " bytes.", 400);
"One or more parameters are invalid. " +
"Reason: Message must be shorter than " + queueMaxMessageSize + " bytes.", 400);
}
int queueDelaySeconds = parseDelaySecondsAttribute(queue.getAttributes().get("DelaySeconds"));
@@ -446,6 +449,41 @@ public class SqsService {
return message;
}
private int parseMaxMessageSize(String value) {
if (value == null || value.isEmpty()) {
return maxMessageSize;
}
try {
return Math.min(1048576, Math.max(1024, Integer.parseInt(value)));
} catch (NumberFormatException ignored) {
return maxMessageSize;
}
}
/**
* 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) {
int total = body == null ? 0 : body.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
if (attributes == null || attributes.isEmpty()) {
return total;
}
for (Map.Entry<String, MessageAttributeValue> entry : attributes.entrySet()) {
total += entry.getKey().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
MessageAttributeValue value = entry.getValue();
if (value.getDataType() != null) {
total += value.getDataType().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
}
if (value.getBinaryValue() != null) {
total += value.getBinaryValue().length;
} else if (value.getStringValue() != null) {
total += value.getStringValue().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
}
}
return total;
}
/**
* Parse the queue-level DelaySeconds attribute. Returns 0 when the
* attribute is null, empty, non-numeric, or negative -- the queue falls