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

@@ -3910,7 +3910,7 @@ public class DynamoDbService implements ResourceProvider {
throw new AwsException("AccessDenied",
"Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied)", 403);
}
var objects = s3Service.listObjects(bucket, prefix, null, 0);
var objects = s3Service.listObjects(bucket, prefix, null, Integer.MAX_VALUE);
if (objects.isEmpty()) {
failImport(desc, "S3NoSuchKey", "No objects found under s3://" + bucket + "/" + prefix);
} else {
@@ -486,7 +486,7 @@ public class S3Controller {
public Response listObjects(@PathParam("bucket") String bucket,
@QueryParam("prefix") String prefix,
@QueryParam("delimiter") String delimiter,
@QueryParam("max-keys") Integer maxKeys,
@QueryParam("max-keys") String maxKeys,
@QueryParam("list-type") String listType,
@QueryParam("continuation-token") String continuationToken,
@QueryParam("start-after") String startAfter,
@@ -639,7 +639,7 @@ public class S3Controller {
s3Service.authorizeListBucket(bucket, authorization);
int max = (maxKeys != null && maxKeys > 0) ? maxKeys : 1000;
int max = resolveMaxKeys(maxKeys);
boolean v1 = !"2".equals(listType);
String effectiveStartAfter = v1 && marker != null ? marker : startAfter;
String effectiveContinuationToken = v1 ? null : continuationToken;
@@ -1655,13 +1655,13 @@ public class S3Controller {
return Response.ok(xml.build()).type(MediaType.APPLICATION_XML).build();
}
private Response handleListObjectVersions(String bucket, String prefix, String delimiter, Integer maxKeys,
private Response handleListObjectVersions(String bucket, String prefix, String delimiter, String maxKeys,
String keyMarker, String versionIdMarker, String encodingType) {
if (hasText(versionIdMarker) && !hasText(keyMarker)) {
throw new AwsException("InvalidArgument",
"A version-id marker cannot be specified without a key marker.", 400);
}
int max = (maxKeys != null && maxKeys > 0) ? maxKeys : 1000;
int max = resolveMaxKeys(maxKeys);
S3Service.ListVersionsResult result =
s3Service.listObjectVersions(bucket, prefix, delimiter, max, keyMarker, versionIdMarker);
XmlBuilder xml = new XmlBuilder()
@@ -3710,6 +3710,25 @@ public class S3Controller {
return Response.ok(xml.build()).build();
}
/**
* AWS accepts any integer from 0 to {@link Integer#MAX_VALUE} for {@code max-keys} and treats
* a missing parameter as 1000. Zero is a valid request for an empty page. Anything else,
* negative, non-numeric or overflowing, is rejected with the same InvalidArgument response.
*/
private static int resolveMaxKeys(String maxKeys) {
if (maxKeys == null) {
return 1000;
}
if (maxKeys.matches("\\d{1,10}") && Long.parseLong(maxKeys) <= Integer.MAX_VALUE) {
return Integer.parseInt(maxKeys);
}
Map<String, Object> detail = new LinkedHashMap<>();
detail.put("ArgumentName", "maxKeys");
detail.put("ArgumentValue", maxKeys);
throw new AwsException("InvalidArgument",
"Argument maxKeys must be an integer between 0 and 2147483647", 400, detail);
}
private Response handlePutBucketWebsite(String bucket, byte[] body) {
String xml = new String(body, StandardCharsets.UTF_8);
String indexDoc = XmlParser.extractFirst(xml, "Suffix", null);
@@ -1381,7 +1381,7 @@ public class S3Service implements Resettable, ResourceProvider {
// see https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
boolean isTruncated = false;
String nextContinuationToken = null;
if (maxKeys > 0) {
if (maxKeys >= 0) {
List<S3Object> limitedObjects = new ArrayList<>();
List<String> limitedPrefixes = new ArrayList<>();
int count = 0;
@@ -1400,7 +1400,10 @@ public class S3Service implements Resettable, ResourceProvider {
}
count++;
}
isTruncated = directObjectCount < allObjects.size() || commonPrefixCount < commonPrefixes.size();
// max-keys=0 is a valid request for an empty page: AWS answers it with IsTruncated=false
// and no continuation token even when the bucket has more objects.
isTruncated = maxKeys > 0
&& (directObjectCount < allObjects.size() || commonPrefixCount < commonPrefixes.size());
if (isTruncated) {
nextContinuationToken = lastEmittedKey;
}
@@ -1615,7 +1618,7 @@ public class S3Service implements Resettable, ResourceProvider {
boolean isTruncated = false;
String nextKeyMarker = null;
String nextVersionIdMarker = null;
if (maxKeys > 0) {
if (maxKeys >= 0) {
List<S3Object> pageVersions = new ArrayList<>();
List<String> pagePrefixes = new ArrayList<>();
int vIdx = 0;
@@ -1641,7 +1644,7 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
isTruncated = vIdx < versions.size() || cpIdx < commonPrefixes.size();
isTruncated = maxKeys > 0 && (vIdx < versions.size() || cpIdx < commonPrefixes.size());
if (!isTruncated) {
nextKeyMarker = null;
nextVersionIdMarker = null;