mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ses): scan raw sends for the EICAR test file and keep it out of the mailbox store (#4156)
* feat(ses): scan raw sends for the EICAR test file and keep it out of the mailbox store The content scan now covers raw sends: the wire bytes are searched for the plain and the base64-encoded signature before parsing, so a message the lenient MIME parser cannot walk is still caught, and the parsed message is walked part by part, decoding attachments, descending into forwarded messages and decoding RFC 2047 header words. A candidate found on the wire is confirmed by decoding the run around it, so a base64 near-miss of the signature is not rejected. A rejected record keeps only its source, its envelope and RejectReason; subject, headers, reply-to addresses, bodies and raw data are dropped, and the inspection endpoint lists it in neither the Simple nor the raw shape, so the signature never reaches the mailbox store on disk. A MIME parse failure logs the exception class only, since the message text could echo content. * test(ses): pin that a rejected raw send publishes no parsed subject The raw rejection test checked the header block and the recipient lists but read commonHeaders only for the recipients, so a subject leaking back into the event would have gone unnoticed.
This commit is contained in:
1 parent
90d8c355d9
commit
235ee91a76
9 files changed
+577
-40
No files matched your search
@@ -1,7 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.services.ses.SesContentScan.Result;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -28,4 +35,199 @@ class SesContentScanTest {
|
||||
void signatureInsideATextBodyIsFlagged() {
|
||||
assertTrue(SesContentScan.containsTestVirus(null, "prefix " + SIGNATURE + " suffix"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureInsideAPlainMimeBodyIsFlagged() {
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n\r\n" + SIGNATURE + "\r\n";
|
||||
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(mime.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureInsideAQuotedPrintableAttachmentIsFoundOnlyByDecoding() {
|
||||
// Quoted-printable with escaped characters: neither the plain signature nor its base64
|
||||
// spelling is on the wire, so only the decoded part walk can find it.
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n"
|
||||
+ "--b\r\nContent-Type: text/plain\r\n\r\nsee attachment\r\n"
|
||||
+ "--b\r\nContent-Type: application/octet-stream; name=\"eicar.com\"\r\n"
|
||||
+ "Content-Transfer-Encoding: quoted-printable\r\n"
|
||||
+ "Content-Disposition: attachment; filename=\"eicar.com\"\r\n\r\n"
|
||||
+ quotedPrintable(SIGNATURE) + "\r\n--b--\r\n";
|
||||
byte[] bytes = mime.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertFalse(new String(bytes, StandardCharsets.ISO_8859_1).contains(SIGNATURE),
|
||||
"the fixture must only carry the signature in encoded form");
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureStraddlingAChunkBoundaryInsideALargeAttachmentIsFlagged() {
|
||||
// The decoded part is longer than one scan chunk and the signature starts 30 bytes
|
||||
// before the boundary, so only the carried-over overlap can complete the match. Quoted-
|
||||
// printable keeps the signature off the wire, so the match has to come from the stream.
|
||||
String decoded = "a".repeat(SesContentScan.CHUNK_SIZE - 30) + SIGNATURE + "a".repeat(4096);
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n"
|
||||
+ "--b\r\nContent-Type: application/octet-stream\r\n"
|
||||
+ "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
|
||||
+ quotedPrintable(decoded) + "\r\n--b--\r\n";
|
||||
byte[] bytes = mime.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertFalse(new String(bytes, StandardCharsets.ISO_8859_1).contains(SIGNATURE),
|
||||
"the fixture must only carry the signature in encoded form");
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamScanFindsASignatureStraddlingTheChunkBoundary() throws IOException {
|
||||
// A ByteArrayInputStream hands back exactly the requested chunk, so the signature that
|
||||
// starts 30 bytes before CHUNK_SIZE is split across two reads and only the carried-over
|
||||
// overlap can complete the match. The MIME test above cannot pin this down because the
|
||||
// decoder's own read sizes do not line up with the scan chunk.
|
||||
byte[] signature = SIGNATURE.getBytes(StandardCharsets.US_ASCII);
|
||||
int prefix = SesContentScan.CHUNK_SIZE - 30;
|
||||
byte[] data = new byte[prefix + signature.length + 10];
|
||||
Arrays.fill(data, (byte) 'a');
|
||||
System.arraycopy(signature, 0, data, prefix, signature.length);
|
||||
|
||||
assertTrue(SesContentScan.streamContains(new ByteArrayInputStream(data), signature));
|
||||
assertFalse(SesContentScan.streamContains(new ByteArrayInputStream(new byte[prefix + 200]), signature));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureInsideAForwardedMessageIsFlagged() {
|
||||
// message/rfc822 parts surface as an embedded Message body, not a leaf; the signature sits
|
||||
// in a quoted-printable attachment of the forwarded message, two levels down, so only the
|
||||
// walk into the embedded message can find it.
|
||||
String inner = "From: c@example.com\r\nTo: d@example.com\r\nSubject: fwd\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"i\"\r\n\r\n"
|
||||
+ "--i\r\nContent-Type: text/plain\r\n\r\nforwarded\r\n"
|
||||
+ "--i\r\nContent-Type: application/octet-stream\r\n"
|
||||
+ "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
|
||||
+ quotedPrintable(SIGNATURE) + "\r\n--i--\r\n";
|
||||
String outer = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"o\"\r\n\r\n"
|
||||
+ "--o\r\nContent-Type: text/plain\r\n\r\nsee forwarded message\r\n"
|
||||
+ "--o\r\nContent-Type: message/rfc822\r\n\r\n" + inner + "\r\n--o--\r\n";
|
||||
byte[] bytes = outer.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertFalse(new String(bytes, StandardCharsets.ISO_8859_1).contains(SIGNATURE),
|
||||
"the fixture must only carry the signature in encoded form");
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureInsideAnEncodedWordSubjectIsFoundOnlyByDecodingTheHeader() {
|
||||
// An RFC 2047 encoded word carries at most 75 characters, so the signature spans two words
|
||||
// and each restarts the base64 alignment: neither the plain nor the base64 wire scan sees it.
|
||||
byte[] signature = SIGNATURE.getBytes(StandardCharsets.US_ASCII);
|
||||
String first = Base64.getEncoder().encodeToString(Arrays.copyOfRange(signature, 0, 45));
|
||||
String second = Base64.getEncoder().encodeToString(Arrays.copyOfRange(signature, 45, signature.length));
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\n"
|
||||
+ "Subject: =?UTF-8?B?" + first + "?=\r\n =?UTF-8?B?" + second + "?=\r\n\r\nclean\r\n";
|
||||
byte[] bytes = mime.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertFalse(new String(bytes, StandardCharsets.ISO_8859_1).contains(SIGNATURE),
|
||||
"the fixture must only carry the signature in encoded form");
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64SpellingNothingWouldDecodeIsNotFlagged() {
|
||||
// Neither a header value nor a text body without a transfer encoding is decoded by a mail
|
||||
// client, so the base64 spelling of the signature in either is plain text, not the file.
|
||||
String encoded = Base64.getEncoder().encodeToString(SIGNATURE.getBytes(StandardCharsets.US_ASCII));
|
||||
String mime = "From: a@example.com\r\nX-Probe: " + encoded + "\r\n\r\n" + encoded + "\r\n";
|
||||
|
||||
assertEquals(Result.CLEAN, SesContentScan.scan(mime.getBytes(StandardCharsets.US_ASCII)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signatureInsideABase64AttachmentIsFoundByDecodingThePart() {
|
||||
String encoded = Base64.getMimeEncoder().encodeToString(SIGNATURE.getBytes(StandardCharsets.US_ASCII));
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n"
|
||||
+ "--b\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\n\r\n"
|
||||
+ encoded + "\r\n--b--\r\n";
|
||||
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(mime.getBytes(StandardCharsets.US_ASCII)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thousandsOfForwardingLayersAreWalked() {
|
||||
byte[] bytes = nestedForwards(3_000);
|
||||
|
||||
assertFalse(new String(bytes, StandardCharsets.ISO_8859_1).contains(SIGNATURE));
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nestingBeyondTheParserIsRefusedNotCrashed() {
|
||||
// mime4j nests a reader per forwarded message and overflows the stack at this depth; the
|
||||
// scan must say so rather than throw an Error or call the message clean.
|
||||
assertEquals(Result.UNREADABLE, SesContentScan.scan(nestedForwards(100_000)));
|
||||
}
|
||||
|
||||
// message/rfc822 layers around a quoted-printable copy of the signature, so nothing on the
|
||||
// wire matches and only a walk to the innermost part can find it.
|
||||
private static byte[] nestedForwards(int depth) {
|
||||
StringBuilder mime = new StringBuilder();
|
||||
for (int i = 0; i < depth; i++) {
|
||||
mime.append("Content-Type: message/rfc822\r\n\r\n");
|
||||
}
|
||||
mime.append("Content-Type: text/plain\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||
.append(quotedPrintable(SIGNATURE)).append("\r\n");
|
||||
return mime.toString().getBytes(StandardCharsets.US_ASCII);
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64OfCleanContentIsNotFlagged() {
|
||||
byte[] blob = Base64.getMimeEncoder().encode("nothing to see here, just a long enough clean payload".getBytes(StandardCharsets.US_ASCII));
|
||||
|
||||
assertEquals(Result.CLEAN, SesContentScan.scan(blob));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanMimeWithAnAttachmentIsNotFlagged() {
|
||||
String encoded = Base64.getEncoder().encodeToString("just a file".getBytes(StandardCharsets.US_ASCII));
|
||||
String mime = "From: a@example.com\r\nTo: b@example.com\r\nSubject: x\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n"
|
||||
+ "--b\r\nContent-Type: text/plain\r\n\r\nhello\r\n"
|
||||
+ "--b\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\n\r\n"
|
||||
+ encoded + "\r\n--b--\r\n";
|
||||
|
||||
assertEquals(Result.CLEAN, SesContentScan.scan(mime.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bytesThatAreNotMimeFallBackToARawScan() {
|
||||
byte[] signature = SIGNATURE.getBytes(StandardCharsets.US_ASCII);
|
||||
byte[] bytes = new byte[signature.length + 2];
|
||||
bytes[0] = 0;
|
||||
bytes[1] = 1;
|
||||
System.arraycopy(signature, 0, bytes, 2, signature.length);
|
||||
|
||||
assertEquals(Result.REJECTED, SesContentScan.scan(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyInputIsNotFlagged() {
|
||||
assertEquals(Result.CLEAN, SesContentScan.scan(new byte[0]));
|
||||
assertEquals(Result.CLEAN, SesContentScan.scan((byte[]) null));
|
||||
}
|
||||
|
||||
// Escapes the characters of the signature that quoted-printable may encode, and soft-wraps,
|
||||
// so the wire carries no plain copy of the signature.
|
||||
private static String quotedPrintable(String decoded) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < decoded.length(); i += 60) {
|
||||
if (i > 0) {
|
||||
out.append("=\r\n");
|
||||
}
|
||||
out.append(decoded.substring(i, Math.min(decoded.length(), i + 60))
|
||||
.replace("!", "=21").replace("$", "=24"));
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
+90
-9
@@ -1650,9 +1650,90 @@ class SesEventPublishingV2IntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(34)
|
||||
void eicarAttachment_isAcceptedThenRejected() throws Exception {
|
||||
// The signature comes from the scanner itself so it never appears in test source.
|
||||
String encoded = Base64.getEncoder().encodeToString(
|
||||
SesContentScan.signature().getBytes(StandardCharsets.US_ASCII));
|
||||
String mime = "From: " + SENDER + "\r\nTo: success@simulator.amazonses.com\r\nSubject: scan\r\n"
|
||||
+ "X-SES-MESSAGE-TAGS: probe=leak\r\n"
|
||||
+ "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n"
|
||||
+ "--b\r\nContent-Type: text/plain\r\n\r\nsee attachment\r\n"
|
||||
+ "--b\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\n\r\n"
|
||||
+ encoded + "\r\n--b--\r\n";
|
||||
String rawB64 = Base64.getEncoder().encodeToString(
|
||||
mime.getBytes(StandardCharsets.UTF_8));
|
||||
drainQueue();
|
||||
String messageId = given().contentType("application/json").header("Authorization", SES_AUTH)
|
||||
.body("""
|
||||
{"FromEmailAddress": "%s",
|
||||
"Destination": {"ToAddresses": ["success@simulator.amazonses.com"]},
|
||||
"ConfigurationSetName": "%s",
|
||||
"Content": {"Raw": {"Data": "%s"}}}
|
||||
""".formatted(SENDER, CS, rawB64))
|
||||
.when().post("/v2/email/outbound-emails").then().statusCode(200)
|
||||
.extract().jsonPath().getString("MessageId");
|
||||
|
||||
List<JsonNode> events = receiveSesEvents(2);
|
||||
assertEquals(2, events.size(), "expected Send and Reject, and no Delivery");
|
||||
assertTrue(events.stream().anyMatch(e -> "Send".equals(e.path("eventType").asText())));
|
||||
JsonNode reject = events.stream()
|
||||
.filter(e -> "Reject".equals(e.path("eventType").asText()))
|
||||
.findFirst().orElseThrow();
|
||||
assertEquals("Bad content", reject.path("reject").path("reason").asText());
|
||||
// Nothing read off the message is published: not the subject the parser found, not the tags
|
||||
// from its X-SES-MESSAGE-TAGS header, not the recipient lists from its To and Cc headers.
|
||||
// Only the From the request supplied stays, and mail.destination carries the envelope.
|
||||
for (JsonNode event : events) {
|
||||
JsonNode mail = event.path("mail");
|
||||
String type = event.path("eventType").asText();
|
||||
assertTrue(mail.path("commonHeaders").path("subject").isMissingNode(),
|
||||
type + " must not carry the subject read off the message");
|
||||
assertTrue(mail.path("tags").path("probe").isMissingNode(),
|
||||
type + " must not carry the raw header tag");
|
||||
assertEquals(0, mail.path("commonHeaders").path("to").size(),
|
||||
type + " must not carry the recipients read off the message");
|
||||
for (JsonNode header : mail.path("headers")) {
|
||||
assertEquals("From", header.path("name").asText(),
|
||||
type + " must not carry header " + header.path("name").asText());
|
||||
}
|
||||
assertEquals("success@simulator.amazonses.com", mail.path("destination").path(0).asText());
|
||||
}
|
||||
|
||||
// The request's own tags, validated by the controller, stay on both events.
|
||||
drainQueue();
|
||||
given().contentType("application/json").header("Authorization", SES_AUTH)
|
||||
.body("""
|
||||
{"FromEmailAddress": "%s",
|
||||
"Destination": {"ToAddresses": ["success@simulator.amazonses.com"]},
|
||||
"ConfigurationSetName": "%s",
|
||||
"EmailTags": [{"Name": "kept", "Value": "yes"}],
|
||||
"Content": {"Raw": {"Data": "%s"}}}
|
||||
""".formatted(SENDER, CS, rawB64))
|
||||
.when().post("/v2/email/outbound-emails").then().statusCode(200);
|
||||
List<JsonNode> tagged = receiveSesEvents(2);
|
||||
assertEquals(2, tagged.size(), "expected Send and Reject");
|
||||
for (JsonNode event : tagged) {
|
||||
assertEquals("yes", event.path("mail").path("tags").path("kept").path(0).asText(),
|
||||
event.path("eventType").asText() + " must keep the request tag");
|
||||
assertTrue(event.path("mail").path("tags").path("probe").isMissingNode());
|
||||
}
|
||||
|
||||
// The stored record keeps the marker and its envelope but no copy of the content, so it
|
||||
// is listed in neither the raw shape (no RawData) nor the Simple one (no Subject or Body).
|
||||
given().header("Authorization", SES_AUTH)
|
||||
.when().get("/_aws/ses?id=" + messageId).then().statusCode(200)
|
||||
.body("messages[0].RejectReason", equalTo("Bad content"))
|
||||
.body("messages[0]", hasKey("Destination"))
|
||||
.body("messages[0]", not(hasKey("RawData")))
|
||||
.body("messages[0]", not(hasKey("Subject")))
|
||||
.body("messages[0]", not(hasKey("Body")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(35)
|
||||
void eicarInSimpleBody_isAcceptedThenRejectedAndStoredWithoutItsContent() throws Exception {
|
||||
void eicarInSimpleBody_isAcceptedThenRejectedAndStoredWithoutTheBody() throws Exception {
|
||||
drainQueue();
|
||||
String messageId = given().contentType("application/json").header("Authorization", SES_AUTH)
|
||||
.body("""
|
||||
@@ -1671,21 +1752,20 @@ class SesEventPublishingV2IntegrationTest {
|
||||
assertTrue(events.stream().anyMatch(e -> "Reject".equals(e.path("eventType").asText())));
|
||||
assertTrue(events.stream().noneMatch(e -> "Delivery".equals(e.path("eventType").asText())));
|
||||
|
||||
// The record keeps the reason and the envelope, but none of the scanned content.
|
||||
given().header("Authorization", SES_AUTH)
|
||||
.when().get("/_aws/ses?id=" + messageId).then().statusCode(200)
|
||||
.body("messages[0].RejectReason", equalTo("Bad content"))
|
||||
.body("messages[0].Subject", nullValue())
|
||||
.body("messages[0].Body.text_part", nullValue())
|
||||
.body("messages[0].Body.html_part", nullValue())
|
||||
.body("messages[0]", not(hasKey("Headers")));
|
||||
.body("messages[0]", hasKey("Destination"))
|
||||
.body("messages[0]", not(hasKey("Subject")))
|
||||
.body("messages[0]", not(hasKey("Body")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(36)
|
||||
void eicarInSimpleSubjectOrHeader_isRejectedLikeARawSend() throws Exception {
|
||||
// The signature can sit in the subject or in a caller-supplied header name or value as
|
||||
// well as in a body, so the simple path scans all of them, and the record keeps none.
|
||||
// A raw send is scanned as a whole, header block included, so the simple path has to
|
||||
// cover the subject and the caller's header names and values as well or the two paths
|
||||
// disagree; and the stored record keeps none of them.
|
||||
String signature = SesContentScan.signature().replace("\\", "\\\\");
|
||||
for (String content : new String[] {
|
||||
"\"Subject\": {\"Data\": \"" + signature + "\"}, \"Body\": {\"Text\": {\"Data\": \"clean\"}}",
|
||||
@@ -1723,7 +1803,8 @@ class SesEventPublishingV2IntegrationTest {
|
||||
given().header("Authorization", SES_AUTH)
|
||||
.when().get("/_aws/ses?id=" + messageId).then().statusCode(200)
|
||||
.body("messages[0].RejectReason", equalTo("Bad content"))
|
||||
.body("messages[0].Subject", nullValue())
|
||||
.body("messages[0]", not(hasKey("Subject")))
|
||||
.body("messages[0]", not(hasKey("Body")))
|
||||
.body("messages[0]", not(hasKey("Headers")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
|
||||
import io.github.hectorvent.floci.services.ses.model.SentEmail;
|
||||
@@ -14,6 +15,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -111,6 +113,35 @@ class SesServiceSmtpTest {
|
||||
assertEquals("bounces@example.com", storedEmail(messageId).getReturnPath());
|
||||
}
|
||||
|
||||
@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.
|
||||
String raw = "From: from@example.com\r\nTo: to@example.com\r\nReturn-Path: <bounces@evil.example>\r\n"
|
||||
+ "Subject: " + SesContentScan.signature() + "\r\n\r\nbody\r\n";
|
||||
|
||||
String messageId = service.sendRawEmail("from@example.com", List.of("to@example.com"), raw,
|
||||
"bounces@example.com", null, List.of(), null, "us-east-1");
|
||||
|
||||
assertEquals("bounces@example.com", storedEmail(messageId).getReturnPath());
|
||||
assertEquals("Bad content", storedEmail(messageId).getRejectReason());
|
||||
verify(smtpRelay, never()).relayRaw(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendRawEmail_unreadableMessage_isRefusedNotStoredNotRelayed() {
|
||||
// Nested past what mime4j can parse: the content cannot be scanned, so the send is refused
|
||||
// rather than recorded and relayed unseen.
|
||||
String raw = "Content-Type: message/rfc822\r\n\r\n".repeat(100_000) + "Subject: deep\r\n\r\nx\r\n";
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendRawEmail("from@example.com",
|
||||
List.of("to@example.com"), raw, null, null, List.of(), null, "us-east-1"));
|
||||
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
assertTrue(sentEmails.listAll().isEmpty());
|
||||
verify(smtpRelay, never()).relayRaw(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendRawEmail_callsRelayRaw() {
|
||||
String messageId = service.sendRawEmail("from@example.com",
|
||||
|
||||
@@ -389,6 +389,18 @@ class SmtpRelayTest {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user