fix(s3): honor max-keys=0 and reject invalid max-keys values (#3510)

max-keys=0 was treated as "no limit", so ListObjects, ListObjectsV2 and
ListObjectVersions returned every key instead of an empty page, and a
negative value was accepted. AWS answers max-keys=0 with an empty page
and IsTruncated=false, and rejects negative, non-numeric or overflowing
values with InvalidArgument "Argument maxKeys must be an integer between
0 and 2147483647" plus ArgumentName and ArgumentValue.

The parameter is now read as a string so a non-integer value produces
that 400 instead of the 404 JAX-RS emits for a failed Integer conversion.
The DynamoDB import path, which relied on 0 meaning unlimited, now asks
for Integer.MAX_VALUE explicitly.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-12 23:08:32 -05:00
1 parent 34062f6383
commit b994d58e14
5 files changed
+80 -12

No files matched your search

@@ -4017,7 +4017,7 @@ class DynamoDbServiceTest {
private static S3Service s3With(S3Object... objects) {
var s3 = mock(S3Service.class);
when(s3.listObjects("bucket", "imp/", null, 0)).thenReturn(List.of(objects));
when(s3.listObjects("bucket", "imp/", null, Integer.MAX_VALUE)).thenReturn(List.of(objects));
for (var object : objects) {
when(s3.getObjectMetadata("bucket", object.getKey(), null)).thenReturn(object);
when(s3.openObjectStream("bucket", object.getKey(), null))
@@ -4165,7 +4165,7 @@ class DynamoDbServiceTest {
@Test
void runImport_missingBucket_failsWithS3NoSuchBucket() {
var s3 = mock(S3Service.class);
when(s3.listObjects("missing", "imp/", null, 0))
when(s3.listObjects("missing", "imp/", null, Integer.MAX_VALUE))
.thenThrow(new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
var svc = serviceWithS3(s3, new InMemoryStorage<>());
createUsersTableInCreating(svc);
@@ -4182,7 +4182,7 @@ class DynamoDbServiceTest {
@Test
void runImport_otherS3Error_reportsAnS3FailureCode() {
var s3 = mock(S3Service.class);
when(s3.listObjects("bucket", "imp/", null, 0))
when(s3.listObjects("bucket", "imp/", null, Integer.MAX_VALUE))
.thenThrow(new AwsException("AccessDenied", "Access Denied", 403));
var svc = serviceWithS3(s3, new InMemoryStorage<>());
createUsersTableInCreating(svc);
@@ -13,6 +13,7 @@ import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@@ -2245,6 +2246,51 @@ class S3IntegrationTest {
// --- ListObjectsV2 pagination ---
@Test
@Order(100)
void listObjectsValidatesMaxKeys() {
String bucket = "max-keys-test-bucket";
given().when().put("/" + bucket).then().statusCode(200);
given().body("a").when().put("/" + bucket + "/a.txt").then().statusCode(200);
try {
// max-keys=0 is a valid request for an empty page: no keys, and IsTruncated stays false
// even though the bucket has objects.
for (String query : List.of("?max-keys=0", "?list-type=2&max-keys=0")) {
given()
.when()
.get("/" + bucket + query)
.then()
.statusCode(200)
.body(containsString("<MaxKeys>0</MaxKeys>"))
.body(containsString("<IsTruncated>false</IsTruncated>"))
.body(not(containsString("<Key>")))
.body(not(containsString("<NextContinuationToken>")));
}
given()
.when()
.get("/" + bucket + "?versions&max-keys=0")
.then()
.statusCode(200)
.body(containsString("<MaxKeys>0</MaxKeys>"))
.body(containsString("<IsTruncated>false</IsTruncated>"))
.body(not(containsString("<Version>")));
for (String invalid : List.of("-1", "abc", "2147483648")) {
given()
.when()
.get("/" + bucket + "?list-type=2&max-keys=" + invalid)
.then()
.statusCode(400)
.body(containsString("<Code>InvalidArgument</Code>"))
.body(containsString("<ArgumentName>maxKeys</ArgumentName>"))
.body(containsString("<ArgumentValue>" + invalid + "</ArgumentValue>"));
}
} finally {
given().when().delete("/" + bucket + "/a.txt");
given().when().delete("/" + bucket);
}
}
@Test
@Order(101)
void listObjectsV2StartAfterFiltersResults() {