mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(ses): implement SendEmail ListManagementOptions suppression and unsubscribe (v2) (#1875)
* feat(ses): implement SendEmail ListManagementOptions suppression and unsubscribe (v2)
SendEmail now honors ListManagementOptions (ContactListName / TopicName):
Opt-out suppression — recipients opted out of the named contact list are
suppressed as a Bounce, reusing the existing relay-exclusion and Bounce-event
path. A contact is opted out when UnsubscribeAll is set; with a TopicName, an
explicit OPT_OUT preference (or the topic's OPT_OUT DefaultSubscriptionStatus
when no explicit preference) suppresses; without a TopicName, only
UnsubscribeAll contacts are suppressed. A recipient that is not yet a contact
is created on the list automatically; an unknown contact list fails the send.
Unsubscribe handling — a single-recipient list-managed send gets a functional
unsubscribe link: the {{amazonSESUnsubscribeUrl}} body placeholder is replaced
and List-Unsubscribe / List-Unsubscribe-Post headers are added. The link points
at Floci's own /_aws/ses/unsubscribe endpoint (GET/POST), which applies the
opt-out (topic -> OPT_OUT, no topic -> UnsubscribeAll), auto-creating the
contact if needed. The link uses readable query parameters rather than AWS's
opaque token, and its scheme follows Floci's base URL. Raw sends do not get
link injection yet.
* fix(ses): apply list-management unsubscribe headers to relay + inspection, override duplicates, preserve placeholder in templates
Addresses three review findings on the unsubscribe handling:
- The List-Unsubscribe / List-Unsubscribe-Post headers now reach the SMTP relay
message (relay() takes the additional headers and applies them) and are stored
on SentEmail so GET /_aws/ses surfaces them under Headers — previously they
only appeared in published events.
- withUnsubscribeHeaders now overrides a caller-supplied List-Unsubscribe /
List-Unsubscribe-Post rather than appending a duplicate, matching AWS.
- applyTemplateData leaves the reserved {{amazonSESUnsubscribeUrl}} placeholder
intact instead of failing with MissingRenderingAttribute, so a templated body
can carry it (replacement still happens post-render in the send path).
* fix(ses): sanitize relayed headers against SMTP injection and avoid double-slash unsubscribe URLs
User-supplied SendEmail Headers reach the SMTP relay, where a name or value containing
CR/LF could inject extra headers (e.g. Bcc) into the delivered message. Skip any header
with a blank name or CR/LF in its name/value before adding it to the relay message, and
log the drop. Also trim a trailing slash from the configured base URL when building the
List-Unsubscribe URL so a base-url ending in "/" no longer yields "//_aws/...".
* fix(ses): make the unsubscribe GET a no-mutation confirmation page
The unsubscribe endpoint applied the opt-out on both GET and POST. A GET is a browser
click that AWS answers with a landing page, and treating it as a mutation is the RFC 8058
prefetch hazard: mail clients, scanners and link-preview bots that fetch the URL would
silently unsubscribe the contact. GET now renders a confirmation form that POSTs back;
only the POST (the RFC 8058 one-click request) applies the opt-out. Adds GET test
coverage and updates the docs.
* test(ses): add an SDK compat test for SendEmail with ListManagementOptions
Send with ListManagementOptions through the SES v2 SDK client to prove the nested structure
(ContactListName + optional TopicName) marshals as a real client sends it, and that Floci
actually applies it — an absent recipient is auto-created as a contact on the list. Also note
in the docs that {{amazonSESUnsubscribeUrl}} is left verbatim on a send without
ListManagementOptions (Floci behavior, not verified against real SES).
* fix(ses): harden the unsubscribe confirmation page and document its trust model
Escape '&' as '&' in the confirmation form's action attribute (valid HTML), and add the
RFC 8058 one-click hidden field so the form's POST body matches the advertised
List-Unsubscribe-Post header. Also document — in the controller Javadoc and the SES docs — that
the endpoint carries the target in readable query parameters and, like the rest of Floci, is
unauthenticated, so it is only safe on a trusted dev/test network.
* fix(ses): filter unsafe headers once so all send surfaces stay consistent
The SMTP relay dropped user-supplied headers with a blank name or CR/LF, but the stored
SentEmail (GET /_aws/ses) and published events kept the raw list — so inspection and events
could show headers the relay dropped and retain CR/LF injection payloads. Move the safety
check to MessageHeader.isSafe(), filter the headers once in the send path, and use that
sanitized list for storage, relay, and event publishing alike.
* fix(ses): reject SES v2 message headers that omit a required Name or Value
Content.Simple.Headers / Content.Template.Headers entries declare Name and
Value as required members. A header object missing either member was parsed
leniently and later silently dropped, which is surprising when the request is
otherwise accepted. Reject it at parse time the way AWS does — BadRequestException
with the Smithy "Member must not be null" constraint message anchored at the
offending header path and 1-based index.
This commit is contained in:
1 parent
66fd109ad6
commit
fa9ed9100e
17 files changed
+1269
-64
No files matched your search
+119
@@ -0,0 +1,119 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
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.Body;
|
||||
import software.amazon.awssdk.services.sesv2.model.Content;
|
||||
import software.amazon.awssdk.services.sesv2.model.CreateContactListRequest;
|
||||
import software.amazon.awssdk.services.sesv2.model.DeleteContactListRequest;
|
||||
import software.amazon.awssdk.services.sesv2.model.Destination;
|
||||
import software.amazon.awssdk.services.sesv2.model.EmailContent;
|
||||
import software.amazon.awssdk.services.sesv2.model.GetContactRequest;
|
||||
import software.amazon.awssdk.services.sesv2.model.GetContactResponse;
|
||||
import software.amazon.awssdk.services.sesv2.model.ListManagementOptions;
|
||||
import software.amazon.awssdk.services.sesv2.model.Message;
|
||||
import software.amazon.awssdk.services.sesv2.model.SendEmailRequest;
|
||||
import software.amazon.awssdk.services.sesv2.model.SendEmailResponse;
|
||||
import software.amazon.awssdk.services.sesv2.model.SubscriptionStatus;
|
||||
import software.amazon.awssdk.services.sesv2.model.Topic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* SDK compatibility test for {@code SendEmail} with {@code ListManagementOptions} through the AWS
|
||||
* Java SDK v2 {@link SesV2Client}. A real SDK round-trip proves the nested {@code ListManagementOptions}
|
||||
* structure (ContactListName + optional TopicName) marshals as a real client sends it — and that Floci
|
||||
* actually processes it, since an absent recipient is auto-created as a contact on the list (matches AWS).
|
||||
*/
|
||||
@DisplayName("SES v2 SendEmail ListManagementOptions")
|
||||
class SesListManagementOptionsTest {
|
||||
|
||||
private static final String LIST = "compat-lmo-list";
|
||||
private static final String TOPIC = "weekly";
|
||||
private static final String FROM = "sender@example.com";
|
||||
|
||||
private static SesV2Client sesV2;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
sesV2 = TestFixtures.sesV2Client();
|
||||
// Only one contact list may exist per account; clear any leftover, then create ours.
|
||||
sesV2.listContactLists(r -> { }).contactLists().forEach(cl ->
|
||||
sesV2.deleteContactList(DeleteContactListRequest.builder()
|
||||
.contactListName(cl.contactListName()).build()));
|
||||
sesV2.createContactList(CreateContactListRequest.builder()
|
||||
.contactListName(LIST)
|
||||
.topics(Topic.builder()
|
||||
.topicName(TOPIC)
|
||||
.displayName("Weekly")
|
||||
.defaultSubscriptionStatus(SubscriptionStatus.OPT_IN)
|
||||
.description("weekly digest")
|
||||
.build())
|
||||
.build());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (sesV2 != null) {
|
||||
try {
|
||||
sesV2.deleteContactList(DeleteContactListRequest.builder().contactListName(LIST).build());
|
||||
} catch (RuntimeException ignored) {
|
||||
// best-effort
|
||||
}
|
||||
sesV2.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendWithListManagementOptions_roundTripsAndAutoCreatesContact() {
|
||||
String recipient = "lmo-compat-" + TestFixtures.uniqueName() + "@example.com";
|
||||
|
||||
SendEmailResponse response = sesV2.sendEmail(SendEmailRequest.builder()
|
||||
.fromEmailAddress(FROM)
|
||||
.destination(Destination.builder().toAddresses(recipient).build())
|
||||
.content(EmailContent.builder()
|
||||
.simple(Message.builder()
|
||||
.subject(Content.builder().data("compat-lmo").build())
|
||||
.body(Body.builder().text(Content.builder().data("hi").build()).build())
|
||||
.build())
|
||||
.build())
|
||||
.listManagementOptions(ListManagementOptions.builder()
|
||||
.contactListName(LIST)
|
||||
.topicName(TOPIC)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
assertThat(response.messageId()).isNotBlank();
|
||||
|
||||
// The nested ListManagementOptions was actually applied: an absent recipient is auto-created
|
||||
// as a contact on the named list.
|
||||
GetContactResponse contact = sesV2.getContact(GetContactRequest.builder()
|
||||
.contactListName(LIST).emailAddress(recipient).build());
|
||||
assertThat(contact.emailAddress()).isEqualTo(recipient);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendWithListManagementOptions_topicNameOptional() {
|
||||
String recipient = "lmo-compat-notopic-" + TestFixtures.uniqueName() + "@example.com";
|
||||
|
||||
SendEmailResponse response = sesV2.sendEmail(SendEmailRequest.builder()
|
||||
.fromEmailAddress(FROM)
|
||||
.destination(Destination.builder().toAddresses(recipient).build())
|
||||
.content(EmailContent.builder()
|
||||
.simple(Message.builder()
|
||||
.subject(Content.builder().data("compat-lmo-notopic").build())
|
||||
.body(Body.builder().text(Content.builder().data("hi").build()).build())
|
||||
.build())
|
||||
.build())
|
||||
.listManagementOptions(ListManagementOptions.builder()
|
||||
.contactListName(LIST)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
assertThat(response.messageId()).isNotBlank();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user