diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetSendingTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetSendingTest.java index f4007d3b5d..418157ea41 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetSendingTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesConfigurationSetSendingTest.java @@ -12,6 +12,7 @@ import software.amazon.awssdk.services.ses.model.ConfigurationSetAttribute; import software.amazon.awssdk.services.ses.model.DescribeConfigurationSetRequest; import software.amazon.awssdk.services.ses.model.DescribeConfigurationSetResponse; import software.amazon.awssdk.services.ses.model.UpdateConfigurationSetSendingEnabledRequest; +import software.amazon.awssdk.services.ses.model.VerifyEmailIdentityRequest; import software.amazon.awssdk.services.sesv2.SesV2Client; import software.amazon.awssdk.services.sesv2.model.Body; import software.amazon.awssdk.services.sesv2.model.Content; @@ -67,6 +68,7 @@ class SesConfigurationSetSendingTest { } catch (Exception ignored) {} sesV2.createConfigurationSet(CreateConfigurationSetRequest.builder() .configurationSetName(CS_NAME).build()); + sesV1.verifyEmailIdentity(VerifyEmailIdentityRequest.builder().emailAddress(FROM).build()); } @AfterAll diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesListManagementOptionsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesListManagementOptionsTest.java index ebd4879c7d..ae50e2cac6 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesListManagementOptionsTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesListManagementOptionsTest.java @@ -5,9 +5,11 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import software.amazon.awssdk.services.sesv2.SesV2Client; +import software.amazon.awssdk.services.sesv2.model.AlreadyExistsException; import software.amazon.awssdk.services.sesv2.model.Body; import software.amazon.awssdk.services.sesv2.model.Content; import software.amazon.awssdk.services.sesv2.model.CreateContactListRequest; +import software.amazon.awssdk.services.sesv2.model.CreateEmailIdentityRequest; import software.amazon.awssdk.services.sesv2.model.DeleteContactListRequest; import software.amazon.awssdk.services.sesv2.model.Destination; import software.amazon.awssdk.services.sesv2.model.EmailContent; @@ -53,6 +55,11 @@ class SesListManagementOptionsTest { .description("weekly digest") .build()) .build()); + try { + sesV2.createEmailIdentity(CreateEmailIdentityRequest.builder().emailIdentity(FROM).build()); + } catch (AlreadyExistsException expected) { + // Another suite already verified this shared sender, which is all a send needs. + } } @AfterAll diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTenantTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTenantTest.java index ae1b42250e..456ba7787b 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTenantTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTenantTest.java @@ -11,7 +11,6 @@ import software.amazon.awssdk.services.sesv2.SesV2Client; import software.amazon.awssdk.services.sesv2.model.AlreadyExistsException; import software.amazon.awssdk.services.sesv2.model.BadRequestException; import software.amazon.awssdk.services.sesv2.model.CreateConfigurationSetRequest; -import software.amazon.awssdk.services.sesv2.model.CreateEmailIdentityRequest; import software.amazon.awssdk.services.sesv2.model.CreateTenantRequest; import software.amazon.awssdk.services.sesv2.model.CreateTenantResponse; import software.amazon.awssdk.services.sesv2.model.DeleteConfigurationSetRequest; @@ -72,7 +71,8 @@ class SesTenantTest { @BeforeAll static void setup() { sesV2 = TestFixtures.sesV2Client(); - sesV2.createEmailIdentity(CreateEmailIdentityRequest.builder().emailIdentity(IDENTITY).build()); + // Verified, so tenantSend_gatesOnAssociations can send from an address under it. + TestFixtures.verifySesDomainIdentityViaRoute53(sesV2, IDENTITY); sesV2.createConfigurationSet(CreateConfigurationSetRequest.builder() .configurationSetName(CONFIG_SET).build()); sesV2.putSuppressedDestination(r -> r.emailAddress(ACCOUNT_ADDR) diff --git a/docs/services/ses.md b/docs/services/ses.md index 3b5a1f19d2..89b90f9e77 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -209,9 +209,12 @@ when message tags are supplied both as a request field and as a header, AWS uses only the request field's tags and does not join the two sets. Floci matches this: header tags apply only when the request passes none. -All other `X-SES-*` headers, including the sending-authorization headers -`X-SES-SOURCE-ARN`, `X-SES-FROM-ARN`, and `X-SES-RETURN-PATH-ARN`, are -stripped from the relayed message without being acted on. +The sending-authorization headers `X-SES-SOURCE-ARN`, `X-SES-FROM-ARN`, and +`X-SES-RETURN-PATH-ARN` are read for the verified-sender check (see +[Current Behavior](#current-behavior)): the first instance of each counts, and the +matching `SourceArn`, `FromArn` or `ReturnPathArn` request parameter wins over +it, as on AWS. All `X-SES-*` headers are stripped from the relayed message; +the ones not listed here are not acted on. ## Local Inspection Endpoint @@ -295,10 +298,11 @@ curl $AWS_ENDPOINT_URL/_aws/ses ## Current Behavior -- Identity verification succeeds immediately; no real DNS or inbox verification flow is required. +- An email address identity is verified as soon as it is created; no inbox verification flow is required. A domain identity stays `Pending` until its DKIM CNAME records exist in a Route 53 hosted zone in Floci, then becomes `Success`. +- `SendEmail`, `SendRawEmail` and `SendTemplatedEmail`, and v2 `SendEmail`, require verified senders, as on AWS ([SendEmail](https://docs.aws.amazon.com/ses/latest/APIReference/API_SendEmail.html), [Email sending errors](https://docs.aws.amazon.com/ses/latest/dg/troubleshoot-error-messages.html)): each address used as a "From", "Source", "Sender" or "Return-Path" address must be a verified email address identity or an address in a verified domain identity or one of its subdomains. That is the `Source` or `FromEmailAddress` and the `ReturnPath` or `FeedbackForwardingEmailAddress`, plus, on a raw message, every mailbox of its `From` header, its `Sender` and its `Return-Path` header. A v2 raw send reads the `From` header only in place of a missing `FromEmailAddress` and accepts any `From` header when one is given (probe-confirmed), so the header is checked only then. Every other address present is checked; whether AWS still checks a `Return-Path` header that the request's return path overrides has not been probed. One `MessageRejected` lists every unverified address ("Email address is not verified. The following identities failed the check in region US-EAST-1: a@example.com, b@example.com") and nothing is captured or relayed. An address covered by a sending-authorization ARN of another account (`SourceArn`, `FromArn`, `ReturnPathArn`, their `X-SES-*-ARN` headers, or v2 `FromEmailAddressIdentityArn` and `FeedbackForwardingEmailAddressIdentityArn`) is not checked, since it is the owner's identity; on a raw send the source ARN also covers the `From` and return-path addresses that have no ARN of their own. The bulk sends do not check the sender. The verification codes and invitations Cognito sends are not affected. - `SendEmail` stores the text body or the HTML body as the captured message body. - `SetIdentityNotificationTopic` publishes to the configured topic on a Bounce/Complaint/Delivery event (triggered via the mailbox simulator addresses or the suppression list), independent of any configuration set. The payload uses the legacy format (`notificationType`, no `mail.tags`, headers only when `SetIdentityHeadersInNotificationsEnabled` is on). -- Identity (sending authorization) policies are stored and returned as metadata: the policy document, the per-identity limit of 20, and the create/update/delete error shapes match AWS, but Floci does not evaluate policy authorization (Principal-account existence, Resource-ARN match) or gate sending on it. +- Identity (sending authorization) policies are stored and returned as metadata: the policy document, the per-identity limit of 20, and the create/update/delete error shapes match AWS, but Floci does not evaluate policy authorization (Principal-account existence, Resource-ARN match) or gate sending on it. A send naming another account's identity ARN skips the verified-sender check for the addresses it covers, without looking that identity up or reading its policy. - Receipt rule sets and receipt rules are stored inertly: Floci has no inbound-mail endpoint, so stored rules route no mail and their actions never execute, but the management API round-trips with AWS ordering, validation, and error semantics (probe-verified). Action targets are validated against the local emulator with the AWS error codes: an SNS `TopicArn`, S3 `BucketName`, or Lambda `FunctionArn` must reference an existing local resource, and a `BounceAction` `Sender` must be a verified identity. Deviations: S3 bucket write permissions, KMS keys, and `ConnectAction` role assumability are not verified (AWS checks these); `WorkmailAction` gets no resource validation (neither does AWS). Receipt IP filters are stored the same way, with the probed AWS validation and the 100-filter limit, but no inbound connection is ever filtered. `ReorderReceiptRuleSet` requires an exact permutation of the set's rules; `CloneReceiptRuleSet` copies them under a new creation timestamp, leaving the active flag behind. - Custom verification email templates are stored and returned; `Create`/`Update` require the `FromEmailAddress` to be a verified identity (or a verified domain) and reject an invalid redirection URL, matching AWS. `SendCustomVerificationEmail` renders the template into the `/_aws/ses` inspection mailbox (and the SMTP relay, when configured) and registers the recipient as a pending-verification identity, matching AWS. The template body has no placeholder that AWS substitutes, so it is passed through verbatim with the same fixed disclaimer AWS always appends; the unique verification link AWS appends is not reproduced because Floci has no verification-click flow. The `SuccessRedirectionURL` / `FailureRedirectionURL` are stored and returned by Get/List but are the post-click redirect targets, so they are not used at send time. - For the REST JSON API see [SES v2](#v2) below. diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java index b44436aa27..c72099cd65 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java @@ -185,7 +185,7 @@ public final class CognitoMessageDispatcher { private void sendEmail(UserPool pool, String region, String email, String subject, String body) { Sender sender = sender(pool, region); - ses.sendEmail(SendEmailRequest.builder() + ses.sendServiceEmail(SendEmailRequest.builder() .source(sender.from()) // SES takes Source as the default return path, the envelope sender it records // and hands to the SMTP relay, which takes a bare address. diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java index 5800a619b9..e9798ea59e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityService.java @@ -172,7 +172,12 @@ public class SesIdentityService implements SesTaggable { return refreshIdentityState(identity, region); } - /** True when the address itself or its parent domain is a verified identity. */ + /** + * True when the address itself, its domain or one of that domain's parents is a verified + * identity: "if you create and verify an identity for example.com, you don't need to create + * separate identities for a.example.com, a.b.example.com, user@example.com, user@a.example.com". + * The walk stops before a bare top-level domain. + */ public boolean isVerifiedSender(String fromEmail, String region) { if (fromEmail == null) { return false; @@ -181,8 +186,20 @@ public class SesIdentityService implements SesTaggable { return true; } int at = fromEmail.indexOf('@'); - return at >= 0 && at < fromEmail.length() - 1 - && isIdentityVerified(fromEmail.substring(at + 1), region); + if (at < 0 || at == fromEmail.length() - 1) { + return false; + } + String domain = fromEmail.substring(at + 1); + while (true) { + if (isIdentityVerified(domain, region)) { + return true; + } + int dot = domain.indexOf('.'); + if (dot < 0 || domain.indexOf('.', dot + 1) < 0) { + return false; + } + domain = domain.substring(dot + 1); + } } boolean isIdentityVerified(String identity, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java index c2c933898e..0405e2f9ae 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java @@ -271,6 +271,8 @@ public class SesQueryHandler { .bccAddresses(bccAddresses) .replyToAddresses(replyToAddresses) .returnPath(returnPath) + .sourceArn(getParam(params, "SourceArn")) + .returnPathArn(getParam(params, "ReturnPathArn")) .configurationSetName(configurationSetName) .emailTags(emailTags) .region(region) @@ -295,6 +297,9 @@ public class SesQueryHandler { String messageId = sesService.sendEmail(SendEmailRequest.builder() .source(source) .toAddresses(destinations) + .sourceArn(getParam(params, "SourceArn")) + .fromArn(getParam(params, "FromArn")) + .returnPathArn(getParam(params, "ReturnPathArn")) .configurationSetName(configurationSetName) .emailTags(emailTags) .region(region) @@ -642,6 +647,8 @@ public class SesQueryHandler { .bccAddresses(bccAddresses) .replyToAddresses(replyToAddresses) .returnPath(returnPath) + .sourceArn(getParam(params, "SourceArn")) + .returnPathArn(getParam(params, "ReturnPathArn")) .configurationSetName(configurationSetName) .emailTags(emailTags) .region(region) diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java index dc2a80fcf8..4d7c5e25bd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java @@ -108,6 +108,9 @@ public class SesSendController { List replyToAddresses = addressList(request, "ReplyToAddresses"); String feedbackForwardingAddress = request.path("FeedbackForwardingEmailAddress").asText(null); + // "For Raw emails, the FromEmailAddressIdentityArn value overrides the X-SES-SOURCE-ARN + // and X-SES-FROM-ARN headers", so it stands for both ARNs of a raw send. + String fromIdentityArn = request.path("FromEmailAddressIdentityArn").asText(null); String configurationSetName = request.path("ConfigurationSetName").asText(null); String tenantName = stringMemberOrAbsent(request, "TenantName"); List emailTags = parseEmailTagsArray(request.path("EmailTags"), "EmailTags"); @@ -121,6 +124,9 @@ public class SesSendController { .bccAddresses(bccAddresses) .replyToAddresses(replyToAddresses) .returnPath(feedbackForwardingAddress) + .sourceArn(fromIdentityArn) + .fromArn(fromIdentityArn) + .returnPathArn(request.path("FeedbackForwardingEmailAddressIdentityArn").asText(null)) .configurationSetName(configurationSetName) .emailTags(emailTags) .listManagement(listManagement) diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index b126b42b5f..2ceba294da 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -209,6 +209,10 @@ public class SesService { "tenant", tenantService); } + private String callerAccountId() { + return regionResolver != null ? regionResolver.getAccountId() : defaultAccountId; + } + /** * v2 CreateEmailIdentity. The identity domain builds and persists the complete record in one * write; only the configuration-set existence check is cross-domain, so it is passed in as the @@ -226,7 +230,7 @@ public class SesService { if (identityValue == null || identityValue.isBlank()) { return; } - String accountId = regionResolver != null ? regionResolver.getAccountId() : defaultAccountId; + String accountId = callerAccountId(); tenantService.deleteBackingResource(SesTenantService.RESOURCE_TYPE_IDENTITY, identityValue, region, () -> certificateService.deleteIdentityGuarded(identityValue, accountId, region, () -> doDeleteIdentity(identityValue, region))); @@ -242,9 +246,30 @@ public class SesService { LOG.infov("Deleted identity: {0}", identityValue); } + /** + * The SES send operations: v1 {@code SendEmail}, {@code SendRawEmail} and + * {@code SendTemplatedEmail}, and v2 {@code SendEmail}. The Source, the return path and, on a raw + * send, every From mailbox the send reads, the Sender and the Return-Path header must be verified + * identities, the address itself, its domain or a parent domain, as on AWS. An address that a + * sending-authorization ARN puts under another account's identity is not checked. + */ public String sendEmail(SendEmailRequest request) { + return sendEmail(request, true); + } + + /** + * A message another service sends to its users, such as a Cognito verification code or + * invitation. Its sender is not checked against the account's identities: Cognito's default + * sender, {@code no-reply@verificationemail.com}, belongs to an SES account Cognito owns, and a + * pool's {@code EmailConfiguration} sender is used only once Cognito has found it verified. + */ + public String sendServiceEmail(SendEmailRequest request) { + return sendEmail(request, false); + } + + private String sendEmail(SendEmailRequest request, boolean requireVerifiedSender) { if (request.content() instanceof EmailContent.Raw raw) { - return sendRawEmail(request, raw); + return sendRawEmail(request, raw, requireVerifiedSender); } if (request.content() instanceof EmailContent.InlineTemplate inline) { requireInlineTemplateContent(inline.subject(), inline.textPart(), inline.htmlPart()); @@ -263,9 +288,80 @@ public class SesService { case EmailContent.InlineTemplate inline -> SesTemplateService.render(inline); case EmailContent.Raw raw -> throw new IllegalStateException("Raw content is sent above"); }; + if (requireVerifiedSender) { + requireVerifiedSenders(List.of( + new SendingAddress(request.source(), request.sourceArn()), + new SendingAddress(request.returnPath(), request.returnPathArn())), request.region()); + } return sendSimpleEmail(request.toBuilder().content(content).build(), content, effectiveConfigSet); } + /** + * An address a send uses as a sender, and the sending-authorization ARN of the identity that + * covers it, when the request names one. + */ + private record SendingAddress(String address, String identityArn) { + } + + // "The message must be sent from a verified email address or domain" (API_SendEmail), and the + // error "could apply to the "From", "Source", "Sender", or "Return-Path" address" (developer + // guide, Email sending errors). It runs after the request's own validation so that + // probe-confirmed order is unchanged; where AWS places this check among those errors has not + // been probed. + private void requireVerifiedSenders(List senders, String region) { + String accountId = callerAccountId(); + // A blank address is not used, and neither is the null reverse-path "<>". + List unverified = senders.stream() + .filter(sender -> sender.address() != null) + .filter(sender -> !isAnotherAccountsIdentity(sender.identityArn(), accountId)) + .map(sender -> extractEmailAddress(sender.address())) + .filter(address -> !address.isEmpty()) + .filter(address -> !identityService.isVerifiedSender(address, region)) + .distinct() + .toList(); + if (!unverified.isEmpty()) { + throw new AwsException("MessageRejected", + "Email address is not verified. The following identities failed the check in region " + + region.toUpperCase(Locale.ROOT) + ": " + String.join(", ", unverified), 400); + } + } + + // Sending authorization: an ARN naming another account's identity puts the address under that + // account's identity and policy. Floci evaluates neither, so it lets the delegate through. An + // ARN of the caller's own account, or one that names no SES identity, changes nothing. + private static boolean isAnotherAccountsIdentity(String identityArn, String accountId) { + return AwsArnUtils.resourceIfArnFor(identityArn, "ses") + .filter(resource -> resource.startsWith("identity/")) + .isPresent() + && AwsArnUtils.isForeignAccount(AwsArnUtils.parse(identityArn), accountId); + } + + // A raw send's senders. The From header counts wherever the send reads it: always on v1, and on + // v2 only in place of a missing FromEmailAddress (a v2 send with one accepts any From header, + // probe-confirmed). An ARN passed as a parameter wins over its X-SES-*-ARN header, and the + // source ARN also covers the From and return-path addresses that have no ARN of their own ("If + // you only specify the SourceArn, Amazon SES sets the "From" address and the "Return Path" + // addresses to the identity that you specified in SourceArn"). AWS names no ARN for the Sender. + // Whether AWS still checks a Return-Path header that the request's return path overrides has + // not been probed; every address present is checked. + private static List rawSendingAddresses(SendEmailRequest request, EmailContent.Raw raw, + SmtpRelay.RawMessageHeaders headers) { + String sourceArn = firstNonBlank(request.sourceArn(), headers.sourceArn()); + String fromArn = firstNonBlank(request.fromArn(), headers.fromArn(), sourceArn); + String returnPathArn = firstNonBlank(request.returnPathArn(), headers.returnPathArn(), sourceArn); + List senders = new ArrayList<>(); + senders.add(new SendingAddress(request.source(), sourceArn)); + if (raw.checksFromHeader(request.hasSource())) { + for (String from : headers.fromAddresses()) { + senders.add(new SendingAddress(from, fromArn)); + } + } + senders.add(new SendingAddress(headers.sender(), null)); + senders.add(new SendingAddress(request.returnPath(), returnPathArn)); + senders.add(new SendingAddress(headers.returnPath(), returnPathArn)); + return senders; + } + private static EmailContent.Simple renderInlineTemplate(EmailContent.InlineTemplate template) { requireInlineTemplateContent(template.subject(), template.textPart(), template.htmlPart()); return SesTemplateService.render(template); @@ -420,7 +516,7 @@ public class SesService { return List.copyOf(out); } - private String sendRawEmail(SendEmailRequest request, EmailContent.Raw raw) { + private String sendRawEmail(SendEmailRequest request, EmailContent.Raw raw, boolean requireVerifiedSender) { String source = request.source(); String region = request.region(); if (raw.data() == null || raw.data().isBlank()) { @@ -469,6 +565,9 @@ public class SesService { } SesAddressSyntax.requireFallbackFromAt(request, raw, parsed.message()); SesAddressLength.requireRaw(request, raw, parsed.message()); + if (requireVerifiedSender) { + requireVerifiedSenders(rawSendingAddresses(request, raw, headers), region); + } // 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 suppressedReasons = new LinkedHashMap<>( @@ -570,7 +669,7 @@ public class SesService { Instant timestamp = Instant.now(); // Report the caller's account (resolved per request) in the event payload and source ARN, // falling back to the default account outside a request context (e.g. unit tests). - String sendingAccountId = regionResolver != null ? regionResolver.getAccountId() : defaultAccountId; + String sendingAccountId = callerAccountId(); String sourceArn = (source == null || source.isBlank()) ? null : AwsArnUtils.Arn.of("ses", region, sendingAccountId, @@ -1265,7 +1364,7 @@ public class SesService { * mismatched or the resource doesn't exist anywhere. */ private void requireCallerAccount(ResourceRef ref) { - String callerAccountId = regionResolver != null ? regionResolver.getAccountId() : defaultAccountId; + String callerAccountId = callerAccountId(); if (!ref.account().equals(callerAccountId)) { throw new AwsException("BadRequestException", "Operations on a resource created in a different account is not allowed", 400); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SmtpRelay.java b/src/main/java/io/github/hectorvent/floci/services/ses/SmtpRelay.java index d11d29ce4a..773c918ca4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SmtpRelay.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SmtpRelay.java @@ -69,6 +69,9 @@ public class SmtpRelay { private static final String X_SES_PREFIX = "x-ses-"; private static final String HEADER_RETURN_PATH = "Return-Path"; + private static final String HEADER_SOURCE_ARN = "X-SES-SOURCE-ARN"; + private static final String HEADER_FROM_ARN = "X-SES-FROM-ARN"; + private static final String HEADER_RETURN_PATH_ARN = "X-SES-RETURN-PATH-ARN"; private static final String MESSAGE_ID_DOMAIN = "email.amazonses.com"; private static final String DEFAULT_ATTACHMENT_CONTENT_TYPE = "application/octet-stream"; @@ -681,18 +684,23 @@ public class SmtpRelay { // ──────────────────────────── Raw header parsing ──────────────────────────── /** - * Structured headers extracted from a raw RFC 5322 MIME message: the From address, subject, - * separate To / Cc / Bcc address lists, the {@code Return-Path} envelope sender, and the - * {@code X-SES-CONFIGURATION-SET} / {@code X-SES-MESSAGE-TAGS} control headers. Empty fields - * when the corresponding header is missing. + * Structured headers extracted from a raw RFC 5322 MIME message: the first From address, the + * subject, separate To / Cc / Bcc address lists, the {@code Return-Path} envelope sender, the + * {@code X-SES-CONFIGURATION-SET} / {@code X-SES-MESSAGE-TAGS} control headers, every + * mailbox the From header names, the Sender mailbox, and the sending-authorization headers + * {@code X-SES-SOURCE-ARN}, {@code X-SES-FROM-ARN} and {@code X-SES-RETURN-PATH-ARN}. Of a + * header given more than once, only the first is read, as SES does for the X-headers. Empty + * fields when the corresponding header is missing. */ public record RawMessageHeaders(String from, String subject, List to, List cc, List bcc, String returnPath, String configurationSet, - List messageTags) { + List messageTags, List fromAddresses, + String sender, String sourceArn, String fromArn, + String returnPathArn) { public static RawMessageHeaders empty() { return new RawMessageHeaders("", "", List.of(), List.of(), List.of(), - "", "", List.of()); + "", "", List.of(), List.of(), "", "", "", ""); } } @@ -748,10 +756,15 @@ public class SmtpRelay { String configurationSet = headerValue(message, HEADER_CONFIGURATION_SET); List messageTags = parseMessageTagsHeader( headerValue(message, HEADER_MESSAGE_TAGS)); + Mailbox sender = message.getSender(); return new RawMessageHeaders(from.isEmpty() ? "" : from.get(0), subject, to, cc, bcc, returnPath == null ? "" : returnPath, configurationSet == null ? "" : configurationSet, - messageTags); + messageTags, from, + sender == null ? "" : sender.getAddress(), + headerValueOrEmpty(message, HEADER_SOURCE_ARN), + headerValueOrEmpty(message, HEADER_FROM_ARN), + headerValueOrEmpty(message, HEADER_RETURN_PATH_ARN)); } catch (Exception ignored) { // A message whose addresses cannot be read is treated like one without headers. return RawMessageHeaders.empty(); @@ -807,6 +820,11 @@ public class SmtpRelay { return value.isEmpty() ? null : value; } + private static String headerValueOrEmpty(Entity entity, String name) { + String value = headerValue(entity, name); + return value == null ? "" : value; + } + /** Unwraps the {@code } form used by Return-Path and friends. */ static String extractAngleAddress(String value) { if (value == null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/SendEmailRequest.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/SendEmailRequest.java index 83fc2ac663..f047a1b7a4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/SendEmailRequest.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/SendEmailRequest.java @@ -10,7 +10,9 @@ import java.util.Objects; * controller and Cognito, and rebuilt inside SesService whenever the content is rendered or * sanitized, so the request always carries the content being sent. It is not a wire shape: each * caller translates its own request into these fields (v2 {@code FromEmailAddress} into - * {@code source}, {@code FeedbackForwardingEmailAddress} into {@code returnPath}, v1 {@code Tags} + * {@code source}, {@code FeedbackForwardingEmailAddress} into {@code returnPath}, v2 + * {@code FromEmailAddressIdentityArn} into both {@code sourceArn} and {@code fromArn}, + * {@code FeedbackForwardingEmailAddressIdentityArn} into {@code returnPathArn}, v1 {@code Tags} * into {@code emailTags}, v1 {@code SendRawEmail} {@code Destinations} into {@code toAddresses}), * and a field a caller's API lacks keeps the builder default. */ @@ -21,6 +23,9 @@ public record SendEmailRequest( List bccAddresses, List replyToAddresses, String returnPath, + String sourceArn, + String fromArn, + String returnPathArn, String configurationSetName, List emailTags, ListManagementOptions listManagement, @@ -70,6 +75,9 @@ public record SendEmailRequest( .bccAddresses(bccAddresses) .replyToAddresses(replyToAddresses) .returnPath(returnPath) + .sourceArn(sourceArn) + .fromArn(fromArn) + .returnPathArn(returnPathArn) .configurationSetName(configurationSetName) .emailTags(emailTags) .listManagement(listManagement) @@ -85,6 +93,9 @@ public record SendEmailRequest( private List bccAddresses = List.of(); private List replyToAddresses = List.of(); private String returnPath; + private String sourceArn; + private String fromArn; + private String returnPathArn; private String configurationSetName; private List emailTags = List.of(); private ListManagementOptions listManagement; @@ -101,6 +112,9 @@ public record SendEmailRequest( public Builder bccAddresses(List bccAddresses) { this.bccAddresses = bccAddresses; return this; } public Builder replyToAddresses(List replyToAddresses) { this.replyToAddresses = replyToAddresses; return this; } public Builder returnPath(String returnPath) { this.returnPath = returnPath; return this; } + public Builder sourceArn(String sourceArn) { this.sourceArn = sourceArn; return this; } + public Builder fromArn(String fromArn) { this.fromArn = fromArn; return this; } + public Builder returnPathArn(String returnPathArn) { this.returnPathArn = returnPathArn; return this; } public Builder configurationSetName(String configurationSetName) { this.configurationSetName = configurationSetName; return this; } public Builder emailTags(List emailTags) { this.emailTags = emailTags; return this; } public Builder listManagement(ListManagementOptions listManagement) { this.listManagement = listManagement; return this; } @@ -110,7 +124,8 @@ public record SendEmailRequest( public SendEmailRequest build() { return new SendEmailRequest(source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, - returnPath, configurationSetName, emailTags, listManagement, tenantName, region, content); + returnPath, sourceArn, fromArn, returnPathArn, configurationSetName, emailTags, listManagement, + tenantName, region, content); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java index 8709130e22..5a6767aef7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java @@ -167,7 +167,7 @@ class CognitoLambdaTriggersTest { private static void assertVerificationEmail(SesService ses, String to, String subject, String body) { ArgumentCaptor captor = ArgumentCaptor.forClass(SendEmailRequest.class); - verify(ses).sendEmail(captor.capture()); + verify(ses).sendServiceEmail(captor.capture()); SendEmailRequest sent = captor.getValue(); assertEquals(SendEmailRequest.builder() .source(sent.source()) diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java index e93cfab3cb..c81627c9a4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java @@ -561,7 +561,7 @@ class CognitoMessageDispatcherTest { @Test void dispatchInvitation_failureOnTheOnlyMedium_isRethrown() { - when(ses.sendEmail(any())).thenThrow(new IllegalStateException("SES down")); + when(ses.sendServiceEmail(any())).thenThrow(new IllegalStateException("SES down")); assertThrows(IllegalStateException.class, () -> dispatcher.dispatchInvitation(pool(Map.of()), invitee("alice@example.com", null), "Temp1234!", List.of("EMAIL"), null)); @@ -587,7 +587,7 @@ class CognitoMessageDispatcherTest { private SendEmailRequest sentEmail() { ArgumentCaptor captor = ArgumentCaptor.forClass(SendEmailRequest.class); - verify(ses).sendEmail(captor.capture()); + verify(ses).sendServiceEmail(captor.capture()); return captor.getValue(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetSendingOptionsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetSendingOptionsV2IntegrationTest.java index 80805c3a79..0d66d2acfd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetSendingOptionsV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetSendingOptionsV2IntegrationTest.java @@ -26,7 +26,23 @@ class SesConfigurationSetSendingOptionsV2IntegrationTest { private static final String SES_AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request"; private static final String CS = "cs-sending-options"; - private static final String SENDER = "sender@example.com"; + // A sender of its own, verified below: the identity outlives this class, and other SES classes + // that share the application create sender@example.com themselves. + private static final String SENDER = "cs-sending-options-sender@floci.test"; + + @Test + @Order(0) + void verifySender() { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", SES_AUTH) + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", SENDER) + .when() + .post("/") + .then() + .statusCode(200); + } @Test @Order(1) diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetTrackingOptionsV1IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetTrackingOptionsV1IntegrationTest.java index 7249c3267b..6bfea56896 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetTrackingOptionsV1IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetTrackingOptionsV1IntegrationTest.java @@ -7,8 +7,6 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; -import java.util.List; - import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -33,75 +31,6 @@ class SesConfigurationSetTrackingOptionsV1IntegrationTest { private static final String DOMAIN = "track-v1.floci.test"; private static final String CS = "v1-cs-tracking"; - private static void verifyDomainIdentityViaRoute53(String domain, String callerReference) { - List tokens = given() - .contentType("application/json") - .header("Authorization", AUTH_V2) - .body("{\"EmailIdentity\": \"" + domain + "\"}") - .when() - .post("/v2/email/identities") - .then() - .statusCode(200) - .extract() - .jsonPath() - .getList("DkimAttributes.Tokens", String.class); - - String locationHeader = given() - .contentType("application/xml") - .body(""" - - - floci.test - """ + callerReference + """ - - - """) - .when() - .post("/2013-04-01/hostedzone") - .then() - .statusCode(201) - .extract() - .header("Location"); - - String zoneId = locationHeader.substring(locationHeader.lastIndexOf('/') + 1); - StringBuilder body = new StringBuilder(""" - - - - - """); - for (String token : tokens) { - body.append(""" - - CREATE - - """).append(token).append("._domainkey.").append(domain).append(".\n") - .append(""" - CNAME - 300 - - """).append(token).append(".dkim.amazonses.com.\n") - .append(""" - - - - """); - } - body.append(""" - - - - """); - - given() - .contentType("application/xml") - .body(body.toString()) - .when() - .post("/2013-04-01/hostedzone/" + zoneId + "/rrset") - .then() - .statusCode(200); - } - private static RequestSpecification query(String action) { return given() .contentType("application/x-www-form-urlencoded") @@ -112,7 +41,7 @@ class SesConfigurationSetTrackingOptionsV1IntegrationTest { @Test @Order(1) void setup_verifyDomain_andCreateConfigSet() { - verifyDomainIdentityViaRoute53(DOMAIN, "v1-cs-tracking"); + SesDomainIdentityTestHelper.createVerified(DOMAIN, AUTH_V2); query("CreateConfigurationSet").formParam("ConfigurationSet.Name", CS) .when().post("/").then().statusCode(200); } @@ -172,7 +101,7 @@ class SesConfigurationSetTrackingOptionsV1IntegrationTest { @Test @Order(6) void updateTrackingOptions_changesDomain() { - verifyDomainIdentityViaRoute53("other." + DOMAIN, "v1-cs-tracking-other"); + SesDomainIdentityTestHelper.createVerified("other." + DOMAIN, AUTH_V2); query("UpdateConfigurationSetTrackingOptions") .formParam("ConfigurationSetName", CS) .formParam("TrackingOptions.CustomRedirectDomain", "other." + DOMAIN) diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java index 09a24fdd83..33672ce982 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java @@ -6,8 +6,6 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; -import java.util.List; - import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; @@ -24,75 +22,6 @@ class SesConfigurationSetV2IntegrationTest { private static final String AUTH_HEADER = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request"; - private static void verifyDomainIdentityViaRoute53(String domain, String callerReference) { - List tokens = given() - .contentType("application/json") - .header("Authorization", AUTH_HEADER) - .body("{\"EmailIdentity\": \"" + domain + "\"}") - .when() - .post("/v2/email/identities") - .then() - .statusCode(200) - .extract() - .jsonPath() - .getList("DkimAttributes.Tokens", String.class); - - String locationHeader = given() - .contentType("application/xml") - .body(""" - - - floci.test - """ + callerReference + """ - - - """) - .when() - .post("/2013-04-01/hostedzone") - .then() - .statusCode(201) - .extract() - .header("Location"); - - String zoneId = locationHeader.substring(locationHeader.lastIndexOf('/') + 1); - StringBuilder body = new StringBuilder(""" - - - - - """); - for (String token : tokens) { - body.append(""" - - CREATE - - """).append(token).append("._domainkey.").append(domain).append(".\n") - .append(""" - CNAME - 300 - - """).append(token).append(".dkim.amazonses.com.\n") - .append(""" - - - - """); - } - body.append(""" - - - - """); - - given() - .contentType("application/xml") - .body(body.toString()) - .when() - .post("/2013-04-01/hostedzone/" + zoneId + "/rrset") - .then() - .statusCode(200); - } - @Test @Order(1) void createConfigurationSet() { @@ -837,7 +766,7 @@ class SesConfigurationSetV2IntegrationTest { @Test @Order(37) void putTrackingOptions_verifiedDomain_echoed() { - verifyDomainIdentityViaRoute53("track.floci.test", "v2-cs-tracking"); + SesDomainIdentityTestHelper.createVerified("track.floci.test", AUTH_HEADER); putConfigSet("v2-cs-tracking"); given().contentType("application/json").header("Authorization", AUTH_HEADER) .body("{\"CustomRedirectDomain\": \"track.floci.test\", \"HttpsPolicy\": \"REQUIRE\"}") @@ -867,7 +796,7 @@ class SesConfigurationSetV2IntegrationTest { void putTrackingOptions_invalidHttpsPolicy_returns400() { // AWS validates the HttpsPolicy enum only after CustomRedirectDomain // presence and verification, so the domain must be verified to reach it. - verifyDomainIdentityViaRoute53("track-enum.floci.test", "v2-cs-tracking-enum"); + SesDomainIdentityTestHelper.createVerified("track-enum.floci.test", AUTH_HEADER); putConfigSet("v2-cs-tracking-enum"); given().contentType("application/json").header("Authorization", AUTH_HEADER) .body("{\"CustomRedirectDomain\": \"track-enum.floci.test\", \"HttpsPolicy\": \"BOGUS\"}") @@ -1053,7 +982,7 @@ class SesConfigurationSetV2IntegrationTest { void putTrackingOptions_emptyBody_clears() { // An empty PUT body clears tracking options rather than persisting an // empty block that GetConfigurationSet would echo as {}. - verifyDomainIdentityViaRoute53("clear.floci.test", "v2-cs-tracking-clear"); + SesDomainIdentityTestHelper.createVerified("clear.floci.test", AUTH_HEADER); putConfigSet("v2-cs-tracking-clear"); given().contentType("application/json").header("Authorization", AUTH_HEADER) .body("{\"CustomRedirectDomain\": \"clear.floci.test\", \"HttpsPolicy\": \"REQUIRE\"}") diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesDomainIdentityTestHelper.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesDomainIdentityTestHelper.java new file mode 100644 index 0000000000..ee3d856cd5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesDomainIdentityTestHelper.java @@ -0,0 +1,83 @@ +package io.github.hectorvent.floci.services.ses; + +import java.util.List; +import java.util.UUID; + +import static io.restassured.RestAssured.given; + +/** + * Creates a domain identity that is verified for sending. A domain identity stays Pending until + * its DKIM CNAME records exist in a Route 53 hosted zone, so this publishes them in a zone of its + * own. A test that checks the Pending state first creates the identity itself and then calls + * {@link #publishDkimRecords}. + */ +final class SesDomainIdentityTestHelper { + + private SesDomainIdentityTestHelper() { + } + + static void createVerified(String domain, String sesAuthorization) { + List tokens = given() + .contentType("application/json") + .header("Authorization", sesAuthorization) + .body("{\"EmailIdentity\": \"" + domain + "\"}") + .when() + .post("/v2/email/identities") + .then() + .statusCode(200) + .extract() + .jsonPath() + .getList("DkimAttributes.Tokens", String.class); + publishDkimRecords(domain, tokens); + } + + static void publishDkimRecords(String domain, List tokens) { + String location = given() + .contentType("application/xml") + .body(""" + + + %s + %s + + """.formatted(domain, UUID.randomUUID())) + .when() + .post("/2013-04-01/hostedzone") + .then() + .statusCode(201) + .extract() + .header("Location"); + + StringBuilder changes = new StringBuilder(); + for (String token : tokens) { + changes.append(""" + + CREATE + + %s._domainkey.%s. + CNAME + 300 + + %s.dkim.amazonses.com. + + + + """.formatted(token, domain, token)); + } + given() + .contentType("application/xml") + .body(""" + + + + + %s + + + """.formatted(changes)) + .when() + .post("/2013-04-01/hostedzone/" + location.substring(location.lastIndexOf('/') + 1) + "/rrset") + .then() + .statusCode(200); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java index 00746a293c..8b4fcb55cb 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEventPublishingV2IntegrationTest.java @@ -1304,6 +1304,15 @@ class SesEventPublishingV2IntegrationTest { .then() .statusCode(200); + given() + .contentType("application/json") + .header("Authorization", sesAuth) + .body("{\"EmailIdentity\":\"" + SENDER + "\"}") + .when() + .post("/v2/email/identities") + .then() + .statusCode(200); + given() .contentType("application/json") .header("Authorization", sesAuth) diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityNotificationTopicPublishV1IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityNotificationTopicPublishV1IntegrationTest.java index 1f0ff1b92d..ecc8fce37d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityNotificationTopicPublishV1IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityNotificationTopicPublishV1IntegrationTest.java @@ -119,6 +119,9 @@ class SesIdentityNotificationTopicPublishV1IntegrationTest { verifyDomainIdentity(FALLBACK_DOMAIN); setIdentityNotificationTopic(FALLBACK_DOMAIN, "Bounce", topicArn); + // The domain stays Pending without its DKIM records, so the address under it is verified + // on its own to be allowed to send; it has no topic of its own. + verifyEmailIdentity("anyone@" + FALLBACK_DOMAIN); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java index 839567dbe5..03c60c3d0a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java @@ -16,6 +16,10 @@ import static org.hamcrest.Matchers.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class SesIntegrationTest { + // sender@example.com is deleted at @Order(20), and the domain test after it expects no + // identity under example.com, so the later sends use a sender of their own. + private static final String INSPECTION_SENDER = "inspection-sender@floci.test"; + private static String authorization(String service) { return authorization(service, "us-east-1"); } @@ -24,6 +28,18 @@ class SesIntegrationTest { return "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/" + service + "/aws4_request"; } + private static void verifySender(String address) { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", authorization("email")) + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", address) + .when() + .post("/") + .then() + .statusCode(200); + } + @Test @Order(1) void verifyEmailIdentity() { @@ -222,13 +238,14 @@ class SesIntegrationTest { // Regression for https://github.com/floci-io/floci/issues/797 // AWS SES SendRawEmail allows omitting the Source parameter when the // raw MIME message contains a From: header. + verifySender(INSPECTION_SENDER); given() .contentType("application/x-www-form-urlencoded") .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request") .formParam("Action", "SendRawEmail") .formParam("Destinations.member.1", "recipient@example.com") .formParam("RawMessage.Data", - "From: sender@example.com\r\nTo: recipient@example.com\r\nSubject: hello\r\n\r\nbody\r\n") + "From: " + INSPECTION_SENDER + "\r\nTo: recipient@example.com\r\nSubject: hello\r\n\r\nbody\r\n") .when() .post("/") .then() @@ -421,13 +438,14 @@ class SesIntegrationTest { @Test @Order(21) void sendEmailV1_replyToAddressesStoredInInspection() { + verifySender(INSPECTION_SENDER); given().delete("/_aws/ses").then().statusCode(200); given() .contentType("application/x-www-form-urlencoded") .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request") .formParam("Action", "SendEmail") - .formParam("Source", "sender@example.com") + .formParam("Source", INSPECTION_SENDER) .formParam("Destination.ToAddresses.member.1", "recipient@example.com") .formParam("ReplyToAddresses.member.1", "reply@example.com") .formParam("Message.Subject.Data", "V1 ReplyTo") @@ -449,15 +467,17 @@ class SesIntegrationTest { @Test @Order(21) void sendEmailV1_returnPathStoredInInspection() { + verifySender(INSPECTION_SENDER); + verifySender("inspection-bounces@floci.test"); given().delete("/_aws/ses").then().statusCode(200); given() .contentType("application/x-www-form-urlencoded") .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request") .formParam("Action", "SendEmail") - .formParam("Source", "sender@example.com") + .formParam("Source", INSPECTION_SENDER) .formParam("Destination.ToAddresses.member.1", "recipient@example.com") - .formParam("ReturnPath", "bounces@example.com") + .formParam("ReturnPath", "inspection-bounces@floci.test") .formParam("Message.Subject.Data", "V1 ReturnPath") .formParam("Message.Body.Text.Data", "body") .when() @@ -470,17 +490,19 @@ class SesIntegrationTest { .get("/_aws/ses") .then() .statusCode(200) - .body("messages[0].ReturnPath", equalTo("bounces@example.com")); + .body("messages[0].ReturnPath", equalTo("inspection-bounces@floci.test")); } @Test @Order(21) void sendRawEmailV1_returnPathHeaderStoredInInspection() { + verifySender(INSPECTION_SENDER); + verifySender("inspection-mime-bounces@floci.test"); given().delete("/_aws/ses").then().statusCode(200); - String raw = "From: sender@example.com\r\n" + String raw = "From: " + INSPECTION_SENDER + "\r\n" + "To: recipient@example.com\r\n" - + "Return-Path: \r\n" + + "Return-Path: \r\n" + "Subject: raw-return-path\r\n\r\nbody"; String rawB64 = java.util.Base64.getEncoder().encodeToString( raw.getBytes(java.nio.charset.StandardCharsets.UTF_8)); @@ -489,7 +511,7 @@ class SesIntegrationTest { .contentType("application/x-www-form-urlencoded") .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request") .formParam("Action", "SendRawEmail") - .formParam("Source", "sender@example.com") + .formParam("Source", INSPECTION_SENDER) .formParam("Destinations.member.1", "recipient@example.com") .formParam("RawMessage.Data", rawB64) .when() @@ -502,7 +524,7 @@ class SesIntegrationTest { .get("/_aws/ses") .then() .statusCode(200) - .body("messages[0].ReturnPath", equalTo("mime-bounces@example.com")); + .body("messages[0].ReturnPath", equalTo("inspection-mime-bounces@floci.test")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesListManagementOptionsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesListManagementOptionsV2IntegrationTest.java index 52334d4051..372815c3a8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesListManagementOptionsV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesListManagementOptionsV2IntegrationTest.java @@ -24,6 +24,15 @@ class SesListManagementOptionsV2IntegrationTest { private static final String LIST = "lmo-newsletter"; private static final String FROM = "lmo-sender@floci.test"; + @Test + @Order(0) + void setup_verifySender() { + given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH) + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", FROM) + .when().post("/").then().statusCode(200); + } + @Test @Order(0) void setup_contactList() { diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesSendAddressSyntaxIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendAddressSyntaxIntegrationTest.java index 07741438d2..03347d41a6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesSendAddressSyntaxIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendAddressSyntaxIntegrationTest.java @@ -20,6 +20,21 @@ 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"; + // A sender of its own: an identity under example.com would outlive this class and reach the + // other SES classes that share the application. + private static final String VERIFIED_SENDER = "syntax-sender@floci.test"; + + private static void verifySender() { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH) + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", VERIFIED_SENDER) + .when() + .post("/") + .then() + .statusCode(200); + } @Test void v1SendEmail_sourceWithoutAt_returnsInvalidParameterValue() { @@ -302,16 +317,17 @@ class SesSendAddressSyntaxIntegrationTest { @ParameterizedTest @ValueSource(strings = {"sender.example.com", "sender@", "やまだ@example.com", "sender@例え.jp"}) void v2SendEmailRaw_malformedFromHeaderIsNotCheckedWithFromEmailAddress(String from) { + verifySender(); given() .contentType("application/json; charset=UTF-8") .header("Authorization", AUTH) .body(""" { - "FromEmailAddress": "sender@example.com", + "FromEmailAddress": "%s", "Destination": {"ToAddresses": ["%s"]}, "Content": {"Raw": {"Data": "%s"}} } - """.formatted(TO, rawMessage(from))) + """.formatted(VERIFIED_SENDER, TO, rawMessage(from))) .when() .post("/v2/email/outbound-emails") .then() @@ -322,17 +338,18 @@ class SesSendAddressSyntaxIntegrationTest { @Test void v2SendEmailRaw_overLimitFromHeaderIsNotMeasuredWithFromEmailAddress() { String from = "A".repeat(321 - " ".length()) + " "; + verifySender(); given() .contentType("application/json; charset=UTF-8") .header("Authorization", AUTH) .body(""" { - "FromEmailAddress": "sender@example.com", + "FromEmailAddress": "%s", "Destination": {"ToAddresses": ["%s"]}, "Content": {"Raw": {"Data": "%s"}} } - """.formatted(TO, rawMessage(from))) + """.formatted(VERIFIED_SENDER, TO, rawMessage(from))) .when() .post("/v2/email/outbound-emails") .then() diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesSendRawNoDestinationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendRawNoDestinationIntegrationTest.java index e093823564..db6f2ec3d5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesSendRawNoDestinationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendRawNoDestinationIntegrationTest.java @@ -17,6 +17,7 @@ class SesSendRawNoDestinationIntegrationTest { private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request"; private static final String TO = "success@simulator.amazonses.com"; + private static final String VERIFIED_SENDER = "no-destination-sender@floci.test"; private static String rawData(String headers) { return Base64.getEncoder().encodeToString( @@ -25,15 +26,25 @@ class SesSendRawNoDestinationIntegrationTest { @Test void v2SendEmail_rawWithoutDestination_takesRecipientsFromHeaders() { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH) + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", VERIFIED_SENDER) + .when() + .post("/") + .then() + .statusCode(200); + String messageId = given() .contentType("application/json") .header("Authorization", AUTH) .body(""" { - "FromEmailAddress": "sender@example.com", + "FromEmailAddress": "%s", "Content": {"Raw": {"Data": "%s"}} } - """.formatted(rawData("From: sender@example.com\r\nTo: " + TO + "\r\n"))) + """.formatted(VERIFIED_SENDER, rawData("From: " + VERIFIED_SENDER + "\r\nTo: " + TO + "\r\n"))) .when() .post("/v2/email/outbound-emails") .then() diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesSendVerifiedSenderIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendVerifiedSenderIntegrationTest.java new file mode 100644 index 0000000000..e2480339e9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesSendVerifiedSenderIntegrationTest.java @@ -0,0 +1,560 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; + +/** + * The send operations require a verified sender, as on AWS: "The message must be sent from a + * verified email address or domain. If you attempt to send email using a non-verified address or + * domain, the operation results in an "Email address not verified" error." + * (SendEmail). + * An address counts as verified when the address itself, its domain or a parent domain is a verified + * identity. The check covers every "From", "Source", "Sender" and "Return-Path" address + * (Email + * sending errors) the send uses, except the ones a sending-authorization ARN puts under another + * account's identity. + */ +@QuarkusTest +class SesSendVerifiedSenderIntegrationTest { + + private static final String AUTH_V1 = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request"; + private static final String AUTH_V2 = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request"; + private static final String UNVERIFIED = "never-verified@unverified-sender.floci.test"; + private static final String VERIFIED = "verified@verified-sender.floci.test"; + private static final String NOT_VERIFIED_MESSAGE = + "Email address is not verified. The following identities failed the check in region US-EAST-1: "; + private static final String TEMPLATE = "verified-sender-check"; + + // Sending authorization: the owner account verifies OWNER_ADDRESS, and the delegate account + // sends from it. + private static final String OWNER_ACCOUNT = "111111111111"; + private static final String DELEGATE_ACCOUNT = "222222222222"; + private static final String OWNER_ADDRESS = "user@owner.floci.test"; + private static final String OWNER_IDENTITY_ARN = + "arn:aws:ses:us-east-1:" + OWNER_ACCOUNT + ":identity/" + OWNER_ADDRESS; + private static final String DELEGATE_IDENTITY_ARN = + "arn:aws:ses:us-east-1:" + DELEGATE_ACCOUNT + ":identity/" + OWNER_ADDRESS; + private static final String DELEGATE_AUTH_V1 = + "AWS4-HMAC-SHA256 Credential=" + DELEGATE_ACCOUNT + "/20260101/us-east-1/email/aws4_request"; + private static final String DELEGATE_AUTH_V2 = + "AWS4-HMAC-SHA256 Credential=" + DELEGATE_ACCOUNT + "/20260101/us-east-1/ses/aws4_request"; + + private static RequestSpecification query(String action) { + return query(AUTH_V1, action); + } + + private static RequestSpecification query(String authorization, String action) { + return given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", authorization) + .formParam("Action", action); + } + + private static RequestSpecification sendEmail(String authorization, String source) { + return query(authorization, "SendEmail") + .formParam("Source", source) + .formParam("Destination.ToAddresses.member.1", "to@example.com") + .formParam("Message.Subject.Data", "s") + .formParam("Message.Body.Text.Data", "b"); + } + + private static RequestSpecification sendRawEmail(String authorization, String rawMessage) { + return query(authorization, "SendRawEmail") + .formParam("Destinations.member.1", "to@example.com") + .formParam("RawMessage.Data", rawMessage); + } + + private static RequestSpecification sendTemplatedEmail(String authorization, String source) { + return query(authorization, "SendTemplatedEmail") + .formParam("Source", source) + .formParam("Destination.ToAddresses.member.1", "to@example.com") + .formParam("Template", TEMPLATE) + .formParam("TemplateData", "{}"); + } + + private static String simpleBody(String from) { + return """ + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}} + } + """.formatted(from); + } + + private static RequestSpecification v2Request(String authorization, String body) { + return given() + .contentType("application/json") + .header("Authorization", authorization) + .body(body); + } + + private static void verifyEmailIdentity(String address) { + query("VerifyEmailIdentity").formParam("EmailAddress", address) + .when().post("/").then().statusCode(200); + } + + private static void verifyOwnerAddress() { + query("AWS4-HMAC-SHA256 Credential=" + OWNER_ACCOUNT + "/20260101/us-east-1/email/aws4_request", + "VerifyEmailIdentity") + .formParam("EmailAddress", OWNER_ADDRESS) + .when().post("/").then().statusCode(200); + } + + private static void createTemplate(String authorization) { + query(authorization, "CreateTemplate") + .formParam("Template.TemplateName", TEMPLATE) + .formParam("Template.SubjectPart", "s") + .formParam("Template.TextPart", "b") + .when() + .post("/"); + } + + /** A base64 raw message from {@code from}, with each of {@code headers} ("Name: value") above it. */ + private static String rawMessage(String from, String... headers) { + StringBuilder raw = new StringBuilder(); + for (String header : headers) { + raw.append(header).append("\r\n"); + } + raw.append("From: ").append(from).append("\r\nTo: to@example.com\r\nSubject: s\r\n\r\nbody"); + return Base64.getEncoder().encodeToString(raw.toString().getBytes(StandardCharsets.UTF_8)); + } + + private static void assertSent(RequestSpecification request, String action) { + request + .when() + .post("/") + .then() + .statusCode(200) + .body(action + "Response." + action + "Result.MessageId", notNullValue()); + } + + private static void assertSentV2(RequestSpecification request) { + request + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200) + .body("MessageId", notNullValue()); + } + + private static void assertMessageRejectedV2(RequestSpecification request, String unverified) { + request + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(400) + .body("__type", equalTo("MessageRejected")) + .body("message", equalTo(NOT_VERIFIED_MESSAGE + unverified)); + } + + private static void assertMessageRejected(RequestSpecification request, String unverified) { + request + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("MessageRejected")) + .body("ErrorResponse.Error.Message", equalTo(NOT_VERIFIED_MESSAGE + unverified)); + } + + private static void assertNothingCapturedFrom(String source) { + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages.Source", everyItem(not(equalTo(source)))); + } + + @Test + void v1SendEmail_unverifiedSource_returnsMessageRejected() { + assertMessageRejected(sendEmail(AUTH_V1, UNVERIFIED), UNVERIFIED); + + assertNothingCapturedFrom(UNVERIFIED); + } + + @Test + void v1SendEmail_displayNameSourceWithVerifiedAddress_succeeds() { + verifyEmailIdentity(VERIFIED); + + assertSent(sendEmail(AUTH_V1, "Verified Sender <" + VERIFIED + ">"), "SendEmail"); + } + + @Test + void v1SendRawEmail_unverifiedSource_returnsMessageRejected() { + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(UNVERIFIED)).formParam("Source", UNVERIFIED), + UNVERIFIED); + + assertNothingCapturedFrom(UNVERIFIED); + } + + @Test + void v1SendRawEmail_withoutSource_unverifiedMimeFrom_returnsMessageRejected() { + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(UNVERIFIED)), UNVERIFIED); + } + + @Test + void v1SendTemplatedEmail_unverifiedSource_returnsMessageRejected() { + createTemplate(AUTH_V1); + + assertMessageRejected(sendTemplatedEmail(AUTH_V1, UNVERIFIED), UNVERIFIED); + } + + @Test + void v2SendEmail_simple_unverifiedFrom_returnsMessageRejected() { + assertMessageRejectedV2(v2Request(AUTH_V2, simpleBody(UNVERIFIED)), UNVERIFIED); + + assertNothingCapturedFrom(UNVERIFIED); + } + + @Test + void v2SendEmail_raw_unverifiedMimeFrom_returnsMessageRejected() { + assertMessageRejectedV2(v2Request(AUTH_V2, """ + { + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Raw": {"Data": "%s"}} + } + """.formatted(rawMessage(UNVERIFIED))), UNVERIFIED); + } + + @Test + void v2SendEmail_verifiedEmailIdentity_succeeds() { + v2Request(AUTH_V2, "{\"EmailIdentity\": \"v2-" + VERIFIED + "\"}") + .when() + .post("/v2/email/identities"); + + assertSentV2(v2Request(AUTH_V2, simpleBody("v2-" + VERIFIED))); + } + + @Test + void v2SendEmail_addressUnderVerifiedDomain_succeeds() { + String domain = "verified-domain.floci.test"; + SesDomainIdentityTestHelper.createVerified(domain, AUTH_V2); + + assertSentV2(v2Request(AUTH_V2, simpleBody("anyone@" + domain))); + } + + @Test + void v2SendEmail_addressUnderASubdomainOfAVerifiedDomain_succeeds() { + // "if you create and verify an identity for example.com, you don't need to create separate + // identities for a.example.com, a.b.example.com, user@example.com, user@a.example.com". + String domain = "verified-parent.floci.test"; + SesDomainIdentityTestHelper.createVerified(domain, AUTH_V2); + + assertSentV2(v2Request(AUTH_V2, simpleBody("anyone@a.b." + domain))); + } + + @Test + void v1SendEmail_addressUnderADomainThatOnlyEndsLikeAVerifiedOne_returnsMessageRejected() { + String domain = "verified-suffix.floci.test"; + SesDomainIdentityTestHelper.createVerified(domain, AUTH_V2); + String lookalike = "anyone@not-" + domain; + + assertMessageRejected(sendEmail(AUTH_V1, lookalike), lookalike); + } + + @Test + void v1SendRawEmail_withoutSource_unverifiedSecondMimeFrom_returnsMessageRejected() { + String first = "first-" + VERIFIED; + String second = "second@unverified-sender.floci.test"; + verifyEmailIdentity(first); + + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(first + ", " + second)), second); + + assertNothingCapturedFrom(first); + } + + @Test + void v1SendRawEmail_withoutSource_everyMimeFromVerified_succeeds() { + String first = "both-first-" + VERIFIED; + String second = "both-second-" + VERIFIED; + verifyEmailIdentity(first); + verifyEmailIdentity(second); + + assertSent(sendRawEmail(AUTH_V1, rawMessage(first + ", " + second)), "SendRawEmail"); + } + + @Test + void v1SendEmail_addressUnderPendingDomain_returnsMessageRejected() { + // A domain identity stays Pending until its DKIM records are published, and a pending + // domain does not verify the addresses under it. + String domain = "pending-domain.floci.test"; + query("VerifyDomainIdentity").formParam("Domain", domain) + .when().post("/").then().statusCode(200); + + assertMessageRejected(sendEmail(AUTH_V1, "anyone@" + domain), "anyone@" + domain); + } + + @Test + void v1SendEmail_unverifiedReturnPath_returnsMessageRejected() { + String source = "rp-source-" + VERIFIED; + String returnPath = "bounces@unverified-sender.floci.test"; + verifyEmailIdentity(source); + + assertMessageRejected(sendEmail(AUTH_V1, source).formParam("ReturnPath", returnPath), returnPath); + + assertNothingCapturedFrom(source); + } + + @Test + void v1SendTemplatedEmail_unverifiedReturnPath_returnsMessageRejected() { + String source = "rp-templated-" + VERIFIED; + String returnPath = "templated-bounces@unverified-sender.floci.test"; + verifyEmailIdentity(source); + createTemplate(AUTH_V1); + + assertMessageRejected(sendTemplatedEmail(AUTH_V1, source).formParam("ReturnPath", returnPath), returnPath); + + assertNothingCapturedFrom(source); + } + + @Test + void v2SendEmail_simple_unverifiedFeedbackForwardingAddress_returnsMessageRejected() { + String source = "rp-v2-" + VERIFIED; + String feedback = "v2-bounces@unverified-sender.floci.test"; + verifyEmailIdentity(source); + + assertMessageRejectedV2(v2Request(AUTH_V2, """ + { + "FromEmailAddress": "%s", + "FeedbackForwardingEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}} + } + """.formatted(source, feedback)), feedback); + + assertNothingCapturedFrom(source); + } + + @Test + void v2SendEmail_raw_unverifiedFeedbackForwardingAddress_returnsMessageRejected() { + String source = "rp-v2-raw-" + VERIFIED; + String feedback = "v2-raw-bounces@unverified-sender.floci.test"; + verifyEmailIdentity(source); + + assertMessageRejectedV2(v2Request(AUTH_V2, """ + { + "FeedbackForwardingEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Raw": {"Data": "%s"}} + } + """.formatted(feedback, rawMessage(source))), feedback); + + assertNothingCapturedFrom(source); + } + + @Test + void v1SendRawEmail_verifiedSourceWithUnverifiedMimeFrom_returnsMessageRejected() { + String source = "raw-source-" + VERIFIED; + verifyEmailIdentity(source); + + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(UNVERIFIED)).formParam("Source", source), UNVERIFIED); + + assertNothingCapturedFrom(source); + } + + @Test + void v2SendEmail_raw_verifiedFromEmailAddress_mimeFromIsNotChecked() { + // A v2 raw send reads the From header only in place of a missing FromEmailAddress. + String source = "raw-v2-source-" + VERIFIED; + verifyEmailIdentity(source); + + assertSentV2(v2Request(AUTH_V2, """ + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Raw": {"Data": "%s"}} + } + """.formatted(source, rawMessage(UNVERIFIED)))); + } + + @Test + void v1SendRawEmail_unverifiedSenderHeader_returnsMessageRejected() { + String from = "raw-sender-from-" + VERIFIED; + String sender = "assistant@unverified-sender.floci.test"; + verifyEmailIdentity(from); + + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(from, "Sender: " + sender)), sender); + + assertNothingCapturedFrom(from); + } + + @Test + void v1SendRawEmail_unverifiedReturnPathHeader_returnsMessageRejected() { + String from = "raw-return-path-from-" + VERIFIED; + String returnPath = "raw-bounces@unverified-sender.floci.test"; + verifyEmailIdentity(from); + + assertMessageRejected(sendRawEmail(AUTH_V1, rawMessage(from, "Return-Path: <" + returnPath + ">")), + returnPath); + + assertNothingCapturedFrom(from); + } + + @Test + void v1SendRawEmail_sourceFromSenderAndReturnPathAllVerified_succeeds() { + String source = "all-source-" + VERIFIED; + String from = "all-from-" + VERIFIED; + String sender = "all-sender-" + VERIFIED; + String returnPath = "all-return-path-" + VERIFIED; + for (String address : List.of(source, from, sender, returnPath)) { + verifyEmailIdentity(address); + } + + assertSent(sendRawEmail(AUTH_V1, rawMessage(from, "Sender: " + sender, "Return-Path: <" + returnPath + ">")) + .formParam("Source", source), "SendRawEmail"); + } + + @Test + void v1SendEmail_twoUnverifiedAddresses_oneErrorNamesBoth() { + String returnPath = "both-bounces@unverified-sender.floci.test"; + + assertMessageRejected(sendEmail(AUTH_V1, UNVERIFIED).formParam("ReturnPath", returnPath), + UNVERIFIED + ", " + returnPath); + } + + @Test + void v1SendEmail_sourceArnOfAnotherAccount_succeeds() { + verifyOwnerAddress(); + + assertSent(sendEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS).formParam("SourceArn", OWNER_IDENTITY_ARN), + "SendEmail"); + } + + @Test + void v1SendEmail_addressVerifiedOnlyInAnotherAccount_withoutSourceArn_returnsMessageRejected() { + verifyOwnerAddress(); + + assertMessageRejected(sendEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS), OWNER_ADDRESS); + } + + @Test + void v1SendEmail_sourceArnOfTheCallersOwnAccount_returnsMessageRejected() { + verifyOwnerAddress(); + + assertMessageRejected(sendEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS) + .formParam("SourceArn", DELEGATE_IDENTITY_ARN), OWNER_ADDRESS); + } + + @Test + void v1SendEmail_sourceArnThatNamesNoSesIdentity_returnsMessageRejected() { + verifyOwnerAddress(); + + assertMessageRejected(sendEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS) + .formParam("SourceArn", "arn:aws:ses:us-east-1:" + OWNER_ACCOUNT + ":configuration-set/owner"), + OWNER_ADDRESS); + assertMessageRejected(sendEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS) + .formParam("SourceArn", "arn:aws:sns:us-east-1:" + OWNER_ACCOUNT + ":identity/" + OWNER_ADDRESS), + OWNER_ADDRESS); + } + + @Test + void v1SendEmail_returnPathArnOfAnotherAccount_coversOnlyTheReturnPath() { + verifyOwnerAddress(); + + assertMessageRejected(sendEmail(DELEGATE_AUTH_V1, UNVERIFIED) + .formParam("ReturnPath", OWNER_ADDRESS) + .formParam("ReturnPathArn", OWNER_IDENTITY_ARN), UNVERIFIED); + } + + @Test + void v1SendTemplatedEmail_sourceAndReturnPathArnsOfAnotherAccount_succeeds() { + verifyOwnerAddress(); + createTemplate(DELEGATE_AUTH_V1); + + assertSent(sendTemplatedEmail(DELEGATE_AUTH_V1, OWNER_ADDRESS) + .formParam("SourceArn", OWNER_IDENTITY_ARN) + .formParam("ReturnPath", OWNER_ADDRESS) + .formParam("ReturnPathArn", OWNER_IDENTITY_ARN), "SendTemplatedEmail"); + } + + @Test + void v1SendRawEmail_sourceArnHeaderOfAnotherAccount_coversTheFromAndReturnPath() { + verifyOwnerAddress(); + + assertSent(sendRawEmail(DELEGATE_AUTH_V1, rawMessage(OWNER_ADDRESS, + "X-SES-SOURCE-ARN: " + OWNER_IDENTITY_ARN, "Return-Path: <" + OWNER_ADDRESS + ">")), "SendRawEmail"); + } + + @Test + void v1SendRawEmail_fromAndReturnPathArnHeadersOfAnotherAccount_coverTheirAddresses() { + verifyOwnerAddress(); + + assertSent(sendRawEmail(DELEGATE_AUTH_V1, rawMessage(OWNER_ADDRESS, + "X-SES-FROM-ARN: " + OWNER_IDENTITY_ARN, "X-SES-RETURN-PATH-ARN: " + OWNER_IDENTITY_ARN, + "Return-Path: <" + OWNER_ADDRESS + ">")), "SendRawEmail"); + } + + @Test + void v1SendRawEmail_sourceArnParameterWinsOverItsHeader() { + verifyOwnerAddress(); + + assertSent(sendRawEmail(DELEGATE_AUTH_V1, + rawMessage(OWNER_ADDRESS, "X-SES-SOURCE-ARN: " + DELEGATE_IDENTITY_ARN)) + .formParam("Source", OWNER_ADDRESS) + .formParam("SourceArn", OWNER_IDENTITY_ARN), "SendRawEmail"); + } + + @Test + void v1SendRawEmail_fromArnParameterWinsOverItsHeader() { + verifyOwnerAddress(); + + assertSent(sendRawEmail(DELEGATE_AUTH_V1, rawMessage(OWNER_ADDRESS, "X-SES-FROM-ARN: " + DELEGATE_IDENTITY_ARN)) + .formParam("FromArn", OWNER_IDENTITY_ARN), "SendRawEmail"); + } + + @Test + void v1SendRawEmail_returnPathArnParameterCoversTheReturnPathHeader() { + verifyOwnerAddress(); + + assertSent(sendRawEmail(DELEGATE_AUTH_V1, rawMessage(OWNER_ADDRESS, "Return-Path: <" + OWNER_ADDRESS + ">")) + .formParam("FromArn", OWNER_IDENTITY_ARN) + .formParam("ReturnPathArn", OWNER_IDENTITY_ARN), "SendRawEmail"); + } + + @Test + void v2SendEmail_fromAndFeedbackForwardingIdentityArnsOfAnotherAccount_succeeds() { + verifyOwnerAddress(); + + assertSentV2(v2Request(DELEGATE_AUTH_V2, """ + { + "FromEmailAddress": "%s", + "FromEmailAddressIdentityArn": "%s", + "FeedbackForwardingEmailAddress": "%s", + "FeedbackForwardingEmailAddressIdentityArn": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}} + } + """.formatted(OWNER_ADDRESS, OWNER_IDENTITY_ARN, OWNER_ADDRESS, OWNER_IDENTITY_ARN))); + } + + @Test + void v2SendEmail_raw_fromEmailAddressIdentityArnOverridesTheArnHeaders() { + // "For Raw emails, the FromEmailAddressIdentityArn value overrides the X-SES-SOURCE-ARN and + // X-SES-FROM-ARN headers specified in raw email message content." + verifyOwnerAddress(); + + assertSentV2(v2Request(DELEGATE_AUTH_V2, """ + { + "FromEmailAddressIdentityArn": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Raw": {"Data": "%s"}} + } + """.formatted(OWNER_IDENTITY_ARN, rawMessage(OWNER_ADDRESS, + "X-SES-SOURCE-ARN: " + DELEGATE_IDENTITY_ARN, "X-SES-FROM-ARN: " + DELEGATE_IDENTITY_ARN)))); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressLengthTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressLengthTest.java index 19391ce7ec..662ec66c37 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressLengthTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressLengthTest.java @@ -28,10 +28,14 @@ class SesServiceAddressLengthTest { new EmailContent.Simple("Subject", "body", null, List.of()); private SesService service; + private SesIdentityService identityService; @BeforeEach void setUp() { - service = SesServiceTestBuilder.create().build(); + SesServiceTestBuilder builder = SesServiceTestBuilder.create(); + service = builder.build(); + identityService = builder.identityService(); + identityService.verifyEmailIdentity(SENDER, REGION); } @Test @@ -218,6 +222,8 @@ class SesServiceAddressLengthTest { @Test void rawSend_otherAddressHeadersAreNotChecked() { String tooLong = address(330, 'a'); + // Sender is a sending address, so it must be verified to reach the send. + identityService.verifyEmailIdentity(tooLong, REGION); assertDoesNotThrow(() -> service.sendEmail(request(SENDER).content(raw(SENDER, TO, "Sender: " + tooLong + "\r\nResent-To: " + tooLong + "\r\n")).build())); diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressSyntaxTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressSyntaxTest.java index e99a955142..c34b7717d6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressSyntaxTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceAddressSyntaxTest.java @@ -46,6 +46,7 @@ class SesServiceAddressSyntaxTest { service = builder.build(); sentEmails = builder.sentEmailService(); identityService = builder.identityService(); + identityService.verifyEmailIdentity(SENDER, REGION); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java index 080d22da9d..34828b8eda 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java @@ -52,6 +52,7 @@ class SesServiceListManagementTest { contactStore = builder.contactStore(); service = builder.build(); contacts = builder.contactService(); + builder.identityService().verifyEmailIdentity(FROM, REGION); // Sports defaults OPT_IN, Promos defaults OPT_OUT. contacts.createContactList(LIST, "desc", List.of( diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java index fa938e076b..cbe6c9296c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java @@ -48,6 +48,7 @@ class SesServiceSmtpTest { suppression = builder.suppressionService(); cvetTemplates = builder.cvetService(); identities = builder.identityService(); + identities.verifyEmailIdentity("from@example.com", "us-east-1"); } private void cvetTemplate(String name, String subject, String content) { @@ -239,6 +240,7 @@ class SesServiceSmtpTest { @Test void sendEmail_explicitReturnPath_isRelayedAndStored() { + identities.verifyEmailIdentity("bounces@example.com", "us-east-1"); String messageId = service.sendEmail(SendEmailRequest.builder() .source("from@example.com") .toAddresses(List.of("to@example.com")) @@ -254,7 +256,10 @@ class SesServiceSmtpTest { @Test void sendRawEmail_rejectedMessage_keepsNoReturnPathFromItsHeaders() { // The scan trips on the subject; the Return-Path header, read off the same message, must - // not survive on the record either, so the request's return path is used. + // not survive on the record either, so the request's return path is used. Both return paths + // are verified, so the send gets past the sender check to the scan. + identities.verifyEmailIdentity("bounces@example.com", "us-east-1"); + identities.verifyEmailIdentity("bounces@evil.example", "us-east-1"); String raw = "From: from@example.com\r\nTo: to@example.com\r\nReturn-Path: \r\n" + "Subject: " + SesContentScan.signature() + "\r\n\r\nbody\r\n"; @@ -307,6 +312,8 @@ class SesServiceSmtpTest { @Test void sendRawEmail_returnPathHeader_winsOverRequestField() { + identities.verifyEmailIdentity("request-bounces@example.com", "us-east-1"); + identities.verifyEmailIdentity("mime-bounces@example.com", "us-east-1"); String raw = "From: from@example.com\r\n" + "To: to@example.com\r\n" + "Return-Path: \r\n" @@ -326,6 +333,7 @@ class SesServiceSmtpTest { @Test void sendRawEmail_noReturnPathHeader_usesRequestField() { + identities.verifyEmailIdentity("request-bounces@example.com", "us-east-1"); String raw = "From: from@example.com\r\nTo: to@example.com\r\nSubject: x\r\n\r\nbody"; service.sendEmail(SendEmailRequest.builder() diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantSendV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantSendV2IntegrationTest.java index ca2cd58086..db83f5abd7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantSendV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantSendV2IntegrationTest.java @@ -59,8 +59,8 @@ class SesTenantSendV2IntegrationTest { void setup_tenantAndResources() { v2().body("{\"TenantName\":\"" + TENANT + "\"}") .when().post("/v2/email/tenants").then().statusCode(200); - v2().body("{\"EmailIdentity\":\"" + DOMAIN + "\"}") - .when().post("/v2/email/identities").then().statusCode(200); + // Verified, so the addresses under it may send once the tenant gate lets them through. + SesDomainIdentityTestHelper.createVerified(DOMAIN, AUTH); v2().body("{\"ConfigurationSetName\":\"" + CONFIG_SET + "\"}") .when().post("/v2/email/configuration-sets").then().statusCode(200); v2().body("{\"TemplateName\":\"" + TEMPLATE + "\",\"TemplateContent\":{\"Subject\":\"s\",\"Text\":\"t\"}}") diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java index 9924ed41f4..d83b1d9fa7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java @@ -145,59 +145,7 @@ class SesV2IntegrationTest { .jsonPath() .getList("DkimAttributes.Tokens", String.class); - String locationHeader = given() - .contentType("application/xml") - .body(""" - - - floci.test - ses-v2-dkim-success - - """) - .when() - .post("/2013-04-01/hostedzone") - .then() - .statusCode(201) - .extract() - .header("Location"); - - String zoneId = locationHeader.substring(locationHeader.lastIndexOf('/') + 1); - StringBuilder body = new StringBuilder(""" - - - - - """); - for (String token : tokens) { - body.append(""" - - CREATE - - """).append(token).append("._domainkey.v2dkim-success.floci.test.\n") - .append(""" - CNAME - 300 - - """).append(token).append(".dkim.amazonses.com.\n") - .append(""" - - - - """); - } - body.append(""" - - - - """); - - given() - .contentType("application/xml") - .body(body.toString()) - .when() - .post("/2013-04-01/hostedzone/" + zoneId + "/rrset") - .then() - .statusCode(200); + SesDomainIdentityTestHelper.publishDkimRecords("v2dkim-success.floci.test", tokens); given() .contentType("application/json") @@ -998,6 +946,17 @@ class SesV2IntegrationTest { @Test @Order(67) void sendEmail_raw_noFromAddress_usesMimeFrom() { + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + {"EmailIdentity": "raw-from@floci.test"} + """) + .when() + .post("/v2/email/identities") + .then() + .statusCode(200); + given() .contentType("application/json") .header("Authorization", AUTH_HEADER) @@ -1304,17 +1263,19 @@ class SesV2IntegrationTest { void inspectionEndpoint_returnsEmailsFromAllRegions() { given().delete("/_aws/ses").then().statusCode(200); - // Create identity usable in both regions (domain covers all addresses) - given() - .contentType("application/json") - .header("Authorization", AUTH_HEADER) - .body(""" - {"EmailIdentity": "example.com"} - """) - .when() - .post("/v2/email/identities") - .then() - .statusCode(200); + // Identities are regional, so the sender is verified in each region it sends from. + for (String region : List.of("us-east-1", "ap-northeast-1")) { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/email/aws4_request") + .formParam("Action", "VerifyEmailIdentity") + .formParam("EmailAddress", "multi@example.com") + .when() + .post("/") + .then() + .statusCode(200); + } // Send from us-east-1 given() diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayTest.java index 463311ce2d..42f780c06e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayTest.java @@ -440,6 +440,37 @@ class SmtpRelayTest { assertTrue(h.messageTags().isEmpty()); } + @Test + void parseRawHeaders_extractsSenderAndTheFirstSendingAuthorizationHeaders() { + String raw = "From: sender@example.com\r\n" + + "Sender: Assistant \r\n" + + "To: to@example.com\r\n" + + "X-SES-SOURCE-ARN: arn:aws:ses:us-east-1:111111111111:identity/example.com\r\n" + + "X-SES-SOURCE-ARN: arn:aws:ses:us-east-1:222222222222:identity/example.com\r\n" + + "X-SES-FROM-ARN: arn:aws:ses:us-east-1:333333333333:identity/example.com\r\n" + + "X-SES-RETURN-PATH-ARN: arn:aws:ses:us-east-1:444444444444:identity/example.com\r\n" + + "Subject: x\r\n\r\nbody"; + + SmtpRelay.RawMessageHeaders h = SmtpRelay.parseRawHeaders(raw); + + assertEquals("assistant@example.com", h.sender()); + assertEquals("arn:aws:ses:us-east-1:111111111111:identity/example.com", h.sourceArn()); + assertEquals("arn:aws:ses:us-east-1:333333333333:identity/example.com", h.fromArn()); + assertEquals("arn:aws:ses:us-east-1:444444444444:identity/example.com", h.returnPathArn()); + } + + @Test + void parseRawHeaders_withoutSenderOrSendingAuthorizationHeaders_returnsBlanks() { + String raw = "From: sender@example.com\r\nTo: to@example.com\r\nSubject: x\r\n\r\nbody"; + + SmtpRelay.RawMessageHeaders h = SmtpRelay.parseRawHeaders(raw); + + assertEquals("", h.sender()); + assertEquals("", h.sourceArn()); + assertEquals("", h.fromArn()); + assertEquals("", h.returnPathArn()); + } + @Test void parseMessageTagsHeader_skipsEntriesWithoutAName() { assertEquals(List.of(new MessageTag("ok", "1")),