fix(stepfunctions): paginate ListActivities results (#6181)

* fix(stepfunctions): paginate ListActivities results

* fix(stepfunctions): validate ListActivities token length
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-21 17:34:31 -04:00
1 parent 2ecc01b3e5
commit 2d1fbf33eb
3 files changed
+152 -6

No files matched your search

@@ -552,15 +552,19 @@ public class StepFunctionsJsonHandler {
}
private Response handleListActivities(JsonNode request, String region) {
List<Activity> list = service.listActivities(region);
PaginatedResult<Activity> page = service.listActivities(region,
parseOptionalInt(request, "maxResults"), optionalText(request, "nextToken"));
ObjectNode response = objectMapper.createObjectNode();
ArrayNode array = response.putArray("activities");
for (Activity a : list) {
for (Activity a : page.items()) {
ObjectNode item = array.addObject();
item.put("activityArn", a.getActivityArn());
item.put("name", a.getName());
item.put("creationDate", a.getCreationDate());
}
if (page.nextToken() != null) {
response.put("nextToken", page.nextToken());
}
return Response.ok(response).build();
}
@@ -696,9 +700,9 @@ public class StepFunctionsJsonHandler {
/**
* Reads an optional integer field from the JSON request, rejecting wrong types
* with a typed ValidationException. JsonNode.asInt() silently coerces strings,
* fractional numbers, and explicit nulls to 0, which would let invalid payloads
* slip through as 0 (and thus be treated as "use default" by the service).
* with a typed ValidationException. JsonNode.intValue() silently truncates
* out-of-range integers, while asInt() also coerces wrong types to 0; either
* can make an invalid value look like "use default" to the service.
* Returns null when the field is absent or explicitly null in the JSON.
*/
private static Integer parseOptionalInt(JsonNode request, String fieldName) {
@@ -706,7 +710,7 @@ public class StepFunctionsJsonHandler {
if (node == null || node.isNull()) {
return null;
}
if (!node.isIntegralNumber()) {
if (!node.isIntegralNumber() || !node.canConvertToInt()) {
throw new AwsException("ValidationException",
"Value '" + node + "' at '" + fieldName
+ "' failed to satisfy constraint: Member must be an integer.", 400);
@@ -1144,6 +1144,19 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
return activityStore.scan(k -> k.startsWith(prefix) && k.contains(":activity:"));
}
public PaginatedResult<Activity> listActivities(String region, Integer maxResults, String nextToken) {
if (maxResults != null && (maxResults < 0 || maxResults > 1000)) {
throw new AwsException("ValidationException", "maxResults must be between 0 and 1000.", 400);
}
if (nextToken != null && (nextToken.isEmpty() || nextToken.length() > 1024)) {
throw new AwsException("ValidationException", "nextToken length must be between 1 and 1024.", 400);
}
int pageSize = maxResults == null || maxResults == 0 ? 100 : maxResults;
return Pagination.paginate(listActivities(region), Activity::getActivityArn, pageSize, nextToken,
"states:activities:" + region,
token -> new AwsException("InvalidToken", "Invalid nextToken.", 400));
}
public void deleteActivity(String arn) {
activityStore.delete(arn);
activityQueues.remove(arn);
@@ -0,0 +1,129 @@
package io.github.hectorvent.floci.services.stepfunctions;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class StepFunctionsListActivitiesPaginationIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void maxResultsCapsThePageAndReturnsAContinuationToken() {
String prefix = "page-" + UUID.randomUUID() + "-";
List<String> arns = new ArrayList<>();
try {
for (int index = 0; index < 2; index++) {
Response created = given()
.header("X-Amz-Target", "AWSStepFunctions.CreateActivity")
.contentType(CONTENT_TYPE)
.body(Map.of("name", prefix + index))
.post("/")
.then().statusCode(200).extract().response();
arns.add(created.jsonPath().getString("activityArn"));
}
Set<String> listedArns = new HashSet<>();
Set<String> issuedTokens = new HashSet<>();
String nextToken = null;
do {
Response page = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(nextToken == null ? Map.of("maxResults", 1)
: Map.of("maxResults", 1, "nextToken", nextToken))
.post("/")
.then().statusCode(200).extract().response();
List<String> pageArns = page.jsonPath().getList("activities.activityArn");
assertEquals(1, pageArns.size(), "maxResults=1 must cap every page");
assertTrue(listedArns.add(pageArns.getFirst()), "pages must not repeat an activity");
nextToken = page.jsonPath().getString("nextToken");
if (nextToken != null) {
assertTrue(issuedTokens.add(nextToken), "pagination must not repeat a token");
}
} while (nextToken != null);
assertTrue(listedArns.containsAll(arns), "both created activities must be listed");
assertTrue(!issuedTokens.isEmpty(), "more than one activity needs a continuation token");
Response defaultPage = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(Map.of("maxResults", 0))
.post("/")
.then().statusCode(200).extract().response();
List<String> defaultArns = defaultPage.jsonPath().getList("activities.activityArn");
assertEquals(Math.min(100, listedArns.size()), defaultArns.size(),
"maxResults=0 uses the default page size");
} finally {
for (String arn : arns) {
given()
.header("X-Amz-Target", "AWSStepFunctions.DeleteActivity")
.contentType(CONTENT_TYPE)
.body(Map.of("activityArn", arn))
.post("/")
.then().statusCode(200);
}
}
}
@Test
void rejectsPageSizeThatCannotFitInInteger() {
Response response = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(Map.of("maxResults", 4294967296L))
.post("/")
.then().statusCode(400).extract().response();
assertEquals("ValidationException", response.jsonPath().getString("__type"));
}
@Test
void rejectsInvalidPageSizeAndToken() {
for (Object maxResults : List.of(-1, 1001, "1")) {
Response response = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(Map.of("maxResults", maxResults))
.post("/")
.then().statusCode(400).extract().response();
assertEquals("ValidationException", response.jsonPath().getString("__type"));
}
for (String nextToken : List.of("", "x".repeat(1025))) {
Response response = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(Map.of("nextToken", nextToken))
.post("/")
.then().statusCode(400).extract().response();
assertEquals("ValidationException", response.jsonPath().getString("__type"));
}
Response response = given()
.header("X-Amz-Target", "AWSStepFunctions.ListActivities")
.contentType(CONTENT_TYPE)
.body(Map.of("nextToken", "not-a-valid-token"))
.post("/")
.then().statusCode(400).extract().response();
assertEquals("InvalidToken", response.jsonPath().getString("__type"));
}
}