Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ses): make FromEmailAddress optional for v2 SendEmail (#1561)
Kenshin Okinaka01c7e1e62d00
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.javaindex 611320a14c..99a19683ac 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java@@ -270,11 +270,10 @@ public class SesController {JsonNode request = objectMapper.readTree(body);requireJsonObject(request);+ // FromEmailAddress is optional per the AWS v2 contract. Each content type below+ // enforces the sender requirement the way AWS does: Raw can take its From from the+ // MIME message, while Simple and Templated require FromEmailAddress.String fromEmailAddress = request.path("FromEmailAddress").asText(null);- if (fromEmailAddress == null || fromEmailAddress.isBlank()) {- throw new AwsException("BadRequestException",- "FromEmailAddress is required.", 400);- }JsonNode destination = requireObjectOrAbsent(request, "Destination");List<String> toAddresses = jsonArrayToList(destination.path("ToAddresses"));@@ -301,6 +300,10 @@ public class SesController {messageId = sesService.sendRawEmail(fromEmailAddress, allDestinations, rawData,configurationSetName, emailTags, region);} else if (content.has("Simple")) {+ if (fromEmailAddress == null || fromEmailAddress.isBlank()) {+ // AWS returns BadRequestException with a null message body here.+ throw new AwsException("BadRequestException", null, 400);+ }JsonNode simple = content.path("Simple");String subject = simple.path("Subject").path("Data").asText("");String bodyText = simple.path("Body").path("Text").path("Data").asText(null);@@ -310,6 +313,9 @@ public class SesController {bccAddresses, replyToAddresses, subject, bodyText, bodyHtml,configurationSetName, emailTags, additionalHeaders, region);} else if (content.has("Template")) {+ if (fromEmailAddress == null || fromEmailAddress.isBlank()) {+ throw new AwsException("BadRequestException", "Source cannot be empty", 400);+ }JsonNode template = content.path("Template");String templateName = template.path("TemplateName").asText(null);String templateArn = template.path("TemplateArn").asText(null);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.javaindex a032bc3ce3..6c5093eb6b 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@@ -254,6 +254,13 @@ public class SesService {String effectiveSource = sourceOmitted && headers != null && !headers.from().isBlank()? headers.from(): source;+ if (effectiveSource == null || effectiveSource.isBlank()) {+ // Shared by the v1 Query and v2 REST surfaces. Throw the v1-native code; the v2+ // controller's remapV1Exception translates InvalidParameterValue -> BadRequestException.+ // Verified against real AWS: v1 returns InvalidParameterValue and v2 BadRequestException,+ // both with this message.+ throw new AwsException("InvalidParameterValue", "Missing required header 'From'.", 400);+ }List<String> effectiveDestinations = hasExplicitDestinations? destinations: allRecipients(headers.to(), headers.cc(), headers.bcc());diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.javaindex d653dbfe7e..3cd4130ce4 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java@@ -220,6 +220,27 @@ class SesIntegrationTest {.body(containsString("<MessageId>"));}+ @Test+ @Order(30)+ void sendRawEmail_withoutSource_andNoMimeFrom_returnsInvalidParameterValue() {+ // Complements sendRawEmail_withoutSource_acceptedWhenMimeFromPresent: with neither a+ // Source parameter nor a MIME From header there is no sender, so the send is rejected.+ // Verified against real AWS: v1 returns InvalidParameterValue "Missing required header 'From'." (400).+ given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")+ .formParam("Action", "SendRawEmail")+ .formParam("Destinations.member.1", "[email protected]")+ .formParam("RawMessage.Data", "Subject: hello\r\n\r\nbody\r\n")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body(containsString("<Code>InvalidParameterValue</Code>"))+ // The apostrophes are XML-escaped on the Query (XML) wire, matching AWS.+ .body(containsString("Missing required header 'From'."));+ }+@Test@Order(12)void getSendQuota() {diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.javaindex 2830f08ce3..9c08351797 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java@@ -852,7 +852,73 @@ class SesV2IntegrationTest {.post("/v2/email/outbound-emails").then().statusCode(400)- .body("__type", equalTo("BadRequestException"));+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Missing required header 'From'."));+ }++ // FromEmailAddress is optional in the AWS v2 contract; the per-content-type behavior+ // below is verified against real AWS (Raw takes its From from the MIME message; Simple+ // returns a null message; Templated returns "Source cannot be empty").++ @Test+ @Order(67)+ void sendEmail_raw_noFromAddress_usesMimeFrom() {+ given()+ .contentType("application/json")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {+ "Destination": {"ToAddresses": ["[email protected]"]},+ "Content": {"Raw": {"Data": "From: [email protected]\\r\\nTo: [email protected]\\r\\nSubject: s\\r\\n\\r\\nbody"}}+ }+ """)+ .when()+ .post("/v2/email/outbound-emails")+ .then()+ .statusCode(200)+ .body("MessageId", notNullValue());+ }++ @Test+ @Order(68)+ void sendEmail_simple_missingFrom() {+ given()+ .contentType("application/json")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {+ "Destination": {"ToAddresses": ["[email protected]"]},+ "Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "hi"}}}}+ }+ """)+ .when()+ .post("/v2/email/outbound-emails")+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ // AWS returns the message key present with a JSON null value.+ .body("$", hasKey("message"))+ .body("message", nullValue());+ }++ @Test+ @Order(69)+ void sendEmail_template_missingFrom() {+ given()+ .contentType("application/json")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {+ "Destination": {"ToAddresses": ["[email protected]"]},+ "Content": {"Template": {"TemplateName": "any-tpl", "TemplateData": "{}"}}+ }+ """)+ .when()+ .post("/v2/email/outbound-emails")+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Source cannot be empty"));}// ──────────────── Inspection endpoint (/_aws/ses) ────────────────