Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ses): refuse a list token whose scope only starts with the request's (#4870)
Kenshin Okinaka62bcfb95edb7
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobService.javaindex bb84399dd8..984e40f394 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobService.java@@ -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);}diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesListPaging.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesListPaging.javaindex bd34d6c211..e6e3c50522 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesListPaging.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesListPaging.java@@ -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);}diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.javaindex 1b2572e99d..9fa4fb8a5f 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java@@ -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);}diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesListPagingTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesListPagingTest.javaindex e33a528efb..5b25935b42 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesListPagingTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesListPagingTest.java@@ -251,12 +251,32 @@ class SesListPagingTest {@Testvoid 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"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantPagingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantPagingIntegrationTest.javaindex 5c0fa3f971..5310389c24 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantPagingIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantPagingIntegrationTest.java@@ -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);