refactor(ses): move the raw message parsing into the mime package (#9239)

SesRawMessage now owns decoding a RawMessage.Data blob, the single
parse the send path shares, and the headers read off it. SmtpRelay
keeps only the relay itself and calls the shared helpers.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-23 06:39:26 -04:00
1 parent a2051d4085
commit 2f652f3f87
6 files changed
+442 -385

No files matched your search

@@ -1,5 +1,6 @@
package io.floci.aws.services.ses;
import io.floci.aws.services.ses.mime.SesRawMessage;
import io.floci.aws.services.ses.mime.SesTestSignature;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
@@ -289,7 +290,7 @@ class SesMessageInsightsV2IntegrationTest {
// The signature is assembled at run time so it never appears in the tree.
String raw = "From: " + SENDER + "\r\nTo: raw@example.com\r\n"
+ "Subject: rejected-mime-subject\r\n"
+ SmtpRelay.HEADER_MESSAGE_TAGS + ": campaign=fromheader\r\n\r\n"
+ SesRawMessage.HEADER_MESSAGE_TAGS + ": campaign=fromheader\r\n\r\n"
+ SesTestSignature.value() + "\r\n";
String messageId = given().contentType("application/json").header("Authorization", AUTH)
.body("""
@@ -4,7 +4,6 @@ import io.floci.aws.services.ses.mime.NestedMime;
import io.floci.aws.services.ses.mime.SesMimeParser;
import io.floci.aws.services.ses.model.MessageAttachment;
import io.floci.aws.services.ses.model.MessageHeader;
import io.floci.aws.services.ses.model.MessageTag;
import io.vertx.core.Future;
import io.vertx.ext.mail.MailAttachment;
import io.vertx.ext.mail.MailClient;
@@ -367,162 +366,6 @@ class SmtpRelayTest {
verify(mailClient, never()).sendMail(any(MailMessage.class));
}
@Test
void parseRawRecipients_extractsToCcBcc() {
String raw = "From: sender@example.com\r\n"
+ "To: to1@example.com, to2@example.com\r\n"
+ "Cc: cc@example.com\r\n"
+ "Bcc: bcc@example.com\r\n"
+ "Subject: test\r\n\r\nbody";
List<String> recipients = SmtpRelay.parseRawRecipients(raw);
assertEquals(List.of("to1@example.com", "to2@example.com", "cc@example.com", "bcc@example.com"),
recipients);
}
@Test
void parseRawRecipients_acceptsBase64Encoded() {
String raw = "From: a@example.com\r\nTo: only@example.com\r\nSubject: x\r\n\r\nbody";
String b64 = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
assertEquals(List.of("only@example.com"), SmtpRelay.parseRawRecipients(b64));
}
@Test
void tryBase64Decode_plainTextMime_isNotTreatedAsBase64() {
// The MIME decoder skips characters outside the base64 alphabet, so a plain-text message
// whose remaining alphabet characters happen to form a valid length would otherwise decode
// into garbage.
String raw = "From: s@example.com\r\nMessage-ID: <caller@example.com>\r\nSubject: x\r\n\r\nbody";
assertEquals(raw, new String(SmtpRelay.tryBase64Decode(raw),
StandardCharsets.UTF_8));
}
@Test
void tryBase64Decode_base64Payload_isDecoded() {
String raw = "From: s@example.com\r\nSubject: x\r\n\r\nbody";
String encoded = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
assertEquals(raw, new String(SmtpRelay.tryBase64Decode(encoded),
StandardCharsets.UTF_8));
}
@Test
void parseRawRecipients_blankOrUnparseable_returnsEmpty() {
assertTrue(SmtpRelay.parseRawRecipients(null).isEmpty());
assertTrue(SmtpRelay.parseRawRecipients("").isEmpty());
assertTrue(SmtpRelay.parseRawRecipients(" ").isEmpty());
}
@Test
void parseRawRecipients_noRecipientHeaders_returnsEmpty() {
String raw = "From: a@example.com\r\nSubject: x\r\n\r\nbody";
assertTrue(SmtpRelay.parseRawRecipients(raw).isEmpty());
}
@Test
void parseRawHeaders_extractsSubjectAndStructuredRecipients() {
String raw = "From: sender@example.com\r\n"
+ "To: to1@example.com, to2@example.com\r\n"
+ "Cc: cc@example.com\r\n"
+ "Bcc: bcc@example.com\r\n"
+ "Subject: Hello world\r\n\r\nbody";
String b64 = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
SmtpRelay.RawMessageHeaders h = SmtpRelay.parseRawHeaders(b64);
assertEquals("Hello world", h.subject());
assertEquals(List.of("to1@example.com", "to2@example.com"), h.to());
assertEquals(List.of("cc@example.com"), h.cc());
assertEquals(List.of("bcc@example.com"), h.bcc());
}
@Test
void parseRawHeaders_nestedBeyondTheParser_returnsEmptyRecord() {
// mime4j nests a reader per forwarded message and overflows the stack at this depth; the
// header pre-parse must answer like any other unparseable message, not kill the request.
String raw = "Content-Type: message/rfc822\r\n\r\n".repeat(100_000) + "Subject: deep\r\n\r\nx\r\n";
SmtpRelay.ParsedRawMessage parsed = SmtpRelay.parseRawMessage(raw);
assertNull(parsed.message());
assertEquals("", parsed.headers().subject());
assertTrue(parsed.headers().to().isEmpty());
}
@Test
void parseRawHeaders_blank_returnsEmptyRecord() {
SmtpRelay.RawMessageHeaders h = SmtpRelay.parseRawHeaders(null);
assertEquals("", h.subject());
assertTrue(h.to().isEmpty());
assertTrue(h.cc().isEmpty());
assertTrue(h.bcc().isEmpty());
}
@Test
void parseRawHeaders_extractsReturnPathAndSesControlHeaders() {
String raw = "From: sender@example.com\r\n"
+ "To: to@example.com\r\n"
+ "Return-Path: <bounces@example.com>\r\n"
+ "X-SES-CONFIGURATION-SET: my-config-set\r\n"
+ "X-SES-MESSAGE-TAGS: campaign=spring, tier=gold\r\n"
+ "Subject: x\r\n\r\nbody";
SmtpRelay.RawMessageHeaders h = SmtpRelay.parseRawHeaders(raw);
assertEquals("bounces@example.com", h.returnPath());
assertEquals("my-config-set", h.configurationSet());
assertEquals(List.of(new MessageTag("campaign", "spring"), new MessageTag("tier", "gold")),
h.messageTags());
}
@Test
void parseRawHeaders_withoutSesHeaders_returnsBlanksAndNoTags() {
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.returnPath());
assertEquals("", h.configurationSet());
assertTrue(h.messageTags().isEmpty());
}
@Test
void parseRawHeaders_extractsSenderAndTheFirstSendingAuthorizationHeaders() {
String raw = "From: sender@example.com\r\n"
+ "Sender: Assistant <assistant@example.com>\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")),
SmtpRelay.parseMessageTagsHeader("ok=1, =2, novalueseparator"));
assertTrue(SmtpRelay.parseMessageTagsHeader(null).isEmpty());
assertTrue(SmtpRelay.parseMessageTagsHeader(" ").isEmpty());
}
@Test
void formatMessageId_usesTheReturnedMessageIdAsTheLocalPart() {
assertEquals("<abc-123@email.amazonses.com>", SmtpRelay.formatMessageId("abc-123"));
@@ -693,16 +536,6 @@ class SmtpRelayTest {
assertEquals(List.of("Carol <carol@example.com>"), sent.getCc());
}
@Test
void parseRawRecipients_stripsDisplayNames() {
// The envelope, suppression checks and published events all work with bare addresses.
String raw = "From: Alice <alice@example.com>\r\n"
+ "To: Bob Recipient <bob@example.com>\r\n"
+ "Subject: x\r\n\r\nbody";
assertEquals(List.of("bob@example.com"), SmtpRelay.parseRawRecipients(raw));
assertEquals("alice@example.com", SmtpRelay.parseRawHeaders(raw).from());
}
@Test
void relayRaw_preservesAttachments() {
SmtpRelay relay = enabledRelay();
@@ -0,0 +1,187 @@
package io.floci.aws.services.ses.mime;
import io.floci.aws.services.ses.model.MessageTag;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class SesRawMessageTest {
@Test
void parseRawRecipients_extractsToCcBcc() {
String raw = "From: sender@example.com\r\n"
+ "To: to1@example.com, to2@example.com\r\n"
+ "Cc: cc@example.com\r\n"
+ "Bcc: bcc@example.com\r\n"
+ "Subject: test\r\n\r\nbody";
List<String> recipients = SesRawMessage.parseRawRecipients(raw);
assertEquals(List.of("to1@example.com", "to2@example.com", "cc@example.com", "bcc@example.com"),
recipients);
}
@Test
void parseRawRecipients_acceptsBase64Encoded() {
String raw = "From: a@example.com\r\nTo: only@example.com\r\nSubject: x\r\n\r\nbody";
String b64 = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
assertEquals(List.of("only@example.com"), SesRawMessage.parseRawRecipients(b64));
}
@Test
void tryBase64Decode_plainTextMime_isNotTreatedAsBase64() {
// The MIME decoder skips characters outside the base64 alphabet, so a plain-text message
// whose remaining alphabet characters happen to form a valid length would otherwise decode
// into garbage.
String raw = "From: s@example.com\r\nMessage-ID: <caller@example.com>\r\nSubject: x\r\n\r\nbody";
assertEquals(raw, new String(SesRawMessage.tryBase64Decode(raw),
StandardCharsets.UTF_8));
}
@Test
void tryBase64Decode_base64Payload_isDecoded() {
String raw = "From: s@example.com\r\nSubject: x\r\n\r\nbody";
String encoded = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
assertEquals(raw, new String(SesRawMessage.tryBase64Decode(encoded),
StandardCharsets.UTF_8));
}
@Test
void parseRawRecipients_blankOrUnparseable_returnsEmpty() {
assertTrue(SesRawMessage.parseRawRecipients(null).isEmpty());
assertTrue(SesRawMessage.parseRawRecipients("").isEmpty());
assertTrue(SesRawMessage.parseRawRecipients(" ").isEmpty());
}
@Test
void parseRawRecipients_noRecipientHeaders_returnsEmpty() {
String raw = "From: a@example.com\r\nSubject: x\r\n\r\nbody";
assertTrue(SesRawMessage.parseRawRecipients(raw).isEmpty());
}
@Test
void parseRawHeaders_extractsSubjectAndStructuredRecipients() {
String raw = "From: sender@example.com\r\n"
+ "To: to1@example.com, to2@example.com\r\n"
+ "Cc: cc@example.com\r\n"
+ "Bcc: bcc@example.com\r\n"
+ "Subject: Hello world\r\n\r\nbody";
String b64 = Base64.getEncoder().encodeToString(
raw.getBytes(StandardCharsets.UTF_8));
SesRawMessage.Headers h = SesRawMessage.parseRawHeaders(b64);
assertEquals("Hello world", h.subject());
assertEquals(List.of("to1@example.com", "to2@example.com"), h.to());
assertEquals(List.of("cc@example.com"), h.cc());
assertEquals(List.of("bcc@example.com"), h.bcc());
}
@Test
void parseRawHeaders_nestedBeyondTheParser_returnsEmptyRecord() {
// mime4j nests a reader per forwarded message and overflows the stack at this depth; the
// header pre-parse must answer like any other unparseable message, not kill the request.
String raw = "Content-Type: message/rfc822\r\n\r\n".repeat(100_000) + "Subject: deep\r\n\r\nx\r\n";
SesRawMessage.Parsed parsed = SesRawMessage.parseRawMessage(raw);
assertNull(parsed.message());
assertEquals("", parsed.headers().subject());
assertTrue(parsed.headers().to().isEmpty());
}
@Test
void parseRawHeaders_blank_returnsEmptyRecord() {
SesRawMessage.Headers h = SesRawMessage.parseRawHeaders(null);
assertEquals("", h.subject());
assertTrue(h.to().isEmpty());
assertTrue(h.cc().isEmpty());
assertTrue(h.bcc().isEmpty());
}
@Test
void parseRawHeaders_extractsReturnPathAndSesControlHeaders() {
String raw = "From: sender@example.com\r\n"
+ "To: to@example.com\r\n"
+ "Return-Path: <bounces@example.com>\r\n"
+ "X-SES-CONFIGURATION-SET: my-config-set\r\n"
+ "X-SES-MESSAGE-TAGS: campaign=spring, tier=gold\r\n"
+ "Subject: x\r\n\r\nbody";
SesRawMessage.Headers h = SesRawMessage.parseRawHeaders(raw);
assertEquals("bounces@example.com", h.returnPath());
assertEquals("my-config-set", h.configurationSet());
assertEquals(List.of(new MessageTag("campaign", "spring"), new MessageTag("tier", "gold")),
h.messageTags());
}
@Test
void parseRawHeaders_withoutSesHeaders_returnsBlanksAndNoTags() {
String raw = "From: sender@example.com\r\nTo: to@example.com\r\nSubject: x\r\n\r\nbody";
SesRawMessage.Headers h = SesRawMessage.parseRawHeaders(raw);
assertEquals("", h.returnPath());
assertEquals("", h.configurationSet());
assertTrue(h.messageTags().isEmpty());
}
@Test
void parseRawHeaders_extractsSenderAndTheFirstSendingAuthorizationHeaders() {
String raw = "From: sender@example.com\r\n"
+ "Sender: Assistant <assistant@example.com>\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";
SesRawMessage.Headers h = SesRawMessage.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";
SesRawMessage.Headers h = SesRawMessage.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")),
SesRawMessage.parseMessageTagsHeader("ok=1, =2, novalueseparator"));
assertTrue(SesRawMessage.parseMessageTagsHeader(null).isEmpty());
assertTrue(SesRawMessage.parseMessageTagsHeader(" ").isEmpty());
}
@Test
void parseRawRecipients_stripsDisplayNames() {
// The envelope, suppression checks and published events all work with bare addresses.
String raw = "From: Alice <alice@example.com>\r\n"
+ "To: Bob Recipient <bob@example.com>\r\n"
+ "Subject: x\r\n\r\nbody";
assertEquals(List.of("bob@example.com"), SesRawMessage.parseRawRecipients(raw));
assertEquals("alice@example.com", SesRawMessage.parseRawHeaders(raw).from());
}
@Test
void isControlHeader_matchesTheXSesPrefixInAnyCase() {
assertTrue(SesRawMessage.isControlHeader("X-SES-CONFIGURATION-SET"));
assertTrue(SesRawMessage.isControlHeader("x-ses-message-tags"));
assertFalse(SesRawMessage.isControlHeader("X-SESSION"));
assertFalse(SesRawMessage.isControlHeader("X-Mailer"));
}
}