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

@@ -30,6 +30,7 @@ import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executor;
@@ -370,7 +371,7 @@ public class SesImportJobService implements Resettable {
/** The filter is checked before the page size and token, and a token is bound to it, as on the tenant lists. */
public PaginatedResult<ImportJob> listImportJobs(String region, String destinationType,
SesListPaging paging, Integer pageSize, String nextToken) {
return paging.page(region, "/" + destinationType, listImportJobs(region, destinationType),
return paging.page(region, Objects.toString(destinationType, ""), listImportJobs(region, destinationType),
SesImportJobService::cursor, pageSize, nextToken);
}
@@ -177,7 +177,9 @@ public enum SesListPaging {
/**
* {@code scope} names what else the request selected, a tenant or a filter, for the lists whose
* tokens SES refuses once that changes.
* tokens SES refuses once that changes. The token carries the scope's length, so a scope that
* merely starts with another, which a name holding the token's own separator can do, is not
* taken for it.
*/
<T> PaginatedResult<T> page(String region, String scope, List<T> all, Function<T, String> cursorOf,
Integer pageSize, String nextToken) {
@@ -190,7 +192,8 @@ public enum SesListPaging {
if (emptyToken) {
throw invalidToken.apply(nextToken);
}
return Pagination.paginate(all, cursorOf, limit, nextToken, namespace + "@" + region + scope,
String boundTo = scope.isEmpty() ? "" : "#" + scope.length() + "#" + scope;
return Pagination.paginate(all, cursorOf, limit, nextToken, namespace + "@" + region + boundTo,
invalidToken);
}
@@ -22,6 +22,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
@@ -421,7 +422,7 @@ public class SesTenantService {
public PaginatedResult<TenantResourceAssociation> listTenantResources(Tenant tenant, String typeFilter,
String region, SesListPaging paging,
Integer pageSize, String nextToken) {
return paging.page(region, "/" + tenant.tenantId() + "/" + typeFilter,
return paging.page(region, tenant.tenantId() + "/" + Objects.toString(typeFilter, ""),
listTenantResources(tenant, typeFilter, region), TenantResourceAssociation::resourceArn,
pageSize, nextToken);
}
@@ -444,7 +445,7 @@ public class SesTenantService {
public PaginatedResult<TenantResourceAssociation> listResourceTenants(AssociationResource ref, String region,
SesListPaging paging, Integer pageSize,
String nextToken) {
return paging.page(region, "/" + ref.arn(), listResourceTenants(ref, region),
return paging.page(region, ref.arn(), listResourceTenants(ref, region),
SesTenantService::associationCursor, pageSize, nextToken);
}
@@ -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);