mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ses.model.EmailContent;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendEmailRequest;
|
||||
import org.apache.james.mime4j.dom.Message;
|
||||
|
||||
@@ -32,8 +33,10 @@ final class SesAddressLength {
|
||||
* encoded and escaped quotes count (probe-confirmed). Each address of a header is checked on its
|
||||
* own, so a header listing several short addresses passes however long it is.
|
||||
*/
|
||||
static void requireRaw(SendEmailRequest request, Message message) {
|
||||
SesSendAddresses.forEachRaw(request, message, RAW_RECIPIENT_HEADERS, SesAddressLength::require);
|
||||
static void requireRaw(SendEmailRequest request, EmailContent.Raw raw, Message message) {
|
||||
SesSendAddresses.forEachRaw(request, message, RAW_RECIPIENT_HEADERS,
|
||||
raw.checksFromHeader(request.hasSource()) ? SesAddressLength::require : SesSendAddresses.UNCHECKED,
|
||||
SesAddressLength::require);
|
||||
}
|
||||
|
||||
static void require(String address) {
|
||||
|
||||
@@ -2,11 +2,13 @@ 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.EmailContent;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendEmailRequest;
|
||||
import org.apache.james.mime4j.dom.Message;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* The address syntax check AWS applies to every address of a send ahead of the destination,
|
||||
@@ -18,11 +20,15 @@ import java.util.List;
|
||||
* except for a CR or LF, which AWS rejects with its own wording even before whitespace.
|
||||
* In a raw message AWS checks the From, To, Cc and Bcc headers (probe-confirmed);
|
||||
* the Reply-To and Return-Path headers were not probed and are left unchecked. The v2 controller
|
||||
* remaps the code to BadRequestException.
|
||||
* remaps the code to BadRequestException. A v2 raw send checks its From header only without a
|
||||
* sender parameter, and then answers an invalid From address for every violation but a missing
|
||||
* {@code @}, which it reports later (probe-confirmed for the four JavaMail messages; a quoted CR
|
||||
* or LF is assumed to follow).
|
||||
*/
|
||||
final class SesAddressSyntax {
|
||||
|
||||
private static final List<String> RAW_CHECKED_HEADERS = List.of("To", "Cc", "Bcc");
|
||||
private static final String MISSING_FINAL_DOMAIN = "Missing final '@domain'";
|
||||
|
||||
private SesAddressSyntax() {
|
||||
}
|
||||
@@ -31,8 +37,40 @@ final class SesAddressSyntax {
|
||||
SesSendAddresses.forEachEnvelope(request, SesAddressSyntax::require);
|
||||
}
|
||||
|
||||
static void requireRaw(SendEmailRequest request, Message message) {
|
||||
SesSendAddresses.forEachRaw(request, message, RAW_CHECKED_HEADERS, SesAddressSyntax::require);
|
||||
static void requireRaw(SendEmailRequest request, EmailContent.Raw raw, Message message) {
|
||||
SesSendAddresses.forEachRaw(request, message, RAW_CHECKED_HEADERS, fromHeaderCheck(request, raw),
|
||||
SesAddressSyntax::require);
|
||||
}
|
||||
|
||||
private static Consumer<String> fromHeaderCheck(SendEmailRequest request, EmailContent.Raw raw) {
|
||||
if (!raw.checksFromHeader(request.hasSource())) {
|
||||
return SesSendAddresses.UNCHECKED;
|
||||
}
|
||||
return raw.fromHeaderAsFallback() ? SesAddressSyntax::requireFallbackFrom : SesAddressSyntax::require;
|
||||
}
|
||||
|
||||
private static void requireFallbackFrom(String address) {
|
||||
String violation = violation(address);
|
||||
if (violation != null && !MISSING_FINAL_DOMAIN.equals(violation)) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid From address provided.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The missing {@code @} of a v2 raw From header read in place of the sender, which AWS reports
|
||||
* only after the configuration set and the missing-destination checks, and before the length
|
||||
* check (probe-confirmed).
|
||||
*/
|
||||
static void requireFallbackFromAt(SendEmailRequest request, EmailContent.Raw raw, Message message) {
|
||||
if (raw.fromHeaderAsFallback() && !request.hasSource()) {
|
||||
SesSendAddresses.forEachHeaderAddress(message, "From", SesAddressSyntax::requireAt);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireAt(String address) {
|
||||
if (MISSING_FINAL_DOMAIN.equals(violation(address))) {
|
||||
throw new AwsException("InvalidParameterValue", MISSING_FINAL_DOMAIN, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +136,7 @@ final class SesAddressSyntax {
|
||||
}
|
||||
}
|
||||
if (i >= addrSpec.length()) {
|
||||
return "Missing final '@domain'";
|
||||
return MISSING_FINAL_DOMAIN;
|
||||
}
|
||||
String domain = addrSpec.substring(i + 1);
|
||||
if (domain.isEmpty()) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
final class SesSendAddresses {
|
||||
|
||||
static final Consumer<String> UNCHECKED = address -> { };
|
||||
|
||||
private SesSendAddresses() {
|
||||
}
|
||||
|
||||
@@ -30,12 +32,13 @@ final class SesSendAddresses {
|
||||
/**
|
||||
* A raw send: the sender parameter, the From header, the envelope fields of the request, and
|
||||
* then the named headers of the MIME message. Each address of a header is visited on its own,
|
||||
* as written (an encoded-word name stays encoded).
|
||||
* as written (an encoded-word name stays encoded). The From header goes to its own check, which
|
||||
* is {@link #UNCHECKED} when the send ignores that header.
|
||||
*/
|
||||
static void forEachRaw(SendEmailRequest request, Message message, List<String> headerNames,
|
||||
Consumer<String> check) {
|
||||
Consumer<String> fromHeaderCheck, Consumer<String> check) {
|
||||
check.accept(request.source());
|
||||
forEachHeaderAddress(message, "From", check);
|
||||
forEachHeaderAddress(message, "From", fromHeaderCheck);
|
||||
forEachRecipient(request, check);
|
||||
for (String name : headerNames) {
|
||||
forEachHeaderAddress(message, name, check);
|
||||
@@ -130,7 +133,7 @@ final class SesSendAddresses {
|
||||
}
|
||||
}
|
||||
|
||||
private static void forEachHeaderAddress(Message message, String name, Consumer<String> check) {
|
||||
static void forEachHeaderAddress(Message message, String name, Consumer<String> check) {
|
||||
Header header = message == null ? null : message.getHeader();
|
||||
if (header == null) {
|
||||
return;
|
||||
|
||||
@@ -135,7 +135,7 @@ public class SesSendController {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Content.Raw.Data is required.", 400);
|
||||
}
|
||||
SendEmailRequest rawRequest = sendRequest.content(new EmailContent.Raw(rawData)).build();
|
||||
SendEmailRequest rawRequest = sendRequest.content(new EmailContent.Raw(rawData, true)).build();
|
||||
sesService.checkTenantRawSendAccess(tenantName, fromEmailAddress, rawData,
|
||||
rawRequest.hasRecipients(), configurationSetName, regionResolver.getAccountId(), region);
|
||||
messageId = sesService.sendEmail(rawRequest);
|
||||
|
||||
@@ -426,13 +426,12 @@ public class SesService {
|
||||
if (raw.data() == null || raw.data().isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "RawMessage.Data is required.", 400);
|
||||
}
|
||||
boolean sourceOmitted = source == null || source.isBlank();
|
||||
// The MIME headers are always parsed: X-SES-CONFIGURATION-SET can name the configuration
|
||||
// set that decides whether events are published at all, so the configuration set cannot be
|
||||
// resolved before the message has been read.
|
||||
SmtpRelay.ParsedRawMessage parsed = SmtpRelay.parseRawMessage(raw.data());
|
||||
SmtpRelay.RawMessageHeaders headers = parsed.headers();
|
||||
SesAddressSyntax.requireRaw(request, parsed.message());
|
||||
SesAddressSyntax.requireRaw(request, raw, parsed.message());
|
||||
// AWS accepts the configuration set either as a request field or as the
|
||||
// X-SES-CONFIGURATION-SET header on the message itself; the request field wins.
|
||||
String requestedConfigSet = firstNonBlank(request.configurationSetName(), headers.configurationSet());
|
||||
@@ -444,7 +443,7 @@ public class SesService {
|
||||
List<MessageTag> effectiveTags = (request.emailTags() == null || request.emailTags().isEmpty())
|
||||
? headers.messageTags()
|
||||
: request.emailTags();
|
||||
String effectiveSource = sourceOmitted && !headers.from().isBlank()
|
||||
String effectiveSource = !request.hasSource() && !headers.from().isBlank()
|
||||
? headers.from()
|
||||
: source;
|
||||
if (effectiveSource == null || effectiveSource.isBlank()) {
|
||||
@@ -454,12 +453,11 @@ public class SesService {
|
||||
// both with this message.
|
||||
throw new AwsException("InvalidParameterValue", "Missing required header 'From'.", 400);
|
||||
}
|
||||
SesAddressLength.requireRaw(request, parsed.message());
|
||||
// FromEmailAddress was omitted, so the configuration set couldn't be resolved from the
|
||||
// sender until the MIME "From" was parsed. Re-resolve from the effective sender now so an
|
||||
// email identity's default configuration set still applies to a Raw send without an
|
||||
// explicit FromEmailAddress.
|
||||
if (sourceOmitted && (requestedConfigSet == null || requestedConfigSet.isBlank())) {
|
||||
if (!request.hasSource() && (requestedConfigSet == null || requestedConfigSet.isBlank())) {
|
||||
effectiveConfigSet = resolveDefaultConfigurationSet(requestedConfigSet, effectiveSource, region);
|
||||
configSetService.validateForSending(effectiveConfigSet, region);
|
||||
}
|
||||
@@ -469,6 +467,8 @@ public class SesService {
|
||||
if (effectiveDestinations.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "Missing required header 'To'.", 400);
|
||||
}
|
||||
SesAddressSyntax.requireFallbackFromAt(request, raw, parsed.message());
|
||||
SesAddressLength.requireRaw(request, raw, parsed.message());
|
||||
// Resolve suppression before recording the message so a bad ListManagementOptions (e.g. an
|
||||
// unknown contact list) fails the whole send without leaving an orphaned SentEmail record.
|
||||
Map<String, String> suppressedReasons = new LinkedHashMap<>(
|
||||
|
||||
@@ -47,8 +47,19 @@ public sealed interface EmailContent {
|
||||
/**
|
||||
* A raw send reads the envelope's to, cc and bcc as one flat destination list, and falls back
|
||||
* to the MIME headers when that list is empty. It ignores {@code replyToAddresses}: the
|
||||
* message carries its own Reply-To header.
|
||||
* message carries its own Reply-To header. {@code fromHeaderAsFallback} marks a v2 send, which
|
||||
* reads the From header only in place of a missing sender parameter: it skips the header's
|
||||
* syntax and length checks when the sender is given and words a malformed one as an invalid
|
||||
* From address (probe-confirmed). A v1 send always checks the header with JavaMail's messages.
|
||||
*/
|
||||
record Raw(String data) implements EmailContent {
|
||||
record Raw(String data, boolean fromHeaderAsFallback) implements EmailContent {
|
||||
|
||||
public Raw(String data) {
|
||||
this(data, false);
|
||||
}
|
||||
|
||||
public boolean checksFromHeader(boolean senderGiven) {
|
||||
return !fromHeaderAsFallback || !senderGiven;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,10 @@ public record SendEmailRequest(
|
||||
return all;
|
||||
}
|
||||
|
||||
public boolean hasSource() {
|
||||
return source != null && !source.isBlank();
|
||||
}
|
||||
|
||||
public boolean hasRecipients() {
|
||||
return !recipients().isEmpty();
|
||||
}
|
||||
|
||||
+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