fix(cloudtrail): validate LookupEvents MaxResults (#7183)

* fix(cloudtrail): validate LookupEvents MaxResults

* fix(cloudtrail): handle huge LookupEvents numbers

* test(cloudtrail): preserve large MaxResults behavior

* fix(eventbridge): retry target dispatch scope failures

* revert(eventbridge): separate retry fix from CloudTrail PR
This commit is contained in:
Rashen Anupama authored and GitHub committed 2026-11-01 11:19:14 -04:00
1 parent 8499581af3
commit 5556bbae58
2 files changed
+121

No files matched your search

@@ -14,6 +14,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -242,11 +243,52 @@ public class CloudTrailJsonHandler {
}
private Response lookupEvents(JsonNode req, String region) {
validateMaxResults(req.path("MaxResults"));
ObjectNode resp = mapper.createObjectNode();
resp.putArray("Events");
return Response.ok(resp).build();
}
/** An omitted or null {@code MaxResults} is optional and takes the service default. */
private static void validateMaxResults(JsonNode maxResults) {
if (maxResults.isMissingNode() || maxResults.isNull()) {
return;
}
int value;
if (maxResults.isNumber()) {
double doubleValue = maxResults.doubleValue();
if (maxResults.isFloatingPointNumber() && !Double.isFinite(doubleValue)) {
value = doubleValue > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
} else {
BigDecimal number = maxResults.decimalValue();
value = number.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0
? Integer.MIN_VALUE : number.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0
? Integer.MAX_VALUE : number.intValue();
}
} else if (maxResults.isTextual()) {
throw serialization("STRING_VALUE can not be converted to an Integer");
} else if (maxResults.isArray()) {
throw serialization("Start of list found where not expected");
} else if (maxResults.isObject()) {
throw serialization("Start of structure or map found where not expected.");
} else {
throw serialization((maxResults.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE")
+ " can not be converted to an Integer");
}
if (value < 1) {
throw invalidMaxResults(value);
}
}
private static AwsException invalidMaxResults(int value) {
return new AwsException("InvalidMaxResultsException",
"A max results value of " + value + " is invalid. Specify max results between 1 and 1000.", 400);
}
private static AwsException serialization(String message) {
return new AwsException("SerializationException", message, 400);
}
private Response addTags(JsonNode req) {
String resourceId = req.path("ResourceId").asText(null);
service.addTags(resourceId, parseTagsList(req.path("TagsList")));
@@ -209,6 +209,85 @@ class CloudTrailIntegrationTest {
.body("Events", hasSize(0));
}
@Test
void lookupEventsAcceptsOmittedAndPositiveMaxResults() {
invokeCloudTrail("LookupEvents", "{}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":null}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":50}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":51}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1000}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1001}")
.then().statusCode(200).body("Events", hasSize(0));
}
@Test
void lookupEventsCoercesFractionalMaxResultsLikeTheSdkParser() {
invokeCloudTrail("LookupEvents", "{\"MaxResults\":0.5}")
.then().statusCode(400)
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of 0 is invalid."));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":-0.5}")
.then().statusCode(400)
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of 0 is invalid."));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1.5}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":50.9}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1000.5}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":2147483648}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":-2147483649}")
.then().statusCode(400)
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of -2147483648 is invalid."));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":1e309}")
.then().statusCode(200).body("Events", hasSize(0));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":-1e309}")
.then().statusCode(400)
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of -2147483648 is invalid."));
}
@Test
void lookupEventsRejectsNonPositiveMaxResults() {
invokeCloudTrail("LookupEvents", "{\"MaxResults\":0}")
.then().statusCode(400)
.body(containsString("\"__type\""))
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of 0 is invalid."))
.body(containsString("Specify max results between 1 and 1000."));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":-1}")
.then().statusCode(400)
.body(containsString("InvalidMaxResultsException"))
.body(containsString("A max results value of -1 is invalid."))
.body(containsString("Specify max results between 1 and 1000."));
}
@Test
void lookupEventsRejectsNonNumericMaxResultsAsSerializationErrors() {
invokeCloudTrail("LookupEvents", "{\"MaxResults\":\"ten\"}")
.then().statusCode(400)
.body(containsString("SerializationException"))
.body(containsString("STRING_VALUE can not be converted to an Integer"));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":[1]}")
.then().statusCode(400)
.body(containsString("SerializationException"))
.body(containsString("Start of list found where not expected"));
invokeCloudTrail("LookupEvents", "{\"MaxResults\":{\"a\":1}}")
.then().statusCode(400)
.body(containsString("SerializationException"))
.body(containsString("Start of structure or map found where not expected"));
}
@Test
void describeTrailsRoundTripsCreatedTrail() {
String suffix = UUID.randomUUID().toString().substring(0, 8);