mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user