fix(ses): skip the raw relay when the message cannot be parsed or is nested too deeply (#9066)

This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-19 21:55:32 -04:00
1 parent 0af048a099
commit a769b77124
7 files changed
+73 -15

No files matched your search

@@ -307,7 +307,7 @@ class SesServiceSmtpTest {
SmtpRelay.RawRelayMessage relayed = capturedRawRelay();
assertEquals("from@example.com", relayed.from());
assertEquals(List.of("to@example.com"), relayed.destinations());
assertEquals("raw MIME", relayed.rawMessage());
assertNotNull(relayed.message());
assertEquals(messageId, relayed.messageId());
}
@@ -1,5 +1,6 @@
package io.floci.aws.services.ses;
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;
@@ -325,6 +326,28 @@ class SmtpRelayTest {
List.of("to@example.com"), "Subject: X\r\n\r\nBody")));
}
@Test
void relayRaw_nestingThatOverflowsTheParser_isSkippedNotThrown() {
SmtpRelay relay = new SmtpRelay(mailClient, true);
String rawMime = NestedMime.rfc822Layers(NestedMime.OVERFLOWS_THE_PARSER);
assertDoesNotThrow(() -> relay.relayRaw(raw("from@example.com", List.of("to@example.com"), rawMime)));
verify(mailClient, never()).sendMail(any(MailMessage.class));
}
@Test
void relayRaw_nestingThatParsesButOverflowsTheWriter_isSkippedNotThrown() {
SmtpRelay relay = new SmtpRelay(mailClient, true);
SmtpRelay.RawRelayMessage nested = raw("from@example.com", List.of("to@example.com"),
NestedMime.rfc822Layers(NestedMime.OVERFLOWS_THE_WRITER));
assertNotNull(nested.message());
assertDoesNotThrow(() -> relay.relayRaw(nested));
verify(mailClient, never()).sendMail(any(MailMessage.class));
}
@Test
void parseRawRecipients_extractsToCcBcc() {
String raw = "From: sender@example.com\r\n"
@@ -0,0 +1,18 @@
package io.floci.aws.services.ses.mime;
/** Builds messages wrapped in {@code message/rfc822} layers, for the depth limits of mime4j. */
public final class NestedMime {
/** Deep enough that mime4j, which nests a reader per forwarded message, overflows the stack parsing it. */
public static final int OVERFLOWS_THE_PARSER = 100_000;
/** Shallow enough to parse, too deep for the writer that re-serializes embedded messages. */
public static final int OVERFLOWS_THE_WRITER = 10_000;
private NestedMime() {
}
public static String rfc822Layers(int depth) {
return "Content-Type: message/rfc822\r\n\r\n".repeat(depth) + "Content-Type: text/plain\r\n\r\nbody\r\n";
}
}
@@ -22,9 +22,7 @@ class SesMimeParserTest {
@Test
void nestingThatOverflowsTheParserIsNullNotAnError() {
// mime4j nests a reader per forwarded message and overflows the stack at this depth.
String layer = "Content-Type: message/rfc822\r\n\r\n";
byte[] mime = (layer.repeat(100_000) + "Content-Type: text/plain\r\n\r\nbody\r\n")
byte[] mime = NestedMime.rfc822Layers(NestedMime.OVERFLOWS_THE_PARSER)
.getBytes(StandardCharsets.US_ASCII);
assertNull(SesMimeParser.parse(mime));