From e1796b33b1eabb91c5946dfcda10dbcda421b765 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sun, 25 Oct 2026 02:35:47 +0900 Subject: [PATCH] fix(ses): refuse more than 50 bulk destinations the way SES does (#6430) * fix(ses): refuse more than 50 bulk destinations the way SES does SendBulkEmail and SendBulkTemplatedEmail answered MessageRejected "Number of destinations (N) exceeds the maximum of 50." after resolving the stored template, configuration set and tenant. SES answers "You must specify at least 1 destination, and no more than 50 destinations" (BadRequestException on v2, InvalidParameterValue on v1) before those lookups, and v2 answers an empty BulkEmailEntries the same way. An absent v2 BulkEmailEntries is now the Smithy not-null violation. * fix(ses): keep a null bulk entry list on the count error --- .../floci/services/ses/SesQueryHandler.java | 6 +- .../floci/services/ses/SesSendController.java | 22 +-- .../floci/services/ses/SesService.java | 21 ++- .../ses/SesBulkV1IntegrationTest.java | 44 +++-- .../ses/SesBulkV2IntegrationTest.java | 164 ++++++++++++++++-- 5 files changed, 216 insertions(+), 41 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java index 10071f1d09..79cee27fac 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java @@ -779,8 +779,6 @@ public class SesQueryHandler { "Template or TemplateArn is required.", 400); } String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn); - EmailTemplate template = templateService.getTemplate(resolvedName, region); - JsonNode defaultTemplateData = parseTemplateData(defaultDataRaw); List entries = new ArrayList<>(); for (int i = 1; ; i++) { @@ -801,6 +799,10 @@ public class SesQueryHandler { throw new AwsException("InvalidParameterValue", "At least one destination is required.", 400); } + // AWS counts the destinations before it looks the template up (probe-confirmed). + SesService.requireBulkDestinationCount(entries.size()); + EmailTemplate template = templateService.getTemplate(resolvedName, region); + JsonNode defaultTemplateData = parseTemplateData(defaultDataRaw); String configurationSetName = getParam(params, "ConfigurationSetName"); String returnPath = getParam(params, "ReturnPath"); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java index 2d77d7eb36..b886cb7f2b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java @@ -247,6 +247,10 @@ public class SesSendController { JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendBulkEmail", request); validateTrackingOverrides(request); + JsonNode bulkEntries = request.path("BulkEmailEntries"); + if (bulkEntries.isMissingNode() || bulkEntries.isNull()) { + throw constraintViolation(null, "bulkEmailEntries", "Member must not be null"); + } String fromEmailAddress = request.path("FromEmailAddress").asText(null); if (fromEmailAddress == null || fromEmailAddress.isBlank()) { throw new AwsException("BadRequestException", @@ -281,9 +285,9 @@ public class SesSendController { "DefaultContent.Template requires TemplateName, TemplateArn, or TemplateContent.", 400); } - String subject; - String text; - String html; + String subject = null; + String text = null; + String html = null; // Inline template content is not a stored template resource, so it stays out of the // tenant gate below. String gateTemplateName = null; @@ -294,7 +298,11 @@ public class SesSendController { html = inline.path("Html").asText(null); // Shape validation belongs before the tenant gate below, as on SendEmail. SesService.requireInlineTemplateContent(subject, text, html); - } else { + } + // AWS counts the entries after the inline content check and before it looks the stored + // template up (probe-confirmed). + SesService.requireBulkDestinationCount(bulkEntries.size()); + if (!hasInline) { String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn); @@ -309,12 +317,6 @@ public class SesSendController { List defaultHeaders = parseHeadersArray(template.path("Headers"), "defaultContent.template.headers"); - JsonNode bulkEntries = request.path("BulkEmailEntries"); - if (!bulkEntries.isArray() || bulkEntries.isEmpty()) { - throw new AwsException("BadRequestException", - "BulkEmailEntries must be a non-empty array.", 400); - } - List entries = new ArrayList<>(); int entryIndex = 1; for (JsonNode node : bulkEntries) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index f4b9fb82ad..a1fe8cdd96 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -1708,6 +1708,17 @@ public class SesService { } } + // Probe-confirmed: both bulk sends count the destinations before the stored template lookup, + // v2 also before the configuration set and tenant lookups, and v2 answers an empty list with + // the same message. The v1 order against the configuration set has not been probed. + static void requireBulkDestinationCount(int count) { + if (count < 1 || count > MAX_BULK_DESTINATIONS) { + throw new AwsException("InvalidParameterValue", + "You must specify at least 1 destination, and no more than " + + MAX_BULK_DESTINATIONS + " destinations", 400); + } + } + // SES refuses the send outright for an attachment type it does not accept, unlike the content // scan, which accepts the message and rejects it afterwards. private static void rejectBlockedAttachments(Message parsed) { @@ -1722,16 +1733,8 @@ public class SesService { throw new AwsException("InvalidParameterValue", "Source email is required.", 400); } requireInlineTemplateContent(template.subject(), template.textPart(), template.htmlPart()); - if (request.entries() == null || request.entries().isEmpty()) { - throw new AwsException("InvalidParameterValue", - "At least one destination entry is required.", 400); - } + requireBulkDestinationCount(sizeOf(request.entries())); configSetService.validateForSending(request.configurationSetName(), request.region()); - if (request.entries().size() > MAX_BULK_DESTINATIONS) { - throw new AwsException("MessageRejected", - "Number of destinations (" + request.entries().size() + ") exceeds the maximum of " - + MAX_BULK_DESTINATIONS + ".", 400); - } for (BulkEmailEntry entry : request.entries()) { int recipientCount = sizeOf(entry.toAddresses()) + sizeOf(entry.ccAddresses()) diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV1IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV1IntegrationTest.java index 74cd87ba5f..c6ace85829 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV1IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV1IntegrationTest.java @@ -215,26 +215,23 @@ class SesBulkV1IntegrationTest { } } + // AWS counts the destinations before it looks the template up (probed 2026-10-24). @Test @Order(8) - void sendBulkTemplatedEmail_destinationsExceeds50_returnsMessageRejected() { - RequestSpecification spec = given() + void sendBulkTemplatedEmail_destinationsExceed50_isCountedBeforeTheTemplateLookup() { + destinations(given() .contentType("application/x-www-form-urlencoded") .header("Authorization", AUTH) .formParam("Action", "SendBulkTemplatedEmail") .formParam("Source", "bulk@example.com") - .formParam("Template", "v1-bulk-welcome"); - for (int i = 1; i <= 51; i++) { - spec = spec.formParam( - "Destinations.member." + i + ".Destination.ToAddresses.member.1", - "user" + i + "@example.com"); - } - spec + .formParam("Template", "ghost"), 51) .when() .post("/") .then() .statusCode(400) - .body(containsString("MessageRejected")); + .body(containsString("InvalidParameterValue")) + .body(containsString( + "You must specify at least 1 destination, and no more than 50 destinations")); } @Test @@ -314,4 +311,31 @@ class SesBulkV1IntegrationTest { .statusCode(400) .body(containsString("MessageRejected")); } + + @Test + @Order(13) + void sendBulkTemplatedEmail_fiftyDestinations_sendsEachDestination() { + String body = destinations(given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH) + .formParam("Action", "SendBulkTemplatedEmail") + .formParam("Source", "bulk@example.com") + .formParam("Template", "v1-bulk-welcome") + .formParam("DefaultTemplateData", "{\"name\":\"X\",\"team\":\"floci\"}"), 50) + .when() + .post("/") + .then() + .statusCode(200) + .extract().body().asString(); + + assertEquals(50L, body.split("Success", -1).length - 1L); + } + + private static RequestSpecification destinations(RequestSpecification spec, int count) { + for (int i = 1; i <= count; i++) { + spec = spec.formParam("Destinations.member." + i + ".Destination.ToAddresses.member.1", + "user" + i + "@example.com"); + } + return spec; + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV2IntegrationTest.java index 015ad3d760..2b24de7d70 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesBulkV2IntegrationTest.java @@ -13,6 +13,7 @@ import java.util.stream.Stream; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; @@ -26,6 +27,9 @@ class SesBulkV2IntegrationTest { private static final String AUTH_HEADER = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request"; + private static final String DESTINATION_COUNT_MESSAGE = + "You must specify at least 1 destination, and no more than 50 destinations"; + @Test @Order(1) void createTemplate() { @@ -234,7 +238,8 @@ class SesBulkV2IntegrationTest { .post("/v2/email/outbound-bulk-emails") .then() .statusCode(400) - .body("__type", equalTo("BadRequestException")); + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo(DESTINATION_COUNT_MESSAGE)); } @Test @@ -261,13 +266,7 @@ class SesBulkV2IntegrationTest { @Test @Order(7) - void sendBulkEmail_entriesExceeds50_returnsMessageRejected() { - StringBuilder entries = new StringBuilder(); - for (int i = 1; i <= 51; i++) { - if (i > 1) entries.append(","); - entries.append("{\"Destination\":{\"ToAddresses\":[\"user") - .append(i).append("@example.com\"]}}"); - } + void sendBulkEmail_entriesExceeds50_returnsBadRequest() { given() .contentType("application/json") .header("Authorization", AUTH_HEADER) @@ -279,12 +278,13 @@ class SesBulkV2IntegrationTest { }, "BulkEmailEntries": [%s] } - """.formatted(entries)) + """.formatted(entries(51))) .when() .post("/v2/email/outbound-bulk-emails") .then() .statusCode(400) - .body("__type", equalTo("MessageRejected")); + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo(DESTINATION_COUNT_MESSAGE)); } @Test @@ -494,4 +494,148 @@ class SesBulkV2IntegrationTest { .statusCode(400) .body("__type", equalTo("MessageRejected")); } + + @Test + @Order(14) + void sendBulkEmail_fiftyEntries_sendsEachEntry() { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "bulk@example.com", + "DefaultContent": { + "Template": { + "TemplateName": "v2-bulk-welcome", + "TemplateData": "{\\"name\\":\\"X\\",\\"team\\":\\"floci\\"}" + } + }, + "BulkEmailEntries": [%s] + } + """.formatted(entries(50))) + .when() + .post("/v2/email/outbound-bulk-emails") + .then() + .statusCode(200) + .body("BulkEmailEntryResults", hasSize(50)) + .body("BulkEmailEntryResults.Status", everyItem(equalTo("SUCCESS"))); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("missingEntriesMembers") + @Order(15) + void sendBulkEmail_missingEntries_isAModelViolation(String label, String entriesMember) { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "bulk@example.com", + "DefaultContent": { + "Template": {"TemplateName": "v2-bulk-welcome"} + }%s + } + """.formatted(entriesMember)) + .when() + .post("/v2/email/outbound-bulk-emails") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("1 validation error detected: Value at 'bulkEmailEntries' " + + "failed to satisfy constraint: Member must not be null")); + } + + static Stream missingEntriesMembers() { + return Stream.of( + Arguments.of("absent", ""), + Arguments.of("null", ", \"BulkEmailEntries\": null")); + } + + // AWS counts the entries before it resolves the stored template, the configuration set and the + // tenant, each of which would otherwise answer 404 (probed 2026-10-24). + @ParameterizedTest(name = "{0}") + @MethodSource("countBeforeLookupBodies") + @Order(16) + void sendBulkEmail_entryCountIsCheckedBeforeLookups(String label, String body) { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(body) + .when() + .post("/v2/email/outbound-bulk-emails") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo(DESTINATION_COUNT_MESSAGE)); + } + + static Stream countBeforeLookupBodies() { + String inline = "\"DefaultContent\": {\"Template\": {\"TemplateContent\": {\"Subject\": \"s\", \"Text\": \"t\"}}}"; + return Stream.of( + Arguments.of("ghost stored template", """ + {"FromEmailAddress": "bulk@example.com", + "DefaultContent": {"Template": {"TemplateName": "ghost-template"}}, + "BulkEmailEntries": [%s]} + """.formatted(entries(51))), + Arguments.of("ghost stored template by ARN", """ + {"FromEmailAddress": "bulk@example.com", + "DefaultContent": {"Template": { + "TemplateArn": "arn:aws:ses:us-east-1:000000000000:template/ghost-template"}}, + "BulkEmailEntries": [%s]} + """.formatted(entries(51))), + Arguments.of("ghost configuration set", """ + {"FromEmailAddress": "bulk@example.com", "ConfigurationSetName": "ghost-cs", %s, + "BulkEmailEntries": [%s]} + """.formatted(inline, entries(51))), + Arguments.of("ghost tenant", """ + {"FromEmailAddress": "bulk@example.com", "TenantName": "ghost-tenant", %s, + "BulkEmailEntries": [%s]} + """.formatted(inline, entries(51))), + Arguments.of("empty entries with a ghost tenant", """ + {"FromEmailAddress": "bulk@example.com", "TenantName": "ghost-tenant", %s, + "BulkEmailEntries": []} + """.formatted(inline))); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("checkedBeforeCountBodies") + @Order(17) + void sendBulkEmail_earlierChecksWinOverEntryCount(String label, String body, String message) { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(body) + .when() + .post("/v2/email/outbound-bulk-emails") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo(message)); + } + + static Stream checkedBeforeCountBodies() { + return Stream.of( + Arguments.of("malformed From", """ + {"FromEmailAddress": "not an address", + "DefaultContent": {"Template": {"TemplateContent": {"Subject": "s", "Text": "t"}}}, + "BulkEmailEntries": [%s]} + """.formatted(entries(51)), "Invalid email address."), + Arguments.of("empty inline content", """ + {"FromEmailAddress": "bulk@example.com", + "DefaultContent": {"Template": {"TemplateContent": {}}}, + "BulkEmailEntries": [%s]} + """.formatted(entries(51)), "Template must have at least a subject, text, or html part.")); + } + + private static String entries(int count) { + StringBuilder entries = new StringBuilder(); + for (int i = 1; i <= count; i++) { + if (i > 1) { + entries.append(","); + } + entries.append("{\"Destination\":{\"ToAddresses\":[\"user") + .append(i).append("@example.com\"]}}"); + } + return entries.toString(); + } }