fix(sqs): reject invalid receive message counts (#6827)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-28 13:19:19 -04:00
1 parent f926968167
commit 8a19c3024b
9 files changed
+194 -5

No files matched your search

@@ -29,6 +29,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.jupiter.api.Assertions.*;
@@ -495,6 +496,35 @@ class SqsEventSourcePollerTest {
return fn;
}
@Test
void largeBatchesUseLegalReceiveCountsAndStillDeliverTheWholeBatch() throws Exception {
LambdaFunction fn = stubThrowFn();
EventSourceMapping esm = esm();
esm.setBatchSize(25);
esm.setMaximumBatchingWindowInSeconds(300);
poller.setClockForTest(() -> 0L);
List<Message> messages = new ArrayList<>();
for (int i = 0; i < 25; i++) {
messages.add(message("large-" + i));
}
AtomicInteger position = new AtomicInteger();
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenAnswer(call -> {
int count = call.getArgument(1);
assertTrue(count >= 1 && count <= 10);
int start = position.getAndAdd(count);
return messages.subList(start, Math.min(start + count, messages.size()));
});
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
for (int i = 0; i < 3; i++) {
awaitPoll(poller.pollAndInvoke(esm));
}
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(executorService).invoke(eq(fn), payload.capture(), eq(InvocationType.RequestResponse));
assertEquals(25, OBJECT_MAPPER.readTree(payload.getValue()).path("Records").size());
}
private void stubReceive(EventSourceMapping esm, List<Message> messages) {
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(messages);
@@ -167,6 +167,18 @@ class PipesPollerTest {
assertEquals("not-json", node.get(0).asText());
}
@Test
void sqsPollingKeepsConfiguredLargeBatchesWithinTheReceiveLimit() throws Exception {
Pipe pipe = new Pipe();
pipe.setName("large-sqs-batch");
pipe.setSource("arn:aws:sqs:us-east-1:000000000000:src-queue");
pipe.setSourceParameters(MAPPER.readTree("{\"SqsQueueParameters\":{\"BatchSize\":25}}"));
when(sqsService.receiveMessage(anyString(), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of());
poller.pollSqs(pipe, "us-east-1");
verify(sqsService).receiveMessage(anyString(), eq(10), anyInt(), anyInt(), eq("us-east-1"));
}
@Test
void pollSqs_enrichmentToNonLambdaTargetForwardsRawResponse() throws Exception {
// A non-Lambda target (here Step Functions) must receive the raw enrichment response, not a
@@ -0,0 +1,138 @@
package io.github.hectorvent.floci.services.sqs;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.Message;
import software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName;
import software.amazon.awssdk.services.sqs.model.SqsException;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;
@QuarkusTest
class SqsReceiveCountIntegrationTest {
private static final String ACCOUNT = "741682000001";
private static final String REGION = "eu-west-2";
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261028/" + REGION + "/sqs/aws4_request";
private SqsClient client;
private String queueUrl;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void createQueue() {
client = AwsTestClients.create(SqsClient.builder(), ACCOUNT, Region.of(REGION));
String created = client.createQueue(request -> request.queueName("receive-count-" + UUID.randomUUID())).queueUrl();
queueUrl = "http://localhost:" + RestAssured.port + URI.create(created).getRawPath();
}
@AfterEach
void cleanUp() {
try {
if (queueUrl != null) {
client.deleteQueue(request -> request.queueUrl(queueUrl));
}
} finally {
client.close();
}
}
@ParameterizedTest
@ValueSource(ints = {-1, 0, 11, Integer.MIN_VALUE, Integer.MAX_VALUE})
void sdkRejectsInvalidCountsWithoutReceivingAMessage(int count) {
send("kept");
SqsException error = assertThrows(SqsException.class, () -> client.receiveMessage(request -> request
.queueUrl(queueUrl).maxNumberOfMessages(count).waitTimeSeconds(0)));
assertEquals(400, error.statusCode());
assertEquals("InvalidParameterValue", error.awsErrorDetails().errorCode());
assertTrue(error.awsErrorDetails().errorMessage().contains("MaxNumberOfMessages"));
assertUnreceivedMessageRemains();
}
@ParameterizedTest
@ValueSource(ints = {1, 10})
void sdkAcceptsBothRangeBoundaries(int count) {
for (int i = 0; i < 12; i++) {
send("message-" + i);
}
assertEquals(count, client.receiveMessage(request -> request.queueUrl(queueUrl)
.maxNumberOfMessages(count).waitTimeSeconds(0)).messages().size());
}
@Test
void omittedAndNullCountsKeepTheDefaultOfOne() {
send("first");
send("second");
assertEquals(1, client.receiveMessage(request -> request.queueUrl(queueUrl)
.visibilityTimeout(0).waitTimeSeconds(0)).messages().size());
given().contentType("application/x-amz-json-1.0").header("Authorization", AUTH)
.header("X-Amz-Target", "AmazonSQS.ReceiveMessage")
.body("{\"QueueUrl\":\"" + queueUrl + "\",\"MaxNumberOfMessages\":null}")
.post("/").then().statusCode(200).body("Messages.size()", equalTo(1));
}
@ParameterizedTest
@ValueSource(strings = {"-1", "0", "11", "2147483648", "4294967297", "1.5", "invalid", ""})
void queryProtocolRejectsInvalidCountsAtBothEndpoints(String count) {
send("kept");
for (String path : List.of("/", URI.create(queueUrl).getRawPath())) {
given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", "ReceiveMessage").formParam("QueueUrl", queueUrl)
.formParam("MaxNumberOfMessages", count).formParam("WaitTimeSeconds", 0)
.post(path).then().statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"));
}
assertUnreceivedMessageRemains();
}
@ParameterizedTest
@ValueSource(strings = {"2147483648", "4294967297", "9223372036854775808", "1.5", "\"1\"", "true", "{}", "[]"})
void jsonProtocolRejectsCoercionAndOverflowAtBothEndpoints(String count) {
send("kept");
for (String path : List.of("/", URI.create(queueUrl).getRawPath())) {
given().contentType("application/x-amz-json-1.0").header("Authorization", AUTH)
.header("X-Amz-Target", "AmazonSQS.ReceiveMessage")
.body("{\"QueueUrl\":\"" + queueUrl + "\",\"MaxNumberOfMessages\":" + count + "}")
.post(path).then().statusCode(400).body("__type", equalTo("InvalidParameterValue"));
}
assertUnreceivedMessageRemains();
}
@Test
void anEmptyQueueStillRejectsAnInvalidCount() {
SqsException error = assertThrows(SqsException.class, () -> client.receiveMessage(request -> request
.queueUrl(queueUrl).maxNumberOfMessages(0).waitTimeSeconds(0)));
assertEquals("InvalidParameterValue", error.awsErrorDetails().errorCode());
}
private void send(String body) {
client.sendMessage(request -> request.queueUrl(queueUrl).messageBody(body));
}
private void assertUnreceivedMessageRemains() {
List<Message> messages = client.receiveMessage(request -> request.queueUrl(queueUrl).maxNumberOfMessages(1)
.waitTimeSeconds(0).messageSystemAttributeNames(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT)).messages();
assertEquals(1, messages.size());
assertEquals("kept", messages.getFirst().body());
assertEquals("1", messages.getFirst().attributesAsStrings().get("ApproximateReceiveCount"));
}
}