fix(sqs): honour ReceiveMessageWaitTimeSeconds when ReceiveMessage omits WaitTimeSeconds (#2989)

* fix(sqs): honour ReceiveMessageWaitTimeSeconds when ReceiveMessage omits WaitTimeSeconds

ReceiveMessage treated a missing WaitTimeSeconds as 0 and returned
immediately, ignoring the queue's ReceiveMessageWaitTimeSeconds
attribute. AWS applies that attribute as the default wait, and only an
explicit WaitTimeSeconds in the request overrides it, so consumers that
enable long polling on the queue were short polling against Floci.

Both protocol handlers now pass -1 when the parameter is absent, and the
service resolves the wait from the queue attribute, mirroring the
existing VisibilityTimeout fallback. CreateQueue stores the AWS default
of 0 so GetQueueAttributes reports the attribute like AWS does.

* fix(sqs): reject ReceiveMessage WaitTimeSeconds outside 0 to 20

An explicit negative or non-integer WaitTimeSeconds was parsed to the
"omitted" sentinel and inherited the queue's long poll instead of
failing. Carry the parameter as a nullable Integer, reject malformed
values at the protocol boundary and out-of-range values in the service
with InvalidParameterValue, and cap the queue attribute fallback at the
AWS maximum of 20 seconds.

* fix(sqs): reject fractional WaitTimeSeconds on the SQS JSON protocol

canConvertToInt() accepts fractional JSON numbers such as 1.5, which
asInt() then truncated before the range check, so a malformed request
ran as a one second poll instead of failing with InvalidParameterValue.

Signed-off-by: Kurt McAlpine <kurt.mcalpine@halter.co.nz>

---------

Signed-off-by: Kurt McAlpine <kurt.mcalpine@halter.co.nz>
This commit is contained in:
Kurt McAlpine authored and GitHub committed 2026-09-03 11:29:36 -04:00
1 parent 4bf17a1fe2
commit ad0f4a1613
7 files changed
+302 -12

No files matched your search

@@ -239,11 +239,23 @@ public class SqsJsonHandler {
return Response.ok(response).build();
}
private Integer getOptionalIntField(JsonNode request, String field) {
JsonNode node = request.path(field);
if (node.isMissingNode() || node.isNull()) {
return null;
}
if (!node.isIntegralNumber() || !node.canConvertToInt()) {
throw new AwsException("InvalidParameterValue",
"Value " + node.asText() + " for parameter " + field + " is invalid. Reason: Must be an integer.", 400);
}
return node.asInt();
}
private Response handleReceiveMessage(JsonNode request, String region) {
String queueUrl = request.path("QueueUrl").asText(null);
int maxMessages = request.path("MaxNumberOfMessages").asInt(1);
int visibilityTimeout = request.path("VisibilityTimeout").asInt(-1);
int waitTimeSeconds = request.path("WaitTimeSeconds").asInt(0);
Integer waitTimeSeconds = getOptionalIntField(request, "WaitTimeSeconds");
java.util.Set<String> requestedAttrs = new java.util.LinkedHashSet<>();
requestedAttrs.addAll(jsonNodeToList(request.path("AttributeNames")));
@@ -262,7 +262,7 @@ public class SqsQueryHandler {
String queueUrl = getParam(params, "QueueUrl");
int maxMessages = getIntParam(params, "MaxNumberOfMessages", 1);
int visibilityTimeout = getIntParam(params, "VisibilityTimeout", -1);
int waitTimeSeconds = getIntParam(params, "WaitTimeSeconds", 0);
Integer waitTimeSeconds = getOptionalIntParam(params, "WaitTimeSeconds");
java.util.Set<String> requestedAttrs = new java.util.LinkedHashSet<>();
requestedAttrs.addAll(collectIndexed(params, "AttributeName."));
@@ -564,6 +564,19 @@ public class SqsQueryHandler {
return params.getFirst(name);
}
private Integer getOptionalIntParam(MultivaluedMap<String, String> params, String name) {
String value = params.getFirst(name);
if (value == null) {
return null;
}
try {
return Integer.parseInt(value.trim());
} catch (NumberFormatException e) {
throw new AwsException("InvalidParameterValue",
"Value " + value + " for parameter " + name + " is invalid. Reason: Must be an integer.", 400);
}
}
private int getIntParam(MultivaluedMap<String, String> params, String name, int defaultValue) {
String value = params.getFirst(name);
if (value == null) return defaultValue;
@@ -35,6 +35,7 @@ public class SqsService implements Resettable, ResourceProvider {
private static final Logger LOG = Logger.getLogger(SqsService.class);
private static final int DEDUP_WINDOW_SECONDS = 300; // 5 minutes
private static final int MAX_RECEIVE_WAIT_TIME_SECONDS = 20;
private final StorageBackend<String, Queue> queueStore;
private final StorageBackend<String, List<Message>> messageStore;
@@ -294,6 +295,7 @@ public class SqsService implements Resettable, ResourceProvider {
queue.getAttributes().putIfAbsent("VisibilityTimeout", String.valueOf(defaultVisibilityTimeout));
queue.getAttributes().putIfAbsent("MaximumMessageSize", String.valueOf(maxMessageSize));
queue.getAttributes().putIfAbsent("DelaySeconds", "0");
queue.getAttributes().putIfAbsent("ReceiveMessageWaitTimeSeconds", "0");
queue.getAttributes().putIfAbsent("MessageRetentionPeriod", "345600");
if (queue.isFifo()) {
if (attributes != null && attributes.containsKey("ContentBasedDeduplication") && "true".equals(attributes.get("ContentBasedDeduplication"))) {
@@ -426,7 +428,7 @@ public class SqsService implements Resettable, ResourceProvider {
"Reason: Message must be shorter than " + queueMaxMessageSize + " bytes.", 400);
}
int queueDelaySeconds = parseDelaySecondsAttribute(queue.getAttributes().get("DelaySeconds"));
int queueDelaySeconds = parseNonNegativeSecondsAttribute(queue.getAttributes().get("DelaySeconds"));
// Resolve the effective delay:
// - FIFO queues only support queue-level DelaySeconds per AWS SQS,
@@ -592,12 +594,7 @@ public class SqsService implements Resettable, ResourceProvider {
return total;
}
/**
* Parse the queue-level DelaySeconds attribute. Returns 0 when the
* attribute is null, empty, non-numeric, or negative -- the queue falls
* back to "no default delay" rather than failing the SendMessage call.
*/
private int parseDelaySecondsAttribute(String value) {
private int parseNonNegativeSecondsAttribute(String value) {
if (value == null || value.isEmpty()) {
return 0;
}
@@ -637,18 +634,23 @@ public class SqsService implements Resettable, ResourceProvider {
}
public List<Message> receiveMessage(String queueUrl, int maxMessages, int visibilityTimeout,
int waitTimeSeconds, String region) {
Integer waitTimeSeconds, String region) {
String storageKey = regionKey(region, queueUrl);
getQueueByUrl(storageKey, queueUrl)
Queue queue = getQueueByUrl(storageKey, queueUrl)
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
"The specified queue does not exist.", 400));
if (maxMessages < 1 || maxMessages > 10) {
maxMessages = 1;
}
if (waitTimeSeconds != null && (waitTimeSeconds < 0 || waitTimeSeconds > MAX_RECEIVE_WAIT_TIME_SECONDS)) {
throw new AwsException("InvalidParameterValue",
"Value " + waitTimeSeconds + " for parameter WaitTimeSeconds is invalid. "
+ "Reason: Must be >= 0 and <= " + MAX_RECEIVE_WAIT_TIME_SECONDS + ".", 400);
}
long start = System.currentTimeMillis();
long maxWait = waitTimeSeconds * 1000L;
long maxWait = resolveWaitTimeSeconds(queue, waitTimeSeconds) * 1000L;
Object lock = queueLocks.computeIfAbsent(storageKey, k -> new Object());
// The queue incarnation this call polls against. DeleteQueue removes it
// from messagesByQueue (and CreateQueue registers a fresh instance), so
@@ -689,6 +691,15 @@ public class SqsService implements Resettable, ResourceProvider {
}
}
private int resolveWaitTimeSeconds(Queue queue, Integer requestedWaitTimeSeconds) {
if (requestedWaitTimeSeconds != null) {
return requestedWaitTimeSeconds;
}
int queueWaitTimeSeconds = parseNonNegativeSecondsAttribute(
queue.getAttributes().get("ReceiveMessageWaitTimeSeconds"));
return Math.min(queueWaitTimeSeconds, MAX_RECEIVE_WAIT_TIME_SECONDS);
}
private RedrivePolicy getOrParseRedrivePolicy(Queue queue, String storageKey) {
String rawPolicy = queue.getAttributes().get("RedrivePolicy");
if (rawPolicy == null) {