mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): reject malformed send addresses with AWS's messages (#5380)
* fix(ses): reject malformed send addresses with AWS's messages Sends accepted any address string. AWS checks every address of a send with JavaMail's strict InternetAddress rules and answers with its messages (probe-confirmed in us-west-2): - no '@': Missing final '@domain' - empty domain: Missing domain - control, whitespace or non-ASCII in the local part: Local address contains control or whitespace - the same in the domain: Domain contains control or whitespace v1 returns InvalidParameterValue, v2 BadRequestException. Display names, comments, encoded words, Punycode and dotless domains pass. The check runs after the missing-sender check and before the destination, configuration set, length and template checks, over the sender, To, Cc, Bcc, return path and Reply-To, and on raw sends over the Source and Destinations parameters and the From and To headers. A bulk send reports a malformed or over-long sender as "Invalid email address<addr>." ahead of a missing template, and a malformed bulk entry recipient fails only that entry. The field walk shared with the 320-character check moves to SesSendAddresses so both checks visit the same fields in one order. * fix(ses): check bulk reply-to and return path, quoted CR/LF and quoted '<' Review follow-ups, probe-confirmed in us-east-1 with a verified sender: - A malformed bulk Reply-To fails every entry with the JavaMail message, while a malformed bulk return path (v1 ReturnPath, v2 FeedbackForwardingEmailAddress) rejects the whole request as "Invalid email address<addr>.", like the sender. - A CR or LF inside a quoted local part is rejected, even when followed by whitespace, as "Invalid email address <addr>.". A '<' inside a quoted local part is no longer taken for the start of the angle-bracketed address, in both the syntax and the length check. The syntax tests now also assert that a rejected send is not recorded.
This commit is contained in:
1 parent
517409a7aa
commit
6d76af0834
12 files changed
+1078
-148
No files matched your search
@@ -5,17 +5,17 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The normalization rules of {@link SesAddressLength#require(String)} and the header splitting of
|
||||
* {@link SesAddressLength#splitAddressList(String)}. The ASCII, non-ASCII, RFC 822 special,
|
||||
* quoted-name, UTF-16, encoded-word and escaped-quote cases are probe-confirmed; the remaining edge
|
||||
* cases pin Floci's own handling of inputs that were not probed.
|
||||
* The normalization rules of {@link SesAddressLength#require(String)}. The ASCII, non-ASCII, RFC 822
|
||||
* special, quoted-name, UTF-16, encoded-word and escaped-quote cases are probe-confirmed; the
|
||||
* remaining edge cases pin Floci's own handling of inputs that were not probed.
|
||||
*/
|
||||
class SesAddressLengthTest {
|
||||
|
||||
@@ -29,6 +29,15 @@ class SesAddressLengthTest {
|
||||
assertDoesNotThrow(() -> SesAddressLength.require(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exceedsLimitUsesTheSameNormalization() {
|
||||
assertFalse(SesAddressLength.exceedsLimit(null));
|
||||
assertFalse(SesAddressLength.exceedsLimit(address(320)));
|
||||
assertTrue(SesAddressLength.exceedsLimit(address(321)));
|
||||
String name = "N".repeat(320 - SUFFIX.length());
|
||||
assertFalse(SesAddressLength.exceedsLimit("\"" + name + "\"" + SUFFIX));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bareAddressLimitIsInclusive() {
|
||||
assertDoesNotThrow(() -> SesAddressLength.require(address(320)));
|
||||
@@ -136,36 +145,6 @@ class SesAddressLengthTest {
|
||||
assertRejects(name + SUFFIX, "\"" + name + "\"" + SUFFIX);
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListSeparatesAddressesAtTopLevelCommas() {
|
||||
assertEquals(List.of("a@b.com", " \"Doe, John\" <c@d.com>", " \"x \\\"y, z\\\"\" <e@f.com>"),
|
||||
SesAddressLength.splitAddressList("a@b.com, \"Doe, John\" <c@d.com>, \"x \\\"y, z\\\"\" <e@f.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListKeepsCommasInsideAngleBrackets() {
|
||||
assertEquals(List.of("Name <a,b@c.com>"), SesAddressLength.splitAddressList("Name <a,b@c.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListIgnoresQuotesAndCommasInsideComments() {
|
||||
assertEquals(List.of("a@b.com (it\"s, (nested)) ", " c@d.com"),
|
||||
SesAddressLength.splitAddressList("a@b.com (it\"s, (nested)) , c@d.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListKeepsParenthesesInsideQuotedNames() {
|
||||
assertEquals(List.of("\"a (b\" <x@y.com>", " z@y.com"),
|
||||
SesAddressLength.splitAddressList("\"a (b\" <x@y.com>, z@y.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListDropsGroupSyntax() {
|
||||
assertEquals(List.of(" a@b.com", " c@d.com"),
|
||||
SesAddressLength.splitAddressList("team: a@b.com, c@d.com;"));
|
||||
assertEquals(List.of(), SesAddressLength.splitAddressList("undisclosed-recipients:;"));
|
||||
}
|
||||
|
||||
private static void assertRejects(String input, String reported) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> SesAddressLength.require(input));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
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.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* The address forms of {@link SesAddressSyntax#violation(String)}. The four messages and the
|
||||
* accepted Punycode, dotless, display-name, encoded-word and comment forms are probe-confirmed;
|
||||
* the quoted local part follows the probe-confirmed CVET handling, and the scan order and the
|
||||
* first-{@code @} split follow JavaMail's {@code InternetAddress} check, whose messages AWS returns.
|
||||
*/
|
||||
class SesAddressSyntaxTest {
|
||||
|
||||
private static final String MISSING_FINAL_DOMAIN = "Missing final '@domain'";
|
||||
private static final String MISSING_DOMAIN = "Missing domain";
|
||||
private static final String LOCAL_CONTROL = "Local address contains control or whitespace";
|
||||
private static final String DOMAIN_CONTROL = "Domain contains control or whitespace";
|
||||
|
||||
@Test
|
||||
void nullAndBlankAreLeftToTheRequiredFieldCheck() {
|
||||
assertNull(SesAddressSyntax.violation(null));
|
||||
assertNull(SesAddressSyntax.violation(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addressWithoutAtIsMissingFinalDomain() {
|
||||
assertEquals(MISSING_FINAL_DOMAIN, SesAddressSyntax.violation("sender.example.com"));
|
||||
assertEquals(MISSING_FINAL_DOMAIN, SesAddressSyntax.violation("Alice <sender.example.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addressWithEmptyDomainIsMissingDomain() {
|
||||
assertEquals(MISSING_DOMAIN, SesAddressSyntax.violation("sender@"));
|
||||
assertEquals(MISSING_DOMAIN, SesAddressSyntax.violation("Alice <sender@>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonAsciiLocalPartIsRejected() {
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("やまだ@example.com"));
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("Alice <やまだ@example.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whitespaceInLocalPartIsRejected() {
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("a b@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonAsciiDomainIsRejected() {
|
||||
assertEquals(DOMAIN_CONTROL, SesAddressSyntax.violation("sender@例え.jp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localPartIsReportedBeforeDomain() {
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("やまだ@例え.jp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void punycodeAndDotlessDomainsPass() {
|
||||
assertNull(SesAddressSyntax.violation("sender@xn--r8jz45g.jp"));
|
||||
assertNull(SesAddressSyntax.violation("sender@b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonAsciiDisplayNamesPass() {
|
||||
assertNull(SesAddressSyntax.violation("山田 <sender@example.com>"));
|
||||
assertNull(SesAddressSyntax.violation("\"山田\" <sender@example.com>"));
|
||||
assertNull(SesAddressSyntax.violation("=?UTF-8?B?5bGx55Sw?= <sender@example.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonAsciiCommentPasses() {
|
||||
assertNull(SesAddressSyntax.violation("sender@example.com (山田)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localPartIsScannedBeforeTheMissingAtIsReported() {
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("やまだ"));
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("a b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstUnquotedAtSeparatesTheDomain() {
|
||||
assertEquals(DOMAIN_CONTROL, SesAddressSyntax.violation("a@例え@example.com"));
|
||||
assertNull(SesAddressSyntax.violation("a@b@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void commentAfterTheAngleBracketsIsIgnored() {
|
||||
assertNull(SesAddressSyntax.violation("Alice <sender@example.com> (work)"));
|
||||
assertEquals(LOCAL_CONTROL, SesAddressSyntax.violation("Alice <やまだ@example.com> (work)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void atInsideAQuotedLocalPartIsNotTheSeparator() {
|
||||
assertNull(SesAddressSyntax.violation("\"a@b\"@example.com"));
|
||||
assertNull(SesAddressSyntax.violation("\"a b\"@example.com"));
|
||||
assertEquals(MISSING_FINAL_DOMAIN, SesAddressSyntax.violation("\"a@b\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void newlineInsideAQuotedLocalPartIsAnInvalidAddress() {
|
||||
for (String local : new String[] {"\"a\nb\"", "\"a\r\nb\"", "\"a\rb\"", "\"a\r\n b\""}) {
|
||||
String address = local + "@example.com";
|
||||
assertEquals("Invalid email address " + address + ".", SesAddressSyntax.violation(address));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void lessThanInsideAQuotedLocalPartIsNotTheAngleBracket() {
|
||||
assertNull(SesAddressSyntax.violation("Display <\"a<b\"@example.com>"));
|
||||
assertEquals(DOMAIN_CONTROL, SesAddressSyntax.violation("Display <\"a<b\"@例え.jp>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireThrowsInvalidParameterValue() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> SesAddressSyntax.require("sender@"));
|
||||
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
assertEquals(MISSING_DOMAIN, e.getMessage());
|
||||
assertDoesNotThrow(() -> SesAddressSyntax.require("sender@example.com"));
|
||||
}
|
||||
}
|
||||
+40
@@ -117,4 +117,44 @@ class SesSendAddressLengthIntegrationTest {
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(EXPECTED_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_fromOverLimit_returnsInvalidEmailAddress() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "%s",
|
||||
"DefaultContent": {"Template": {
|
||||
"TemplateContent": {"Subject": "s", "Text": "b"},
|
||||
"TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["success@simulator.amazonses.com"]}}]
|
||||
}
|
||||
""".formatted(TOO_LONG))
|
||||
.when()
|
||||
.post("/v2/email/outbound-bulk-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid email address<" + TOO_LONG + ">."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1SendBulkTemplatedEmail_sourceOverLimit_returnsInvalidEmailAddress() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendBulkTemplatedEmail")
|
||||
.formParam("Source", TOO_LONG)
|
||||
.formParam("Template", "address-length-missing")
|
||||
.formParam("DefaultTemplateData", "{}")
|
||||
.formParam("Destinations.member.1.Destination.ToAddresses.member.1", "success@simulator.amazonses.com")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Invalid email address<" + TOO_LONG + ">."));
|
||||
}
|
||||
}
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
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;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class SesSendAddressSyntaxIntegrationTest {
|
||||
|
||||
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";
|
||||
|
||||
@Test
|
||||
void v1SendEmail_sourceWithoutAt_returnsInvalidParameterValue() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendEmail")
|
||||
.formParam("Source", "sender.example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", TO)
|
||||
.formParam("Message.Subject.Data", "Subject")
|
||||
.formParam("Message.Body.Text.Data", "Body")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Missing final '@domain'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1SendRawEmail_nonAsciiDomainDestination_returnsInvalidParameterValue() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendRawEmail")
|
||||
.formParam("Source", "sender@example.com")
|
||||
.formParam("Destinations.member.1", "success@例え.jp")
|
||||
.formParam("RawMessage.Data", Base64.getEncoder().encodeToString(
|
||||
"From: sender@example.com\r\nSubject: s\r\n\r\nb".getBytes(StandardCharsets.UTF_8)))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Domain contains control or whitespace"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_nonAsciiLocalRecipient_returnsBadRequestException() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["やまだ@simulator.amazonses.com"]},
|
||||
"Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Local address contains control or whitespace"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_fromWithEmptyDomain_returnsInvalidEmailAddress() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@",
|
||||
"DefaultContent": {"Template": {
|
||||
"TemplateContent": {"Subject": "s", "Text": "b"},
|
||||
"TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["%s"]}}]
|
||||
}
|
||||
""".formatted(TO))
|
||||
.when()
|
||||
.post("/v2/email/outbound-bulk-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid email address<sender@>."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_malformedEntryRecipient_failsOnlyThatEntry() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"DefaultContent": {"Template": {
|
||||
"TemplateContent": {"Subject": "s", "Text": "b"},
|
||||
"TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [
|
||||
{"Destination": {"ToAddresses": ["%1$s"]}},
|
||||
{"Destination": {"ToAddresses": ["success.simulator.amazonses.com"]}},
|
||||
{"Destination": {"ToAddresses": ["%1$s"], "CcAddresses": ["やまだ@example.com"]}}
|
||||
]
|
||||
}
|
||||
""".formatted(TO))
|
||||
.when()
|
||||
.post("/v2/email/outbound-bulk-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("BulkEmailEntryResults[0].Status", equalTo("SUCCESS"))
|
||||
.body("BulkEmailEntryResults[1].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[1].Error", equalTo("Missing final '@domain'"))
|
||||
.body("BulkEmailEntryResults[1].MessageId", nullValue())
|
||||
.body("BulkEmailEntryResults[2].Status", equalTo("INVALID_PARAMETER"))
|
||||
.body("BulkEmailEntryResults[2].Error", equalTo("Local address contains control or whitespace"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1SendBulkTemplatedEmail_malformedEntryRecipient_failsOnlyThatEntry() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", "address-syntax-bulk")
|
||||
.formParam("Template.SubjectPart", "s")
|
||||
.formParam("Template.TextPart", "b")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendBulkTemplatedEmail")
|
||||
.formParam("Source", "sender@example.com")
|
||||
.formParam("Template", "address-syntax-bulk")
|
||||
.formParam("DefaultTemplateData", "{}")
|
||||
.formParam("Destinations.member.1.Destination.ToAddresses.member.1", "success@")
|
||||
.formParam("Destinations.member.2.Destination.ToAddresses.member.1", TO)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("SendBulkTemplatedEmailResponse.SendBulkTemplatedEmailResult.Status.member[0].Status",
|
||||
equalTo("InvalidParameterValue"))
|
||||
.body("SendBulkTemplatedEmailResponse.SendBulkTemplatedEmailResult.Status.member[0].Error",
|
||||
equalTo("Missing domain"))
|
||||
.body("SendBulkTemplatedEmailResponse.SendBulkTemplatedEmailResult.Status.member[1].Status",
|
||||
equalTo("Success"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "DeleteTemplate")
|
||||
.formParam("TemplateName", "address-syntax-bulk")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1SendBulkTemplatedEmail_malformedSourceIsReportedBeforeMissingTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendBulkTemplatedEmail")
|
||||
.formParam("Source", "Alice <sender@>")
|
||||
.formParam("Template", "address-syntax-missing")
|
||||
.formParam("DefaultTemplateData", "{}")
|
||||
.formParam("Destinations.member.1.Destination.ToAddresses.member.1", TO)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Invalid email address<Alice <sender@>>."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_malformedFromIsReportedBeforeMissingTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender.example.com",
|
||||
"DefaultContent": {"Template": {"TemplateName": "address-syntax-missing", "TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["%s"]}}]
|
||||
}
|
||||
""".formatted(TO))
|
||||
.when()
|
||||
.post("/v2/email/outbound-bulk-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid email address<sender.example.com>."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_newlineInQuotedLocalPart_returnsInvalidEmailAddress() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "\\"a\\nb\\"@example.com",
|
||||
"Destination": {"ToAddresses": ["%s"]},
|
||||
"Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}}
|
||||
}
|
||||
""".formatted(TO))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid email address \"a\nb\"@example.com."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendBulkEmail_malformedFeedbackForwarding_rejectsTheRequest() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"FeedbackForwardingEmailAddress": "bounce.example.com",
|
||||
"DefaultContent": {"Template": {
|
||||
"TemplateContent": {"Subject": "s", "Text": "b"},
|
||||
"TemplateData": "{}"}},
|
||||
"BulkEmailEntries": [{"Destination": {"ToAddresses": ["%s"]}}]
|
||||
}
|
||||
""".formatted(TO))
|
||||
.when()
|
||||
.post("/v2/email/outbound-bulk-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid email address<bounce.example.com>."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1SendBulkTemplatedEmail_malformedReturnPath_rejectsTheRequest() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendBulkTemplatedEmail")
|
||||
.formParam("Source", "sender@example.com")
|
||||
.formParam("ReturnPath", "bounce@")
|
||||
.formParam("Template", "address-syntax-missing")
|
||||
.formParam("DefaultTemplateData", "{}")
|
||||
.formParam("Destinations.member.1.Destination.ToAddresses.member.1", TO)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Invalid email address<bounce@>."));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SesSendAddressesTest {
|
||||
|
||||
@Test
|
||||
void splitAddressListSeparatesAddressesAtTopLevelCommas() {
|
||||
assertEquals(List.of("a@b.com", " \"Doe, John\" <c@d.com>", " \"x \\\"y, z\\\"\" <e@f.com>"),
|
||||
SesSendAddresses.splitAddressList("a@b.com, \"Doe, John\" <c@d.com>, \"x \\\"y, z\\\"\" <e@f.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListKeepsCommasInsideAngleBrackets() {
|
||||
assertEquals(List.of("Name <a,b@c.com>"), SesSendAddresses.splitAddressList("Name <a,b@c.com>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListIgnoresQuotesAndCommasInsideComments() {
|
||||
assertEquals(List.of("a@b.com (it\"s, (nested)) ", " c@d.com"),
|
||||
SesSendAddresses.splitAddressList("a@b.com (it\"s, (nested)) , c@d.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListKeepsParenthesesInsideQuotedNames() {
|
||||
assertEquals(List.of("\"a (b\" <x@y.com>", " z@y.com"),
|
||||
SesSendAddresses.splitAddressList("\"a (b\" <x@y.com>, z@y.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListDropsGroupSyntax() {
|
||||
assertEquals(List.of(" a@b.com", " c@d.com"),
|
||||
SesSendAddresses.splitAddressList("team: a@b.com, c@d.com;"));
|
||||
assertEquals(List.of(), SesSendAddresses.splitAddressList("undisclosed-recipients:;"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void splitAddressListKeepsColonsAndCommasInsideDomainLiterals() {
|
||||
assertEquals(List.of("user@[IPv6:2001:db8::1]", " a@[x,y]"),
|
||||
SesSendAddresses.splitAddressList("user@[IPv6:2001:db8::1], a@[x,y]"));
|
||||
assertEquals(List.of(" user@[IPv6:2001:db8::1]"),
|
||||
SesSendAddresses.splitAddressList("team: user@[IPv6:2001:db8::1];"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lastUnquotedAngleSkipsQuotedLessThan() {
|
||||
assertEquals(8, SesSendAddresses.lastUnquotedAngle("Display <\"a<b\"@example.com>"));
|
||||
assertEquals(-1, SesSendAddresses.lastUnquotedAngle("\"a<b\"@example.com"));
|
||||
assertEquals(-1, SesSendAddresses.lastUnquotedAngle("a@example.com"));
|
||||
}
|
||||
}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ses.model.BulkEmailEntry;
|
||||
import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult;
|
||||
import io.github.hectorvent.floci.services.ses.model.EmailContent;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendBulkEmailRequest;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendEmailRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
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 SesServiceAddressSyntaxTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String DOMAIN = "example.com";
|
||||
private static final String SENDER = "sender@" + DOMAIN;
|
||||
private static final String TO = "success@simulator.amazonses.com";
|
||||
private static final String NON_ASCII_LOCAL = "やまだ@" + DOMAIN;
|
||||
private static final String NO_AT = "sender." + DOMAIN;
|
||||
private static final String MISSING_FINAL_DOMAIN = "Missing final '@domain'";
|
||||
private static final String LOCAL_CONTROL = "Local address contains control or whitespace";
|
||||
private static final EmailContent.Simple SIMPLE =
|
||||
new EmailContent.Simple("Subject", "body", null, List.of());
|
||||
|
||||
private SesService service;
|
||||
private SesSentEmailService sentEmails;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
SesServiceTestBuilder builder = SesServiceTestBuilder.create();
|
||||
service = builder.build();
|
||||
sentEmails = builder.sentEmailService();
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_eachEnvelopeFieldIsChecked() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.toAddresses(List.of(TO, NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.ccAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.bccAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.returnPath(NON_ASCII_LOCAL).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.replyToAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_sourceIsReportedBeforeRecipients() {
|
||||
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(NO_AT)
|
||||
.toAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.toAddresses(List.of("success.simulator.amazonses.com")).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_ccIsReportedBeforeBcc() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.ccAddresses(List.of(NON_ASCII_LOCAL)).bccAddresses(List.of(NO_AT)).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_syntaxIsReportedBeforeUnknownConfigurationSet() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.configurationSetName("missing").content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_syntaxOfAnyFieldIsReportedBeforeLength() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.toAddresses(List.of(address(321))).content(SIMPLE).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(address(321))
|
||||
.toAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_syntaxIsReportedBeforeMissingDestination() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.toAddresses(List.of()).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_missingSourceIsStillReportedFirst() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(request(null)
|
||||
.toAddresses(List.of(NON_ASCII_LOCAL)).content(SIMPLE).build()));
|
||||
|
||||
assertEquals("Source email is required.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleSend_nonAsciiDisplayNamesAreAccepted() {
|
||||
assertDoesNotThrow(() -> service.sendEmail(request("山田 <" + SENDER + ">")
|
||||
.toAddresses(List.of("\"山田\" <" + TO + ">")).content(SIMPLE).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void templatedSend_syntaxIsReportedBeforeMissingTemplate() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.content(new EmailContent.Template("missing", null, List.of())).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_sourceParameterIsChecked() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(NON_ASCII_LOCAL)
|
||||
.content(raw(SENDER, TO)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_fromHeaderIsChecked() {
|
||||
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(null)
|
||||
.content(raw(NO_AT, TO)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_destinationsAreChecked() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
.toAddresses(List.of(NON_ASCII_LOCAL)).content(raw(SENDER, TO)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_toHeaderIsChecked() {
|
||||
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(null).toAddresses(List.of())
|
||||
.content(raw(SENDER, TO + ", " + NO_AT)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_domainLiteralInToHeaderIsAccepted() {
|
||||
assertDoesNotThrow(() -> service.sendEmail(request(SENDER).content(raw(SENDER,
|
||||
TO + ", user@[IPv6:2001:db8::1]")).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_unprobedAddressHeadersAreNotChecked() {
|
||||
assertDoesNotThrow(() -> service.sendEmail(request(SENDER).content(raw(SENDER, TO,
|
||||
"Cc: " + NO_AT + "\r\nBcc: " + NON_ASCII_LOCAL + "\r\nReply-To: " + NO_AT
|
||||
+ "\r\nReturn-Path: <>\r\n")).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_nonAsciiFromHeaderNameIsAccepted() {
|
||||
String raw = Base64.getEncoder().encodeToString(("From: 山田 <" + SENDER + ">\r\nTo: " + TO
|
||||
+ "\r\nSubject: s\r\n\r\nbody").getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertDoesNotThrow(() -> service.sendEmail(request(null).content(new EmailContent.Raw(raw)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bulkSend_malformedEntryRecipientFailsOnlyThatEntry() {
|
||||
List<BulkEmailEntryResult> results = service.sendBulkEmail(SendBulkEmailRequest.builder()
|
||||
.source(SENDER)
|
||||
.defaultContent(new EmailContent.InlineTemplate("Subject", "body", null, null, List.of()))
|
||||
.entries(List.of(
|
||||
new BulkEmailEntry(List.of(TO), null, null, null, null, null),
|
||||
new BulkEmailEntry(List.of(TO), List.of(NON_ASCII_LOCAL), null, null, null, null),
|
||||
new BulkEmailEntry(List.of(TO), null, List.of(NO_AT), null, null, null)))
|
||||
.region(REGION)
|
||||
.build());
|
||||
|
||||
assertEquals(BulkEmailEntryResult.Status.SUCCESS, results.get(0).getStatus());
|
||||
assertEquals(BulkEmailEntryResult.Status.INVALID_PARAMETER, results.get(1).getStatus());
|
||||
assertEquals(LOCAL_CONTROL, results.get(1).getError());
|
||||
assertEquals(BulkEmailEntryResult.Status.INVALID_PARAMETER, results.get(2).getStatus());
|
||||
assertEquals(MISSING_FINAL_DOMAIN, results.get(2).getError());
|
||||
assertEquals(1, sentEmails.countInRegion(REGION), "only the successful entry is recorded");
|
||||
}
|
||||
|
||||
@Test
|
||||
void bulkSend_malformedReplyToFailsEveryEntry() {
|
||||
List<BulkEmailEntryResult> results = service.sendBulkEmail(SendBulkEmailRequest.builder()
|
||||
.source(SENDER)
|
||||
.replyToAddresses(List.of(NO_AT))
|
||||
.defaultContent(new EmailContent.InlineTemplate("Subject", "body", null, null, List.of()))
|
||||
.entries(List.of(
|
||||
new BulkEmailEntry(List.of(TO), null, null, null, null, null),
|
||||
new BulkEmailEntry(List.of(TO), null, null, null, null, null)))
|
||||
.region(REGION)
|
||||
.build());
|
||||
|
||||
for (BulkEmailEntryResult result : results) {
|
||||
assertEquals(BulkEmailEntryResult.Status.INVALID_PARAMETER, result.getStatus());
|
||||
assertEquals(MISSING_FINAL_DOMAIN, result.getError());
|
||||
}
|
||||
assertEquals(0, sentEmails.countInRegion(REGION), "no entry is recorded");
|
||||
}
|
||||
|
||||
private void assertRejects(String message, Executable send) {
|
||||
long recorded = sentEmails.countInRegion(REGION);
|
||||
AwsException e = assertThrows(AwsException.class, send);
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertEquals(recorded, sentEmails.countInRegion(REGION), "a rejected send must not be recorded");
|
||||
}
|
||||
|
||||
private static SendEmailRequest.Builder request(String source) {
|
||||
return SendEmailRequest.builder()
|
||||
.source(source)
|
||||
.toAddresses(List.of(TO))
|
||||
.region(REGION);
|
||||
}
|
||||
|
||||
private static EmailContent.Raw raw(String from, String to) {
|
||||
return raw(from, to, "");
|
||||
}
|
||||
|
||||
private static EmailContent.Raw raw(String from, String to, String extraHeaders) {
|
||||
return new EmailContent.Raw("From: " + from + "\r\nTo: " + to + "\r\n" + extraHeaders
|
||||
+ "Subject: s\r\n\r\nbody");
|
||||
}
|
||||
|
||||
private static String address(int length) {
|
||||
return "a".repeat(length - DOMAIN.length() - 1) + "@" + DOMAIN;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user