fix(ses): refuse a list token whose scope only starts with the request's (#4870)

* fix(ses): refuse a list token whose scope only starts with the request's

A tenant-list token named its scope by plain concatenation, so a scope that
began with another one, which a name containing the token's own separator can
do, passed the prefix check and was read as a position in the wrong list. The
token now carries the scope's length.

* test(ses): replay a list token on a resource whose name it starts with

Cover the scope fix through ListResourceTenants itself: a token issued for an
identity whose name continues another identity's with the token separator is
refused on the shorter one and still continues its own list.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-01 21:05:34 -04:00
1 parent d72923ab33
commit 62bcfb95ed
5 files changed
+54 -8

No files matched your search

@@ -251,12 +251,32 @@ class SesListPagingTest {
@Test
void scope_bindsATokenToWhatTheRequestSelected() {
SesListPaging paging = SesListPaging.V2_LIST_TENANT_RESOURCES;
String token = paging.page(REGION, "/tenant-a", ITEMS, Function.identity(), 1, null).nextToken();
String token = paging.page(REGION, "tenant-a", ITEMS, Function.identity(), 1, null).nextToken();
assertThat(paging.page(REGION, "/tenant-a", ITEMS, Function.identity(), 1, token).items(), contains("b"));
assertThat(paging.page(REGION, "tenant-a", ITEMS, Function.identity(), 1, token).items(), contains("b"));
assertError("BadRequestException", "Invalid Next Token",
() -> paging.page(REGION, "/tenant-b", ITEMS, Function.identity(), 1, token));
() -> paging.page(REGION, "tenant-b", ITEMS, Function.identity(), 1, token));
assertError("BadRequestException", "Invalid Next Token", () -> page(paging, 1, token));
// A request that selected nothing has an empty scope, and its token is refused once one is added.
String unscoped = page(paging, 1, null).nextToken();
assertError("BadRequestException", "Invalid Next Token",
() -> paging.page(REGION, "tenant-a", ITEMS, Function.identity(), 1, unscoped));
}
@Test
void scope_isNotMatchedByAScopeItMerelyStartsWith() {
SesListPaging paging = SesListPaging.V2_LIST_RESOURCE_TENANTS;
// An identity name may hold the separator the token ends its scope with.
String longer = paging.page(REGION, "identity/a:b", ITEMS, Function.identity(), 1, null).nextToken();
String shorter = paging.page(REGION, "identity/a", ITEMS, Function.identity(), 1, null).nextToken();
assertError("BadRequestException", "Invalid Next Token",
() -> paging.page(REGION, "identity/a", ITEMS, Function.identity(), 1, longer));
assertError("BadRequestException", "Invalid Next Token",
() -> paging.page(REGION, "identity/a:b", ITEMS, Function.identity(), 1, shorter));
assertThat(paging.page(REGION, "identity/a:b", ITEMS, Function.identity(), 1, longer).items(),
contains("b"));
}
@Test
@@ -225,6 +225,27 @@ class SesTenantPagingIntegrationTest {
.body("message", equalTo("Invalid Next Token"));
}
@Test
@Order(10)
void aTokenIsNotTakenForAResourceWhoseNameItsOwnStartsWith() {
// The second name holds the ':' that ends the scope inside a token.
for (String identity : new String[] {"colon.page.test", "colon.page.test:x"}) {
v2().body("{\"EmailIdentity\":\"" + identity + "\"}")
.when().post("/v2/email/identities").then().statusCode(200);
associate("page-b", ARN_PREFIX + "identity/" + identity);
associate("page-c", ARN_PREFIX + "identity/" + identity);
}
String longer = v2().body("{\"ResourceArn\":\"" + ARN_PREFIX + "identity/colon.page.test:x\",\"PageSize\":1}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(200).extract().path("NextToken");
v2().body("{\"ResourceArn\":\"" + ARN_PREFIX + "identity/colon.page.test\",\"NextToken\":\"" + longer + "\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(400)
.body("message", equalTo("Invalid Next Token"));
v2().body("{\"ResourceArn\":\"" + ARN_PREFIX + "identity/colon.page.test:x\",\"NextToken\":\"" + longer + "\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(200)
.body("ResourceTenants.TenantName", contains("page-c"));
}
private static void associate(String tenant, String arn) {
v2().body("{\"TenantName\":\"" + tenant + "\",\"ResourceArn\":\"" + arn + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(200);