mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): check a v2 raw From header only in place of a missing sender (#5490)
* fix(ses): check a v2 raw From header only in place of a missing sender A v2 SendEmail with Content.Raw reads the MIME From header only when FromEmailAddress is omitted. AWS then answers "Invalid From address provided." for an empty domain or a non-ASCII local part or domain, and keeps "Missing final '@domain'" for a missing @. With FromEmailAddress given, the header is not checked at all. v1 SendRawEmail is unchanged. * fix(ses): skip the v2 raw From header length check when a sender is given A v2 SendEmail with Content.Raw and FromEmailAddress ignores the From header's 320-character limit as well as its syntax. The From-header decision now uses the sender-omitted flag SendRawEmail already computes, and the v2 wording keys on the kind of syntax violation, not its message. * refactor(ses): read a raw send's sender presence from the request SendEmailRequest gains hasSource(), alongside hasRecipients(), so sendRawEmail and the raw From-header checks no longer pass a sender-omitted flag around. * fix(ses): report a v2 raw From header's missing @ after the destination check AWS reports a missing @ in a v2 raw From header, read in place of FromEmailAddress, only after the configuration set and the missing-destination checks and before the length check, so a message with no recipient anywhere gets "Missing required header 'To'." The missing-destination check now precedes the length check on both APIs. * refactor(ses): compare the missing @ by its message constant The v2 raw From-header checks only need to tell a missing @ from the other violations, which the message constant already does, so the violation enum goes and violation() returns its messages directly again. * fix(ses): resolve a raw sender's default configuration set before the destination check A raw send whose sender comes from the MIME From header re-resolves the identity's default configuration set; that check now runs before the missing-destination check again, so a deleted default is still reported instead of "Missing required header 'To'." * test(ses): pin the unknown configuration set ahead of a raw send's missing destination * test(ses): save the stale-default identity through the identity service
This commit is contained in:
1 parent
58bad471f3
commit
1230aec424
11 files changed
+339
-18
No files matched your search
+120
@@ -2,12 +2,16 @@ package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
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.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@@ -269,4 +273,120 @@ class SesSendAddressSyntaxIntegrationTest {
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Invalid email address<bounce@>."));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"sender.example.com, Missing final '@domain'",
|
||||
"sender@, Invalid From address provided.",
|
||||
"やまだ@example.com, Invalid From address provided.",
|
||||
"sender@例え.jp, Invalid From address provided."
|
||||
})
|
||||
void v2SendEmailRaw_malformedFromHeaderWithoutFromEmailAddress(String from, String message) {
|
||||
given()
|
||||
.contentType("application/json; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"Destination": {"ToAddresses": ["%s"]},
|
||||
"Content": {"Raw": {"Data": "%s"}}
|
||||
}
|
||||
""".formatted(TO, rawMessage(from)))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"sender.example.com", "sender@", "やまだ@example.com", "sender@例え.jp"})
|
||||
void v2SendEmailRaw_malformedFromHeaderIsNotCheckedWithFromEmailAddress(String from) {
|
||||
given()
|
||||
.contentType("application/json; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["%s"]},
|
||||
"Content": {"Raw": {"Data": "%s"}}
|
||||
}
|
||||
""".formatted(TO, rawMessage(from)))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MessageId", notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmailRaw_overLimitFromHeaderIsNotMeasuredWithFromEmailAddress() {
|
||||
String from = "A".repeat(321 - " <sender@example.com>".length()) + " <sender@example.com>";
|
||||
|
||||
given()
|
||||
.contentType("application/json; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["%s"]},
|
||||
"Content": {"Raw": {"Data": "%s"}}
|
||||
}
|
||||
""".formatted(TO, rawMessage(from)))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MessageId", notNullValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"sender.example.com, Missing required header 'To'.",
|
||||
"sender@, Invalid From address provided."
|
||||
})
|
||||
void v2SendEmailRaw_malformedFromHeaderWithoutAnyDestination(String from, String message) {
|
||||
String raw = Base64.getEncoder().encodeToString(("From: " + from + "\r\nSubject: s\r\n\r\nb")
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
given()
|
||||
.contentType("application/json; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{"Content": {"Raw": {"Data": "%s"}}}
|
||||
""".formatted(raw))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"sender.example.com, Missing final '@domain'",
|
||||
"sender@, Missing domain",
|
||||
"やまだ@example.com, Local address contains control or whitespace",
|
||||
"sender@例え.jp, Domain contains control or whitespace"
|
||||
})
|
||||
void v1SendRawEmail_malformedFromHeader_keepsJavaMailMessages(String from, String message) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendRawEmail")
|
||||
.formParam("Destinations.member.1", TO)
|
||||
.formParam("RawMessage.Data", rawMessage(from))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message));
|
||||
}
|
||||
|
||||
private static String rawMessage(String from) {
|
||||
return Base64.getEncoder().encodeToString(("From: " + from + "\r\nTo: " + TO + "\r\nSubject: s\r\n\r\nb")
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
+19
@@ -70,6 +70,25 @@ class SesSendRawNoDestinationIntegrationTest {
|
||||
.body("message", equalTo("Missing required header 'To'."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_rawWithoutAnyRecipient_reportsUnknownConfigurationSetFirst() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{
|
||||
"ConfigurationSetName": "no-destination-missing-cs",
|
||||
"Content": {"Raw": {"Data": "%s"}}
|
||||
}
|
||||
""".formatted(rawData("From: sender@example.com\r\n")))
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"))
|
||||
.body("message", equalTo("Configuration set <no-destination-missing-cs> does not exist."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2SendEmail_rawWithEmptyDestination_returnsMissingToHeader() {
|
||||
given()
|
||||
|
||||
+16
@@ -134,6 +134,22 @@ class SesServiceAddressLengthTest {
|
||||
.content(raw(from, TO, "")).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderIsNotMeasuredWhenSenderIsGiven() {
|
||||
String from = address(321, 'c');
|
||||
|
||||
assertDoesNotThrow(() -> service.sendEmail(request(SENDER)
|
||||
.content(new EmailContent.Raw(raw(from, TO, "").data(), true)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderIsMeasuredWithoutSender() {
|
||||
String from = address(321, 'c');
|
||||
|
||||
assertRejects(from, () -> service.sendEmail(request(null)
|
||||
.content(new EmailContent.Raw(raw(from, TO, "").data(), true)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_destinationsAreReportedBeforeToHeader() {
|
||||
String destination = address(500, 'd');
|
||||
|
||||
+107
@@ -4,6 +4,7 @@ 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.Identity;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendBulkEmailRequest;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendEmailRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -11,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@@ -26,19 +28,24 @@ class SesServiceAddressSyntaxTest {
|
||||
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 EMPTY_DOMAIN = "sender@";
|
||||
private static final String NON_ASCII_DOMAIN = "sender@例え.jp";
|
||||
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 String INVALID_FROM = "Invalid From address provided.";
|
||||
private static final EmailContent.Simple SIMPLE =
|
||||
new EmailContent.Simple("Subject", "body", null, List.of());
|
||||
|
||||
private SesService service;
|
||||
private SesSentEmailService sentEmails;
|
||||
private SesIdentityService identityService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
SesServiceTestBuilder builder = SesServiceTestBuilder.create();
|
||||
service = builder.build();
|
||||
sentEmails = builder.sentEmailService();
|
||||
identityService = builder.identityService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -122,6 +129,92 @@ class SesServiceAddressSyntaxTest {
|
||||
.content(raw(NO_AT, TO)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_fromHeaderKeepsJavaMailMessagesWithOrWithoutSender() {
|
||||
for (String source : Arrays.asList(null, SENDER)) {
|
||||
assertRejects("Missing domain", () -> service.sendEmail(request(source)
|
||||
.content(fromHeaderRaw(EMPTY_DOMAIN, false)).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(source)
|
||||
.content(fromHeaderRaw(NON_ASCII_LOCAL, false)).build()));
|
||||
assertRejects("Domain contains control or whitespace", () -> service.sendEmail(request(source)
|
||||
.content(fromHeaderRaw(NON_ASCII_DOMAIN, false)).build()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderWithoutAtKeepsJavaMailMessage() {
|
||||
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(null)
|
||||
.content(fromHeaderRaw(NO_AT, true)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_otherFromHeaderViolationsAreAnInvalidFromAddress() {
|
||||
for (String from : List.of(EMPTY_DOMAIN, NON_ASCII_LOCAL, NON_ASCII_DOMAIN)) {
|
||||
assertRejects(INVALID_FROM, () -> service.sendEmail(request(null)
|
||||
.content(fromHeaderRaw(from, true)).build()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderIsNotCheckedWhenSenderIsGiven() {
|
||||
for (String from : List.of(NO_AT, EMPTY_DOMAIN, NON_ASCII_LOCAL, NON_ASCII_DOMAIN)) {
|
||||
assertDoesNotThrow(() -> service.sendEmail(request(SENDER)
|
||||
.content(fromHeaderRaw(from, true)).build()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_malformedSenderKeepsJavaMailMessage() {
|
||||
assertRejects("Missing domain", () -> service.sendEmail(request(EMPTY_DOMAIN)
|
||||
.content(fromHeaderRaw(SENDER, true)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderWithoutAtIsReportedAfterMissingDestination() {
|
||||
assertRejects("Missing required header 'To'.", () -> service.sendEmail(request(null).toAddresses(List.of())
|
||||
.content(v2RawWithoutTo(NO_AT)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_staleDefaultConfigurationSetOfFromHeaderIsReportedBeforeMissingDestination() {
|
||||
Identity identity = new Identity(DOMAIN, "Domain");
|
||||
identity.setConfigurationSetName("deleted");
|
||||
identityService.save(identity, REGION);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(request(null)
|
||||
.toAddresses(List.of()).content(v2RawWithoutTo(SENDER)).build()));
|
||||
|
||||
assertEquals("Configuration set <deleted> does not exist.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_otherFromHeaderViolationsAreReportedBeforeMissingDestination() {
|
||||
assertRejects(INVALID_FROM, () -> service.sendEmail(request(null).toAddresses(List.of())
|
||||
.content(v2RawWithoutTo(EMPTY_DOMAIN)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderWithoutAtIsReportedAfterRecipientSyntax() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(null).toAddresses(List.of(NON_ASCII_LOCAL))
|
||||
.content(fromHeaderRaw(NO_AT, true)).build()));
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(null).toAddresses(List.of())
|
||||
.content(fromHeaderRaw(NO_AT, true, NON_ASCII_LOCAL)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderWithoutAtIsReportedAfterUnknownConfigurationSet() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(request(null)
|
||||
.configurationSetName("missing").content(fromHeaderRaw(NO_AT, true)).build()));
|
||||
|
||||
assertEquals("ConfigurationSetDoesNotExist", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2RawSend_fromHeaderWithoutAtIsReportedBeforeLength() {
|
||||
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(null).toAddresses(List.of(address(321)))
|
||||
.content(fromHeaderRaw(NO_AT, true)).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSend_destinationsAreChecked() {
|
||||
assertRejects(LOCAL_CONTROL, () -> service.sendEmail(request(SENDER)
|
||||
@@ -220,6 +313,20 @@ class SesServiceAddressSyntaxTest {
|
||||
return raw(from, to, "");
|
||||
}
|
||||
|
||||
private static EmailContent.Raw fromHeaderRaw(String from, boolean fromHeaderAsFallback) {
|
||||
return fromHeaderRaw(from, fromHeaderAsFallback, TO);
|
||||
}
|
||||
|
||||
private static EmailContent.Raw fromHeaderRaw(String from, boolean fromHeaderAsFallback, String to) {
|
||||
return new EmailContent.Raw(Base64.getEncoder().encodeToString(("From: " + from + "\r\nTo: " + to
|
||||
+ "\r\nSubject: s\r\n\r\nbody").getBytes(StandardCharsets.UTF_8)), fromHeaderAsFallback);
|
||||
}
|
||||
|
||||
private static EmailContent.Raw v2RawWithoutTo(String from) {
|
||||
return new EmailContent.Raw(Base64.getEncoder().encodeToString(("From: " + from
|
||||
+ "\r\nSubject: s\r\n\r\nbody").getBytes(StandardCharsets.UTF_8)), true);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
Reference in new issue
Block a user