mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): check v2 send request bodies against the service model (#5765)
* fix(ses): check v2 send request bodies against the service model SendEmail, SendBulkEmail and SendCustomVerificationEmail read their members by hand, so a member of the wrong JSON type was coerced, read as empty, or rejected with a Floci-specific BadRequestException, depending on which member it was. AWS deserializes the whole body against the model first and answers the first member of the wrong type, in body order, with a SerializationException and its deserializer message. tools/aws/regen_sesv2_shapes.py vendors the body shapes of botocore's SES v2 model into src/main/resources/aws/sesv2-request-shapes.json, kept in sync by make aws-data-check like the DynamoDB shapes. SesV2RequestShapes checks a body against them, and the three send endpoints call it before reading any member. The type checks it makes redundant are removed from the send helpers. A null address list element still passes and fails as "Invalid email address null.", as on AWS. Only the string, blob, list, structure and map messages have been probed, so number, boolean and timestamp members are left to their controllers. * fix(ses): report a null v2 send address where SES does A null element of a v2 send address list was rejected for the whole request while the body was read. SES lets it through deserialization and reports it later: SendEmail answers "Invalid email address null." after its content and sender checks and before the tenant lookup, and SendBulkEmail reports it as the INVALID_PARAMETER status of the entry it belongs to, of every entry for a null Reply-To, or as "Destination cannot be empty" when no entry of the request has a non-null recipient. The address lists now keep the null, SendEmail checks for it ahead of the tenant gate, and the bulk entry check reports it per entry. Two ordered integration tests that shared an @Order value get their own. * chore(core): share the vendored request-shape model DynamoDbRequestShapes and SesV2RequestShapes each carried the same Shape and Model records and the same parse() and load(), and load() repeated VendoredResources.read. RequestShapeModel in core.common now holds that half and both checks use it; each keeps its own walk and its protocol's messages.
This commit is contained in:
1 parent
0a5809a559
commit
e24a2882bc
22 files changed
+2853
-219
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RequestShapeModel.Shape;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RequestShapeModelTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void parseReadsOperationsAndEachShapeKind() throws Exception {
|
||||
RequestShapeModel model = RequestShapeModel.parse(MAPPER.readTree("""
|
||||
{
|
||||
"_source": {"generator": "test"},
|
||||
"operations": {"Send": "SendRequest"},
|
||||
"shapes": {
|
||||
"SendRequest": {"type": "structure", "members": {"To": "Addresses", "Tags": "TagMap", "From": "Address"}},
|
||||
"Addresses": {"type": "list", "member": "Address"},
|
||||
"TagMap": {"type": "map", "value": "Address"},
|
||||
"Address": {"type": "string"}
|
||||
}
|
||||
}
|
||||
"""));
|
||||
|
||||
assertEquals(Map.of("Send", "SendRequest"), model.operations());
|
||||
Shape request = model.shapes().get("SendRequest");
|
||||
assertEquals("structure", request.type());
|
||||
assertEquals(List.of("To", "Tags", "From"), List.copyOf(request.members().keySet()));
|
||||
assertEquals("Address", model.shapes().get("Addresses").element());
|
||||
assertEquals("Address", model.shapes().get("TagMap").element());
|
||||
Shape scalar = model.shapes().get("Address");
|
||||
assertEquals("string", scalar.type());
|
||||
assertTrue(scalar.members().isEmpty());
|
||||
assertNull(scalar.element());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadReadsAClasspathResource() {
|
||||
RequestShapeModel model = RequestShapeModel.load("fixtures/core/request-shapes.json", "Test request shapes");
|
||||
|
||||
assertEquals("SendRequest", model.operations().get("Send"));
|
||||
assertEquals("Address", model.shapes().get("Addresses").element());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsedModelCannotBeChanged() {
|
||||
RequestShapeModel model = RequestShapeModel.load("fixtures/core/request-shapes.json", "Test request shapes");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> model.operations().put("Other", "SendRequest"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> model.shapes().remove("Address"));
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> model.shapes().get("SendRequest").members().put("Cc", "Addresses"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadFailsForAMissingResource() {
|
||||
IllegalStateException error = assertThrows(IllegalStateException.class,
|
||||
() -> RequestShapeModel.load("aws/no-such-shapes.json", "Test request shapes"));
|
||||
|
||||
assertEquals("Test request shapes resource not found: aws/no-such-shapes.json", error.getMessage());
|
||||
}
|
||||
}
|
||||
+30
-9
@@ -332,6 +332,21 @@ class SesBulkV2IntegrationTest {
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("wrongTypeSendBulkEmailBodies")
|
||||
@Order(12)
|
||||
void sendBulkEmail_wrongMemberType_returnsSerializationException(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("SerializationException"));
|
||||
}
|
||||
|
||||
static Stream<Arguments> malformedSendBulkEmailBodies() {
|
||||
String validDefaultTemplate = "\"DefaultContent\": {\"Template\": {\"TemplateName\": \"v2-bulk-welcome\", \"TemplateData\": \"{\\\"team\\\":\\\"floci\\\",\\\"name\\\":\\\"X\\\"}\"}}";
|
||||
return Stream.of(
|
||||
@@ -345,6 +360,21 @@ class SesBulkV2IntegrationTest {
|
||||
"BulkEmailEntries": [null]
|
||||
}
|
||||
""".formatted(validDefaultTemplate)),
|
||||
Arguments.of("DefaultTemplateData as invalid JSON string", """
|
||||
{
|
||||
"FromEmailAddress": "bulk@example.com",
|
||||
"DefaultContent": {
|
||||
"Template": {"TemplateName": "v2-bulk-welcome", "TemplateData": "{not json"}
|
||||
},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["alice@example.com"]}}]
|
||||
}
|
||||
""")
|
||||
);
|
||||
}
|
||||
|
||||
static Stream<Arguments> wrongTypeSendBulkEmailBodies() {
|
||||
String validDefaultTemplate = "\"DefaultContent\": {\"Template\": {\"TemplateName\": \"v2-bulk-welcome\", \"TemplateData\": \"{\\\"team\\\":\\\"floci\\\",\\\"name\\\":\\\"X\\\"}\"}}";
|
||||
return Stream.of(
|
||||
Arguments.of("BulkEmailEntries element is string", """
|
||||
{
|
||||
"FromEmailAddress": "bulk@example.com",
|
||||
@@ -368,15 +398,6 @@ class SesBulkV2IntegrationTest {
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["alice@example.com"]}}]
|
||||
}
|
||||
"""),
|
||||
Arguments.of("DefaultTemplateData as invalid JSON string", """
|
||||
{
|
||||
"FromEmailAddress": "bulk@example.com",
|
||||
"DefaultContent": {
|
||||
"Template": {"TemplateName": "v2-bulk-welcome", "TemplateData": "{not json"}
|
||||
},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["alice@example.com"]}}]
|
||||
}
|
||||
"""),
|
||||
Arguments.of("per-entry ReplacementTemplateData as object", """
|
||||
{
|
||||
"FromEmailAddress": "bulk@example.com",
|
||||
|
||||
+3
-2
@@ -109,7 +109,7 @@ class SesListManagementOptionsV2IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void listManagementOptionsWithNonStringTopicName_returns400() {
|
||||
void listManagementOptionsWithNonStringTopicName_returnsSerializationException() {
|
||||
given().contentType("application/json").header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
@@ -120,7 +120,8 @@ class SesListManagementOptionsV2IntegrationTest {
|
||||
}
|
||||
""".formatted(FROM, LIST))
|
||||
.when().post("/v2/email/outbound-emails").then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("NUMBER_VALUE can not be converted to a String"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class SesSendAddressListTypeIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
private static final String TO = "success@simulator.amazonses.com";
|
||||
|
||||
private static void assertSerializationException(String path, String body) {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body(body)
|
||||
.when()
|
||||
.post(path)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
}
|
||||
|
||||
private static String bulkSend(String replyTo, String entryDestination) {
|
||||
return """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
%s
|
||||
"DefaultContent": {"Template": {
|
||||
"TemplateContent": {"Subject": "s", "Text": "b"},
|
||||
"TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": %s}]
|
||||
}
|
||||
""".formatted(replyTo, entryDestination);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_nonArrayReplyTo_returnsSerializationException() {
|
||||
assertSerializationException("/v2/email/outbound-emails", """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["%s"]},
|
||||
"ReplyToAddresses": "reply@example.com",
|
||||
"Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}}
|
||||
}
|
||||
""".formatted(TO));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_nonArrayReplyTo_returnsSerializationException() {
|
||||
assertSerializationException("/v2/email/outbound-bulk-emails",
|
||||
bulkSend("\"ReplyToAddresses\": \"reply@example.com\",", "{\"ToAddresses\": [\"" + TO + "\"]}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_nonArrayEntryDestinationMember_returnsSerializationException() {
|
||||
assertSerializationException("/v2/email/outbound-bulk-emails",
|
||||
bulkSend("", "{\"ToAddresses\": [\"" + TO + "\"], \"CcAddresses\": \"bad-shape\"}"));
|
||||
}
|
||||
}
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* A v2 send member of the wrong JSON type fails deserialization on AWS with a
|
||||
* SerializationException naming the type, ahead of every other check (probe-confirmed). One case
|
||||
* per member group shows the send endpoints check their body against the SES v2 shapes; the
|
||||
* messages per JSON type are covered by SesV2RequestShapesTest.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesSendMemberTypeIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
private static final String TO = "success@simulator.amazonses.com";
|
||||
private static final String SEND = "/v2/email/outbound-emails";
|
||||
private static final String BULK = "/v2/email/outbound-bulk-emails";
|
||||
private static final String VERIFY = "/v2/email/outbound-custom-verification-emails";
|
||||
private static final String SIMPLE =
|
||||
"{\"Simple\": {\"Subject\": {\"Data\": \"s\"}, \"Body\": {\"Text\": {\"Data\": \"b\"}}}}";
|
||||
private static final String INLINE_TEMPLATE =
|
||||
"{\"Template\": {\"TemplateContent\": {\"Subject\": \"s\", \"Text\": \"b\"}, \"TemplateData\": \"{}\"}}";
|
||||
private static final String STRUCTURE = "Start of structure or map found where not expected.";
|
||||
private static final String LIST = "Start of list found where not expected";
|
||||
private static final String NUMBER = "NUMBER_VALUE can not be converted to a String";
|
||||
private static final String EXPECTED_LIST = "Expected list or null";
|
||||
|
||||
private static void assertError(String path, String body, String type, String message) {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body(body)
|
||||
.when()
|
||||
.post(path)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo(type))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
private static void assertSerializationException(String path, String body, String message) {
|
||||
assertError(path, body, "SerializationException", message);
|
||||
}
|
||||
|
||||
private static String send(String members, String content) {
|
||||
return "{" + members + "\"Content\": " + content + "}";
|
||||
}
|
||||
|
||||
private static String bulkSend(String members, String entryDestination) {
|
||||
return "{" + members + "\"DefaultContent\": " + INLINE_TEMPLATE
|
||||
+ ", \"BulkEmailEntries\": [{\"Destination\": " + entryDestination + "}]}";
|
||||
}
|
||||
|
||||
private static String rawData() {
|
||||
return Base64.getEncoder().encodeToString(("From: sender@example.com\r\nTo: " + TO
|
||||
+ "\r\nSubject: s\r\n\r\nb").getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_objectFromEmailAddress_isNotReadAsAbsent() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": {},",
|
||||
"{\"Raw\": {\"Data\": \"" + rawData() + "\"}}"), STRUCTURE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_numericConfigurationSetName_returnsSerializationException() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, \"ConfigurationSetName\": 123,", SIMPLE),
|
||||
NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_numericRawData_namesTheBlobType() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\",",
|
||||
"{\"Raw\": {\"Data\": 123}}"), "NUMBER_VALUE can not be converted to a Blob");
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_simpleContentTypeErrorPrecedesMissingSender() {
|
||||
assertSerializationException(SEND, send("\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]},",
|
||||
"{\"Simple\": {\"Subject\": {\"Data\": 123}, \"Body\": {\"Text\": {\"Data\": \"b\"}}}}"), NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_stringSubject_returnsExpectedNull() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]},",
|
||||
"{\"Simple\": {\"Subject\": \"s\", \"Body\": {\"Text\": {\"Data\": \"b\"}}}}"), "Expected null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_stringHeaders_returnsExpectedList() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]},",
|
||||
"{\"Simple\": {\"Subject\": {\"Data\": \"s\"}, \"Body\": {\"Text\": {\"Data\": \"b\"}}, "
|
||||
+ "\"Headers\": \"X-A: b\"}}"), EXPECTED_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_templateNameTypeErrorPrecedesMissingSender() {
|
||||
assertSerializationException(SEND, send("\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]},",
|
||||
"{\"Template\": {\"TemplateName\": 123, \"TemplateData\": \"{}\"}}"), NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_stringDestination_returnsExpectedNull() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": \"" + TO + "\",", SIMPLE), "Expected null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_arrayDestination_returnsStartOfList() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": [\"" + TO + "\"],", SIMPLE), LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_nonArrayReplyTo_returnsExpectedList() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, "
|
||||
+ "\"ReplyToAddresses\": \"reply@example.com\",", SIMPLE), EXPECTED_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_nullReplyToElement_isAnInvalidAddress() {
|
||||
assertError(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, \"ReplyToAddresses\": [null],", SIMPLE),
|
||||
"BadRequestException", "Invalid email address null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_nullAddressElementIsReportedAfterContentTypeErrors() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, \"ReplyToAddresses\": [null],",
|
||||
"{\"Simple\": {\"Subject\": {\"Data\": 123}, \"Body\": {\"Text\": {\"Data\": \"b\"}}}}"), NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_contentTypeErrorPrecedesEmailTagValidation() {
|
||||
assertSerializationException(SEND, send("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"EmailTags\": [{\"Name\": \"\", \"Value\": \"v\"}],",
|
||||
"{\"Raw\": {\"Data\": 123}}"), "NUMBER_VALUE can not be converted to a Blob");
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_replyToTypeErrorPrecedesMissingSender() {
|
||||
assertSerializationException(BULK, bulkSend("\"ReplyToAddresses\": \"reply@example.com\",",
|
||||
"{\"ToAddresses\": [\"" + TO + "\"]}"), EXPECTED_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_entryDestinationTypeErrorPrecedesMalformedSender() {
|
||||
assertSerializationException(BULK, bulkSend("\"FromEmailAddress\": \"sender@\",",
|
||||
"{\"ToAddresses\": [\"" + TO + "\"], \"CcAddresses\": \"bad-shape\"}"), EXPECTED_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_arrayEntryDestination_returnsStartOfList() {
|
||||
assertSerializationException(BULK, bulkSend("\"FromEmailAddress\": \"sender@example.com\",",
|
||||
"[\"" + TO + "\"]"), LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_numericFromEmailAddress_returnsSerializationException() {
|
||||
assertSerializationException(BULK, bulkSend("\"FromEmailAddress\": 123,",
|
||||
"{\"ToAddresses\": [\"" + TO + "\"]}"), NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_nullReplyToElementIsReportedAfterEntryTypeErrors() {
|
||||
assertSerializationException(BULK, bulkSend("\"FromEmailAddress\": \"sender@example.com\", "
|
||||
+ "\"ReplyToAddresses\": [null],", "{\"ToAddresses\": [1]}"), NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_entryReplacementTypeErrorPrecedesMissingStoredTemplate() {
|
||||
assertSerializationException(BULK, """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"DefaultContent": {"Template": {"TemplateName": "no-such-template", "TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["%s"]}, "ReplacementTags": "bad"}]
|
||||
}
|
||||
""".formatted(TO), EXPECTED_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_entryTypeErrorPrecedesMissingStoredTemplate() {
|
||||
assertSerializationException(BULK, """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"DefaultContent": {"Template": {"TemplateName": "no-such-template", "TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": [1]}}]
|
||||
}
|
||||
""", NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendCustomVerificationEmail_numericEmailAddress_returnsSerializationException() {
|
||||
assertSerializationException(VERIFY, "{\"EmailAddress\": 123, \"TemplateName\": \"t\"}", NUMBER);
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
/**
|
||||
* A null address element passes deserialization on AWS and fails later (probe-confirmed): on
|
||||
* SendEmail after the content and sender checks and before the tenant lookup, on SendBulkEmail as
|
||||
* the status of the entries it affects.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesSendNullAddressIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
private static final String SENDER = "sender@example.com";
|
||||
private static final String TO = "success@simulator.amazonses.com";
|
||||
private static final String SIMPLE =
|
||||
"{\"Simple\": {\"Subject\": {\"Data\": \"s\"}, \"Body\": {\"Text\": {\"Data\": \"b\"}}}}";
|
||||
private static final String INLINE_TEMPLATE =
|
||||
"{\"Template\": {\"TemplateContent\": {\"Subject\": \"s\", \"Text\": \"b\"}, \"TemplateData\": \"{}\"}}";
|
||||
private static final String NULL_ADDRESS = "Invalid email address null.";
|
||||
private static final String EMPTY_DESTINATION = "Destination cannot be empty";
|
||||
|
||||
private static ValidatableResponse post(String path, String body) {
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body(body)
|
||||
.when()
|
||||
.post(path)
|
||||
.then();
|
||||
}
|
||||
|
||||
private static ValidatableResponse send(String body) {
|
||||
return post("/v2/email/outbound-emails", body);
|
||||
}
|
||||
|
||||
private static ValidatableResponse bulk(String members, String entries) {
|
||||
return post("/v2/email/outbound-bulk-emails", "{\"FromEmailAddress\": \"" + SENDER + "\", " + members
|
||||
+ "\"DefaultContent\": " + INLINE_TEMPLATE + ", \"BulkEmailEntries\": [" + entries + "]}");
|
||||
}
|
||||
|
||||
private static String entry(String destination) {
|
||||
return "{\"Destination\": " + destination + "}";
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_nullToAddress_isAnInvalidAddress() {
|
||||
send("{\"FromEmailAddress\": \"" + SENDER + "\", \"Destination\": {\"ToAddresses\": [null]}, "
|
||||
+ "\"Content\": " + SIMPLE + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_missingContentPrecedesNullAddress() {
|
||||
send("{\"FromEmailAddress\": \"" + SENDER + "\", \"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, "
|
||||
+ "\"ReplyToAddresses\": [null]}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Content must contain Raw, Simple, or Template."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_missingSenderPrecedesNullAddress() {
|
||||
send("{\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, \"ReplyToAddresses\": [null], "
|
||||
+ "\"Content\": " + SIMPLE + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_nullAddressPrecedesTenantLookup() {
|
||||
send("{\"FromEmailAddress\": \"" + SENDER + "\", \"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}, "
|
||||
+ "\"ReplyToAddresses\": [null], \"TenantName\": \"no-such-tenant\", \"Content\": " + SIMPLE + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_onlyNullRecipientsInTheRequest_answerEmptyDestination() {
|
||||
bulk("", entry("{\"ToAddresses\": [null]}") + ", " + entry("{\"ToAddresses\": [null]}"))
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[0].Error", equalTo(EMPTY_DESTINATION))
|
||||
.body("BulkEmailEntryResults[1].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[1].Error", equalTo(EMPTY_DESTINATION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_nullRecipientBesideAnotherEntrysRecipient_isAnInvalidAddress() {
|
||||
bulk("", entry("{\"ToAddresses\": [null]}") + ", " + entry("{\"ToAddresses\": [\"" + TO + "\"]}"))
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[0].Error", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_nullRecipientBesideARecipient_isAnInvalidAddress() {
|
||||
bulk("", entry("{\"ToAddresses\": [null, \"" + TO + "\"]}"))
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[0].Error", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_nullCcAddress_isAnInvalidAddress() {
|
||||
bulk("", entry("{\"ToAddresses\": [\"" + TO + "\"], \"CcAddresses\": [null]}"))
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[0].Error", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_nullReplyToAddress_failsEveryEntry() {
|
||||
bulk("\"ReplyToAddresses\": [null], ", entry("{\"ToAddresses\": [\"" + TO + "\"]}") + ", "
|
||||
+ entry("{\"ToAddresses\": [\"" + TO + "\"]}"))
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[0].Error", equalTo(NULL_ADDRESS))
|
||||
.body("BulkEmailEntryResults[1].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[1].Error", equalTo(NULL_ADDRESS));
|
||||
}
|
||||
}
|
||||
+29
-9
@@ -684,11 +684,40 @@ class SesTemplateV2IntegrationTest {
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("wrongTypeSendEmailBodies")
|
||||
@Order(51)
|
||||
void sendEmail_wrongMemberType_returnsSerializationException(String label, String body) {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
}
|
||||
|
||||
static Stream<Arguments> malformedSendEmailBodies() {
|
||||
return Stream.of(
|
||||
Arguments.of("body is array", "[1,2,3]"),
|
||||
Arguments.of("body is JSON null literal", "null"),
|
||||
Arguments.of("body is JSON string", "\"hello\""),
|
||||
Arguments.of("TemplateData as invalid JSON string", """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {"TemplateName": "any", "TemplateData": "{not json"}
|
||||
}
|
||||
}
|
||||
""")
|
||||
);
|
||||
}
|
||||
|
||||
static Stream<Arguments> wrongTypeSendEmailBodies() {
|
||||
return Stream.of(
|
||||
Arguments.of("Destination as string", """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
@@ -713,15 +742,6 @@ class SesTemplateV2IntegrationTest {
|
||||
"Template": {"TemplateName": "any", "TemplateData": [1,2,3]}
|
||||
}
|
||||
}
|
||||
"""),
|
||||
Arguments.of("TemplateData as invalid JSON string", """
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {"TemplateName": "any", "TemplateData": "{not json"}
|
||||
}
|
||||
}
|
||||
""")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class SesV2RequestShapesTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String STRUCTURE = "Start of structure or map found where not expected.";
|
||||
private static final String LIST = "Start of list found where not expected";
|
||||
|
||||
@Test
|
||||
void stringMemberNamesTheJsonTokenItGot() {
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":123}", "NUMBER_VALUE can not be converted to a String");
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":true}", "TRUE_VALUE can not be converted to a String");
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":false}", "FALSE_VALUE can not be converted to a String");
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":{}}", STRUCTURE);
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":[]}", LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blobMemberNamesTheBlobType() {
|
||||
assertRejected("SendEmail", "{\"Content\":{\"Raw\":{\"Data\":123}}}", "NUMBER_VALUE can not be converted to a Blob");
|
||||
assertRejected("SendEmail", "{\"Content\":{\"Raw\":{\"Data\":true}}}", "TRUE_VALUE can not be converted to a Blob");
|
||||
assertRejected("SendEmail", "{\"Content\":{\"Raw\":{\"Data\":{}}}}", STRUCTURE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void structureMemberTakesOnlyAnObject() {
|
||||
assertRejected("SendEmail", "{\"Destination\":\"to@example.com\"}", "Expected null");
|
||||
assertRejected("SendEmail", "{\"Destination\":123}", "Expected null");
|
||||
assertRejected("SendEmail", "{\"Destination\":[]}", LIST);
|
||||
assertRejected("SendEmail", "{\"Content\":{\"Simple\":{\"Subject\":\"s\"}}}", "Expected null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void listMemberTakesOnlyAnArray() {
|
||||
assertRejected("SendEmail", "{\"ReplyToAddresses\":\"r@example.com\"}", "Expected list or null");
|
||||
assertRejected("SendEmail", "{\"ReplyToAddresses\":123}", "Expected list or null");
|
||||
assertRejected("SendEmail", "{\"ReplyToAddresses\":{}}", STRUCTURE);
|
||||
assertRejected("SendEmail", "{\"ReplyToAddresses\":[123]}", "NUMBER_VALUE can not be converted to a String");
|
||||
assertRejected("SendBulkEmail", "{\"BulkEmailEntries\":[{\"Destination\":[]}]}", LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapMemberReadsLikeAStructure() {
|
||||
assertAccepted("ListTenantResources", "{\"Filter\":{\"RESOURCE_TYPE\":\"x\"}}");
|
||||
assertRejected("ListTenantResources", "{\"Filter\":[]}", LIST);
|
||||
assertRejected("ListTenantResources", "{\"Filter\":\"x\"}", "Expected null");
|
||||
assertRejected("ListTenantResources", "{\"Filter\":{\"RESOURCE_TYPE\":1}}",
|
||||
"NUMBER_VALUE can not be converted to a String");
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstBadMemberInBodyOrderIsReported() {
|
||||
assertRejected("SendEmail", "{\"Destination\":[],\"FromEmailAddress\":123}", LIST);
|
||||
assertRejected("SendEmail", "{\"FromEmailAddress\":123,\"Destination\":[]}",
|
||||
"NUMBER_VALUE can not be converted to a String");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullsUnknownMembersAndUnprobedTypesAreLeftToTheOperation() {
|
||||
assertAccepted("SendEmail", "{\"FromEmailAddress\":null,\"Destination\":null,\"Unknown\":5}");
|
||||
assertAccepted("SendEmail", "{\"ReplyToAddresses\":[null]}");
|
||||
assertAccepted("PutAccountSendingAttributes", "{\"SendingEnabled\":\"maybe\"}");
|
||||
assertAccepted("PutConfigurationSetDeliveryOptions", "{\"MaxDeliverySeconds\":\"x\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownOperationIsAServerError() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> SesV2RequestShapes.check("NoSuchOperation", json("{}")));
|
||||
assertEquals("InternalFailure", error.getErrorCode());
|
||||
assertEquals(500, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void bodyShapesLeaveOutUriAndQueryMembers() {
|
||||
assertAccepted("GetEmailIdentity", "{\"EmailIdentity\":123}");
|
||||
}
|
||||
|
||||
private static void assertRejected(String operation, String body, String message) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> SesV2RequestShapes.check(operation, json(body)));
|
||||
assertEquals("SerializationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(message, error.getMessage());
|
||||
}
|
||||
|
||||
private static void assertAccepted(String operation, String body) {
|
||||
assertDoesNotThrow(() -> SesV2RequestShapes.check(operation, json(body)));
|
||||
}
|
||||
|
||||
private static JsonNode json(String body) throws Exception {
|
||||
return MAPPER.readTree(body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user