diff --git a/docs/services/ses.md b/docs/services/ses.md index 92e58e4cd6..d2f8deeb77 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -229,6 +229,29 @@ Alongside the LocalStack fields, each captured message carries a `RejectReason` and none of its content: no `Subject`, `Body`, `Headers`, `ReplyToAddresses` or `RawData`, in neither the Simple nor the raw shape. +A v2 `SendEmail` with `Content.Simple.Attachments` keeps the Simple shape and +adds two fields: `Attachments`, one entry per attachment with `FileName`, +`ContentType`, `ContentDisposition` (`ATTACHMENT` or `INLINE`), `ContentId` +(angle-bracketed, when given), `ContentDescription` (when given) and `Size` +in decoded bytes; and `RawData`, the assembled MIME message, base64-encoded, +as the request addressed it: every `To` and `Cc` recipient the request named, +including any suppression later kept from delivery, and no `Bcc` header. The content scan runs over +that MIME message as it does for a raw send. An `INLINE` attachment with a +`ContentId` is placed in a `multipart/related` part next to the HTML body. +A `ContentType` that is not given is guessed from the file name, falling back +to `application/octet-stream`. +`ContentTransferEncoding` is validated but not applied: every attachment part +is base64-encoded, whatever the request asks for. + +A message carrying an attachment whose file name ends in one of the +[unsupported attachment types](https://docs.aws.amazon.com/ses/latest/dg/attachments.html#mime-types) +(`.exe`, `.bat`, `.js`, `.vbs` and the rest of that list, in any case) is +refused with `MessageRejected` and nothing is recorded, for a Simple send with +attachments and a raw send alike. The name is read from `Content-Disposition`, +or from the `name` parameter of `Content-Type`, in every MIME part, including +a forwarded message. AWS does not document the error, so the message text is +Floci's own. + ## Examples ```bash diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesContentScan.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesContentScan.java index a0f2214d00..35ad675b17 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesContentScan.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesContentScan.java @@ -8,15 +8,19 @@ import org.apache.james.mime4j.dom.Header; import org.apache.james.mime4j.dom.Message; import org.apache.james.mime4j.dom.Multipart; import org.apache.james.mime4j.dom.SingleBody; +import org.apache.james.mime4j.dom.field.ContentTypeField; import org.apache.james.mime4j.stream.Field; import org.jboss.logging.Logger; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; +import java.util.Locale; +import java.util.Set; /** * The content scan SES runs on every accepted message. Real SES scans for viruses and rejects the @@ -45,6 +49,16 @@ final class SesContentScan { // Decoded parts are read as a stream in chunks of this size with a rolling overlap, so the // scan itself never needs a whole part in one array, whatever the parser buffers. static final int CHUNK_SIZE = 64 * 1024; + // The developer guide's "SES unsupported attachment types", matched case-insensitively. + private static final Set BLOCKED_EXTENSIONS = Set.of( + "ade", "adp", "app", "asp", "bas", "bat", "cer", "chm", "cmd", "com", "cpl", "crt", "csh", + "der", "exe", "fxp", "gadget", "hlp", "hta", "inf", "ins", "isp", "its", "js", "jse", "ksh", + "lib", "lnk", "mad", "maf", "mag", "mam", "maq", "mar", "mas", "mat", "mau", "mav", "maw", + "mda", "mdb", "mde", "mdt", "mdw", "mdz", "msc", "msh", "msh1", "msh2", "mshxml", "msh1xml", + "msh2xml", "msi", "msp", "mst", "ops", "pcd", "pif", "plg", "prf", "prg", "reg", "scf", "scr", + "sct", "shb", "shs", "sys", "ps1", "ps1xml", "ps2", "ps2xml", "psc1", "psc2", "tmp", "url", + "vb", "vbe", "vbs", "vps", "vsmacros", "vss", "vst", "vsw", "vxd", "ws", "wsc", "wsf", "wsh", + "xnk"); private SesContentScan() {} @@ -107,14 +121,68 @@ final class SesContentScan { return scan(mime, SmtpRelay.parseMime(mime)); } + /** + * True when any part of the parsed message, forwarded messages included, carries a file name + * whose extension SES refuses. The name is read from {@code Content-Disposition} and, failing + * that, from the {@code name} parameter of {@code Content-Type}. + */ + static boolean hasBlockedAttachment(Message parsed) { + if (parsed == null) { + return false; + } + try { + return anyPart(parsed, entity -> isBlockedFileName(fileName(entity))); + } catch (IOException e) { + // The check reads only headers, so the walk has no stream to fail on. + throw new UncheckedIOException(e); + } + } + + static boolean isBlockedFileName(String fileName) { + if (fileName == null) { + return false; + } + int dot = fileName.lastIndexOf('.'); + return dot >= 0 && BLOCKED_EXTENSIONS.contains(fileName.substring(dot + 1).trim().toLowerCase(Locale.ROOT)); + } + + private static String fileName(Entity entity) { + String name = entity.getFilename(); + if (name == null && entity.getHeader() != null + && entity.getHeader().getField("Content-Type") instanceof ContentTypeField contentType) { + name = contentType.getParameter("name"); + } + return name == null ? null : DecoderUtil.decodeEncodedWords(name, DecodeMonitor.SILENT); + } + + private static boolean entityContains(Entity root) throws IOException { + return anyPart(root, entity -> { + if (headerContains(entity.getHeader())) { + return true; + } + if (entity.getBody() instanceof SingleBody single) { + try (InputStream in = single.getInputStream()) { + return streamContains(in, SIGNATURE); + } + } + return false; + }); + } + + /** A check run on one part of a message by {@link #anyPart}. */ + @FunctionalInterface + private interface PartCheck { + boolean test(Entity entity) throws IOException; + } + // An explicit worklist rather than recursion: a message can nest forwarded messages and // multiparts as deep as its size allows, and the stack must not be the limit. - private static boolean entityContains(Entity root) throws IOException { + private static boolean anyPart(Entity root, PartCheck check) throws IOException { Deque pending = new ArrayDeque<>(); pending.push(root); while (!pending.isEmpty()) { Entity entity = pending.pop(); - if (headerContains(entity.getHeader())) { + if (check.test(entity)) { return true; } Body body = entity.getBody(); @@ -125,12 +193,6 @@ final class SesContentScan { for (Entity part : multipart.getBodyParts()) { pending.push(part); } - } else if (body instanceof SingleBody single) { - try (InputStream in = single.getInputStream()) { - if (streamContains(in, SIGNATURE)) { - return true; - } - } } } return false; diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java index 66160ed7fa..8e75050197 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ses; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.services.ses.model.SentAttachment; import io.github.hectorvent.floci.services.ses.model.SentEmail; import jakarta.inject.Inject; import jakarta.ws.rs.DELETE; @@ -91,6 +92,18 @@ public class SesInspectionController { } else { body.putNull("html_part"); } + + // A Simple send with attachments also carries the assembled MIME message, base64 + // encoded like the RawData of a raw send, and a summary of each attachment. + if (email.getAttachments() != null && !email.getAttachments().isEmpty()) { + ArrayNode attachments = node.putArray("Attachments"); + for (SentAttachment attachment : email.getAttachments()) { + attachments.add(objectMapper.valueToTree(attachment)); + } + if (email.getMimeData() != null) { + node.put("RawData", email.getMimeData()); + } + } } if (email.getHeaders() != null && !email.getHeaders().isEmpty()) { 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 9d17e11d78..f294e44c4b 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 @@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult; import io.github.hectorvent.floci.services.ses.model.EmailContent; import io.github.hectorvent.floci.services.ses.model.EmailTemplate; import io.github.hectorvent.floci.services.ses.model.ListManagementOptions; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.github.hectorvent.floci.services.ses.model.SendBulkEmailRequest; @@ -33,6 +34,8 @@ import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; import java.util.Collections; import java.util.List; import java.util.Map; @@ -149,10 +152,13 @@ public class SesSendController { String bodyHtml = simple.path("Body").path("Html").path("Data").asText(null); List additionalHeaders = parseHeadersArray(simple.path("Headers"), "content.simple.headers"); + List attachments = + parseAttachmentsArray(simple.path("Attachments"), "content.simple.attachments"); sesService.checkTenantSendAccess(tenantName, fromEmailAddress, configurationSetName, null, regionResolver.getAccountId(), region); messageId = sesService.sendEmail(sendRequest - .content(new EmailContent.Simple(subject, bodyText, bodyHtml, additionalHeaders)) + .content(new EmailContent.Simple(subject, bodyText, bodyHtml, additionalHeaders, + attachments)) .build()); } else if (content.has("Template")) { if (fromEmailAddress == null || fromEmailAddress.isBlank()) { @@ -485,10 +491,118 @@ public class SesSendController { return out; } - private AwsException missingHeaderMember(String location, int index, String member) { + /** + * Parse a V2 SES {@code Content.Simple.Attachments} array. {@code FileName} and + * {@code RawContent} are required, the string members carry the documented length limits and + * the two enum members their value sets; a violation is reported with a Smithy constraint + * message anchored at {@code location} and the offending 1-based index, as for headers. + */ + private List parseAttachmentsArray(JsonNode attachmentsNode, String location) { + if (attachmentsNode.isMissingNode() || attachmentsNode.isNull()) { + return List.of(); + } + if (!attachmentsNode.isArray()) { + throw new AwsException("BadRequestException", "Attachments must be an array.", 400); + } + List out = new ArrayList<>(); + int index = 1; + for (JsonNode node : attachmentsNode) { + if (!node.isObject()) { + throw new AwsException("BadRequestException", + "Attachments entries must be JSON objects.", 400); + } + String memberPrefix = location + "." + index + ".member."; + String fileName = attachmentString(node, "FileName", memberPrefix + "fileName", 0, 255); + if (fileName == null) { + throw constraintViolation(null, memberPrefix + "fileName", "Member must not be null"); + } + JsonNode rawNode = node.get("RawContent"); + if (rawNode == null || rawNode.isNull()) { + throw constraintViolation(null, memberPrefix + "rawContent", "Member must not be null"); + } + if (!rawNode.isTextual()) { + throw new AwsException("BadRequestException", + "RawContent must be a base64-encoded string.", 400); + } + byte[] rawContent; + try { + rawContent = Base64.getDecoder().decode(rawNode.textValue()); + } catch (IllegalArgumentException e) { + throw new AwsException("BadRequestException", + "RawContent is not valid base64: " + e.getMessage(), 400); + } + String contentType = attachmentHeaderValue(node, "ContentType", memberPrefix + "contentType", 1, 78); + String description = attachmentHeaderValue(node, "ContentDescription", + memberPrefix + "contentDescription", 0, 1000); + String contentId = attachmentHeaderValue(node, "ContentId", memberPrefix + "contentId", 1, 78); + MessageAttachment.Disposition disposition = attachmentEnum(node, "ContentDisposition", + memberPrefix + "contentDisposition", MessageAttachment.Disposition.class); + MessageAttachment.TransferEncoding transferEncoding = attachmentEnum(node, + "ContentTransferEncoding", memberPrefix + "contentTransferEncoding", + MessageAttachment.TransferEncoding.class); + out.add(new MessageAttachment(fileName, rawContent, contentType, disposition, description, + contentId, transferEncoding)); + index++; + } + return List.copyOf(out); + } + + private static String attachmentString(JsonNode node, String member, String path, int min, int max) { + JsonNode value = node.get(member); + if (value == null || value.isNull()) { + return null; + } + if (!value.isTextual()) { + throw new AwsException("BadRequestException", member + " must be a string.", 400); + } + String text = value.textValue(); + if (text.length() < min) { + throw constraintViolation(text, path, "Member must have length greater than or equal to " + min); + } + if (text.length() > max) { + throw constraintViolation(text, path, "Member must have length less than or equal to " + max); + } + return text; + } + + /** + * An attachment member written verbatim into the part's MIME headers. The mail encoder only + * encodes the file name, so a CR or LF here would add header lines to the part; it is refused + * by the same rule {@link MessageHeader#isSafe()} applies to custom headers. + */ + private static String attachmentHeaderValue(JsonNode node, String member, String path, int min, int max) { + String text = attachmentString(node, member, path, min, max); + if (text != null && !MessageHeader.noCrlf(text)) { + throw new AwsException("BadRequestException", + member + " must not contain line breaks.", 400); + } + return text; + } + + private static > E attachmentEnum(JsonNode node, String member, String path, + Class type) { + String text = attachmentString(node, member, path, 0, Integer.MAX_VALUE); + if (text == null) { + return null; + } + for (E constant : type.getEnumConstants()) { + if (constant.name().equals(text)) { + return constant; + } + } + throw constraintViolation(text, path, + "Member must satisfy enum value set: " + Arrays.toString(type.getEnumConstants())); + } + + private static AwsException constraintViolation(String value, String path, String constraint) { + String subject = value == null ? "Value" : "Value '" + value + "'"; return new AwsException("BadRequestException", - "1 validation error detected: Value at '" + location + "." + index + ".member." + member - + "' failed to satisfy constraint: Member must not be null", 400); + "1 validation error detected: " + subject + " at '" + path + + "' failed to satisfy constraint: " + constraint, 400); + } + + private static AwsException missingHeaderMember(String location, int index, String member) { + return constraintViolation(null, location + "." + index + ".member." + member, "Member must not be null"); } private static ListManagementOptions parseListManagementOptions(JsonNode node) { 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 8f4110ec9b..91f590ed6e 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 @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.services.ses; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; @@ -15,28 +17,30 @@ import io.github.hectorvent.floci.services.ses.model.DeliveryOptions; import io.github.hectorvent.floci.services.ses.model.EmailContent; import io.github.hectorvent.floci.services.ses.model.Identity; import io.github.hectorvent.floci.services.ses.model.ListManagementOptions; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.github.hectorvent.floci.services.ses.model.SendBulkEmailRequest; import io.github.hectorvent.floci.services.ses.model.SendEmailRequest; -import io.github.hectorvent.floci.services.ses.model.Topic; -import io.github.hectorvent.floci.services.ses.model.TrackingOptions; +import io.github.hectorvent.floci.services.ses.model.SentAttachment; import io.github.hectorvent.floci.services.ses.model.SentEmail; -import io.github.hectorvent.floci.services.ses.model.Tenant; -import io.github.hectorvent.floci.services.ses.model.TenantResourceAssociation; import io.github.hectorvent.floci.services.ses.model.SuppressedDestination; import io.github.hectorvent.floci.services.ses.model.SuppressionOptions; import io.github.hectorvent.floci.services.ses.model.Tag; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.services.ses.model.Tenant; +import io.github.hectorvent.floci.services.ses.model.TenantResourceAssociation; +import io.github.hectorvent.floci.services.ses.model.Topic; +import io.github.hectorvent.floci.services.ses.model.TrackingOptions; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.apache.james.mime4j.dom.Message; import org.jboss.logging.Logger; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; +import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; @@ -286,7 +290,8 @@ public class SesService { content = new EmailContent.Simple(content.subject(), replaceUnsubscribePlaceholder(content.bodyText(), url), replaceUnsubscribePlaceholder(content.bodyHtml(), url), - withUnsubscribeHeaders(content.headers(), url)); + withUnsubscribeHeaders(content.headers(), url), + content.attachments()); request = request.toBuilder().content(content).build(); } @@ -305,6 +310,21 @@ public class SesService { boolean rejected = SesContentScan.containsTestVirus(content.subject(), content.bodyText(), content.bodyHtml()) || SesContentScan.containsTestVirus(headerText(content.headers())); + // Attachments make the message multipart, so it is assembled once as the MIME the relay + // delivers (Bcc stays off it, as on the wire) and scanned the way a raw send is. + byte[] mime = null; + if (content.hasAttachments()) { + mime = SmtpRelay.encodeMime(simpleRelayMessage(request, content, effectiveReturnPath, messageId, + request.toAddresses(), request.ccAddresses(), null)); + Message parsedMime = SmtpRelay.parseMime(mime); + rejectBlockedAttachments(parsedMime); + SesContentScan.Result scan = SesContentScan.scan(mime, parsedMime); + if (scan == SesContentScan.Result.UNREADABLE) { + LOG.warnv("SES content scan could not parse the assembled message, only its bytes were checked: " + + "messageId={0}", messageId); + } + rejected = rejected || scan == SesContentScan.Result.REJECTED; + } SentEmail email = new SentEmail(messageId, region, source, request.toAddresses(), request.ccAddresses(), request.bccAddresses(), request.replyToAddresses(), content.subject(), content.bodyText(), content.bodyHtml()); @@ -314,6 +334,10 @@ public class SesService { if (content.hasHeaders()) { email.setHeaders(content.headers()); } + if (mime != null) { + email.setAttachments(sentAttachments(content.attachments())); + email.setMimeData(Base64.getEncoder().encodeToString(mime)); + } email.setEmailTags(request.emailTags()); email.setInsights(SesMessageInsights.build(envelope, SesRecipientEvents.classify(envelope, suppressedReasons, rejected), email.getSentAt())); @@ -326,18 +350,8 @@ public class SesService { List relayedCc = filterUnsuppressed(request.ccAddresses(), suppressedReasons); List relayedBcc = filterUnsuppressed(request.bccAddresses(), suppressedReasons); if (!rejected && sizeOf(relayedTo) + sizeOf(relayedCc) + sizeOf(relayedBcc) > 0) { - smtpRelay.relay(SmtpRelay.RelayMessage.builder(source) - .returnPath(effectiveReturnPath) - .to(relayedTo) - .cc(relayedCc) - .bcc(relayedBcc) - .replyTo(request.replyToAddresses()) - .subject(content.subject()) - .bodyText(content.bodyText()) - .bodyHtml(content.bodyHtml()) - .headers(content.headers()) - .messageId(messageId) - .build()); + smtpRelay.relay(simpleRelayMessage(request, content, effectiveReturnPath, messageId, + relayedTo, relayedCc, relayedBcc)); } else { LOG.infov("SES email accepted but not relayed ({0}): messageId={1}", rejected ? "content rejected" : "all recipients suppressed", messageId); @@ -353,6 +367,38 @@ public class SesService { return messageId; } + private static SmtpRelay.RelayMessage simpleRelayMessage(SendEmailRequest request, EmailContent.Simple content, + String returnPath, String messageId, + List to, List cc, + List bcc) { + return SmtpRelay.RelayMessage.builder(request.source()) + .returnPath(returnPath) + .to(to) + .cc(cc) + .bcc(bcc) + .replyTo(request.replyToAddresses()) + .subject(content.subject()) + .bodyText(content.bodyText()) + .bodyHtml(content.bodyHtml()) + .headers(content.headers()) + .messageId(messageId) + .attachments(content.hasAttachments() ? content.attachments() : null) + .build(); + } + + private static List sentAttachments(List attachments) { + List out = new ArrayList<>(); + for (MessageAttachment attachment : attachments) { + String contentId = attachment.contentId() == null || attachment.contentId().isBlank() + ? null + : SmtpRelay.formatContentId(attachment.contentId()); + out.add(new SentAttachment(attachment.fileName(), SmtpRelay.attachmentContentType(attachment), + attachment.effectiveDisposition().name(), contentId, attachment.contentDescription(), + attachment.size())); + } + return List.copyOf(out); + } + private String sendRawEmail(SendEmailRequest request, EmailContent.Raw raw) { String source = request.source(); String region = request.region(); @@ -421,6 +467,7 @@ public class SesService { if (scan == SesContentScan.Result.UNREADABLE) { throw new AwsException("InvalidParameterValue", "Raw message could not be parsed.", 400); } + rejectBlockedAttachments(parsed.message()); boolean rejected = scan == SesContentScan.Result.REJECTED; // AWS routes bounces to the Return-Path carried by the message, falling back to the // request's return path and then the sender. A rejected message keeps nothing read off its @@ -1453,6 +1500,14 @@ public class SesService { } } + // SES refuses the send outright for an attachment type it does not accept, unlike the content + // scan, which accepts the message and rejects it afterwards. + private static void rejectBlockedAttachments(Message parsed) { + if (SesContentScan.hasBlockedAttachment(parsed)) { + throw new AwsException("MessageRejected", "Message contains invalid content.", 400); + } + } + public List sendBulkEmail(SendBulkEmailRequest request) { EmailContent.InlineTemplate template = request.defaultContent(); if (request.source() == null || request.source().isBlank()) { 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 abafa29b5d..d11d29ce4a 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 @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ses; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.vertx.core.buffer.Buffer; @@ -9,6 +10,7 @@ import io.vertx.ext.mail.MailClient; import io.vertx.ext.mail.MailConfig; import io.vertx.ext.mail.MailMessage; import io.vertx.ext.mail.StartTLSOptions; +import io.vertx.ext.mail.mailencoder.MailEncoder; import jakarta.annotation.PreDestroy; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -32,6 +34,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLConnection; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -67,6 +70,7 @@ 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 MESSAGE_ID_DOMAIN = "email.amazonses.com"; + private static final String DEFAULT_ATTACHMENT_CONTENT_TYPE = "application/octet-stream"; /** * Top-level headers of a raw message that are not copied onto the relayed message. The MIME @@ -176,7 +180,8 @@ public class SmtpRelay { List replyTo, String subject, String bodyText, String bodyHtml, List headers, - String messageId) { + String messageId, + List attachments) { public static Builder builder(String from) { return new Builder(from); @@ -194,6 +199,7 @@ public class SmtpRelay { private String bodyHtml; private List headers; private String messageId; + private List attachments; private Builder(String from) { this.from = from; @@ -209,10 +215,14 @@ public class SmtpRelay { public Builder bodyHtml(String bodyHtml) { this.bodyHtml = bodyHtml; return this; } public Builder headers(List headers) { this.headers = headers; return this; } public Builder messageId(String messageId) { this.messageId = messageId; return this; } + public Builder attachments(List attachments) { + this.attachments = attachments; + return this; + } public RelayMessage build() { return new RelayMessage(from, returnPath, to, cc, bcc, replyTo, - subject, bodyText, bodyHtml, headers, messageId); + subject, bodyText, bodyHtml, headers, messageId, attachments); } } } @@ -241,46 +251,7 @@ public class SmtpRelay { private void doRelay(RelayMessage message) { try { - MailMessage mail = new MailMessage(); - mail.setFrom(message.from()); - applyBounceAddress(mail, message.returnPath(), message.from()); - if (message.to() != null) { - mail.setTo(message.to()); - } - if (message.cc() != null) { - mail.setCc(message.cc()); - } - if (message.bcc() != null) { - mail.setBcc(message.bcc()); - } - if (message.replyTo() != null && !message.replyTo().isEmpty()) { - mail.addHeader("Reply-To", String.join(", ", message.replyTo())); - } - if (message.headers() != null) { - for (MessageHeader header : message.headers()) { - if (!header.isSafe()) { - LOG.warnv("SMTP relay: skipping a header with a blank name or CR/LF in its " - + "name/value (possible header injection) for from={0}", message.from()); - continue; - } - if (FieldName.MESSAGE_ID.equalsIgnoreCase(header.name())) { - // AWS overrides a caller-supplied Message-ID, and rejects it outright as a - // custom header on Simple / Templated content. - continue; - } - mail.addHeader(header.name(), header.value()); - } - } - applyMessageId(mail, message.messageId()); - mail.setSubject(message.subject() != null ? message.subject() : ""); - if (message.bodyText() != null) { - mail.setText(message.bodyText()); - } - if (message.bodyHtml() != null) { - mail.setHtml(message.bodyHtml()); - } - - send(mail); + send(toMailMessage(message)); LOG.debugv("SMTP relay: sent from={0}, to={1}, subject={2}", message.from(), message.to(), message.subject()); } catch (InterruptedException e) { @@ -294,6 +265,130 @@ public class SmtpRelay { } } + /** + * Encodes a structured message into the RFC 5322 bytes the relay delivers, with the encoder the + * mail client itself uses on the wire. The send path stores these bytes for a Simple send with + * attachments and runs the content scan over them. + */ + static byte[] encodeMime(RelayMessage message) { + return new MailEncoder(toMailMessage(message), MESSAGE_ID_DOMAIN).encode() + .getBytes(StandardCharsets.UTF_8); + } + + static MailMessage toMailMessage(RelayMessage message) { + MailMessage mail = new MailMessage(); + mail.setFrom(message.from()); + applyBounceAddress(mail, message.returnPath(), message.from()); + if (message.to() != null) { + mail.setTo(message.to()); + } + if (message.cc() != null) { + mail.setCc(message.cc()); + } + if (message.bcc() != null) { + mail.setBcc(message.bcc()); + } + if (message.replyTo() != null && !message.replyTo().isEmpty()) { + mail.addHeader("Reply-To", String.join(", ", message.replyTo())); + } + if (message.headers() != null) { + for (MessageHeader header : message.headers()) { + if (!header.isSafe()) { + LOG.warnv("SMTP relay: skipping a header with a blank name or CR/LF in its " + + "name/value (possible header injection) for from={0}", message.from()); + continue; + } + if (FieldName.MESSAGE_ID.equalsIgnoreCase(header.name())) { + // AWS overrides a caller-supplied Message-ID, and rejects it outright as a + // custom header on Simple / Templated content. + continue; + } + mail.addHeader(header.name(), header.value()); + } + } + applyMessageId(mail, message.messageId()); + mail.setSubject(message.subject() != null ? message.subject() : ""); + if (message.bodyText() != null) { + mail.setText(message.bodyText()); + } + if (message.bodyHtml() != null) { + mail.setHtml(message.bodyHtml()); + } + applyAttachments(mail, message.attachments()); + return mail; + } + + /** + * Maps {@code Content.Simple.Attachments} onto the outgoing message. An INLINE part with a + * {@code ContentId} joins the related part next to the HTML body so {@code cid:} references + * resolve. The encoder emits that related part only alongside an HTML body, so without one, or + * without a {@code ContentId}, the part travels as a regular attachment that keeps its inline + * disposition. + */ + private static void applyAttachments(MailMessage mail, List attachments) { + if (attachments == null || attachments.isEmpty()) { + return; + } + List regular = new ArrayList<>(); + List related = new ArrayList<>(); + for (MessageAttachment attachment : attachments) { + MailAttachment part = toMailAttachment(attachment); + boolean inline = attachment.effectiveDisposition() == MessageAttachment.Disposition.INLINE + && part.getContentId() != null + && mail.getHtml() != null; + if (inline) { + related.add(part); + } else { + regular.add(part); + } + } + if (!related.isEmpty()) { + mail.setInlineAttachment(related); + } + if (!regular.isEmpty()) { + mail.setAttachment(regular); + } + } + + private static MailAttachment toMailAttachment(MessageAttachment attachment) { + byte[] data = attachment.rawContent() == null ? new byte[0] : attachment.rawContent(); + MailAttachment part = MailAttachment.create() + .setData(Buffer.buffer(data)) + .setContentType(attachmentContentType(attachment)) + .setName(attachment.fileName()) + .setDisposition(attachment.effectiveDisposition().name().toLowerCase(Locale.ROOT)); + if (attachment.contentDescription() != null && !attachment.contentDescription().isBlank()) { + part.setDescription(attachment.contentDescription()); + } + if (attachment.contentId() != null && !attachment.contentId().isBlank()) { + part.setContentId(formatContentId(attachment.contentId())); + } + return part; + } + + /** + * The Content-Type written for an attachment: the caller's, else one guessed from the file + * name, else {@code application/octet-stream}. + */ + static String attachmentContentType(MessageAttachment attachment) { + if (attachment.contentType() != null && !attachment.contentType().isBlank()) { + return attachment.contentType(); + } + String guessed = attachment.fileName() == null + ? null + : URLConnection.guessContentTypeFromName(attachment.fileName()); + return guessed != null ? guessed : DEFAULT_ATTACHMENT_CONTENT_TYPE; + } + + /** The Content-ID header value, angle-bracketed as RFC 2392 requires. */ + static String formatContentId(String contentId) { + String trimmed = contentId.trim(); + if (trimmed.startsWith("<") && trimmed.endsWith(">")) { + return trimmed; + } + return "<" + trimmed + ">"; + } + /** * Relays a raw MIME message. Parsed with Mime4j, then sent via MailClient. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/EmailContent.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/EmailContent.java index f7ad3a7529..d07c14d5bd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/EmailContent.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/EmailContent.java @@ -12,18 +12,27 @@ import java.util.List; */ public sealed interface EmailContent { - record Simple(String subject, String bodyText, String bodyHtml, List headers) - implements EmailContent { + record Simple(String subject, String bodyText, String bodyHtml, List headers, + List attachments) implements EmailContent { + + public Simple(String subject, String bodyText, String bodyHtml, List headers) { + this(subject, bodyText, bodyHtml, headers, List.of()); + } public boolean hasHeaders() { return headers != null && !headers.isEmpty(); } + public boolean hasAttachments() { + return attachments != null && !attachments.isEmpty(); + } + public Simple withSafeHeaders() { if (!hasHeaders()) { return this; } - return new Simple(subject, bodyText, bodyHtml, headers.stream().filter(MessageHeader::isSafe).toList()); + return new Simple(subject, bodyText, bodyHtml, headers.stream().filter(MessageHeader::isSafe).toList(), + attachments); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageAttachment.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageAttachment.java new file mode 100644 index 0000000000..277d4fd896 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageAttachment.java @@ -0,0 +1,32 @@ +package io.github.hectorvent.floci.services.ses.model; + +/** + * One entry of the SES V2 {@code Content.Simple.Attachments} list, with {@code RawContent} already + * decoded from its base64 wire form. Optional members are {@code null} when the request omitted them. + */ +public record MessageAttachment(String fileName, byte[] rawContent, String contentType, + Disposition contentDisposition, String contentDescription, + String contentId, TransferEncoding contentTransferEncoding) { + + /** The {@code ContentDisposition} values AWS accepts. */ + public enum Disposition { + ATTACHMENT, + INLINE + } + + /** The {@code ContentTransferEncoding} values AWS accepts. */ + public enum TransferEncoding { + BASE64, + QUOTED_PRINTABLE, + SEVEN_BIT + } + + /** The disposition the message carries; an omitted one is a regular attachment. */ + public Disposition effectiveDisposition() { + return contentDisposition == null ? Disposition.ATTACHMENT : contentDisposition; + } + + public int size() { + return rawContent == null ? 0 : rawContent.length; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageHeader.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageHeader.java index e58cc1e85c..2575da6cfc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageHeader.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/MessageHeader.java @@ -22,7 +22,8 @@ public record MessageHeader( return name != null && !name.isBlank() && noCrlf(name) && value != null && noCrlf(value); } - private static boolean noCrlf(String s) { + /** True when {@code s} has no CR or LF, so it cannot start another MIME header line. */ + public static boolean noCrlf(String s) { return s.indexOf('\r') < 0 && s.indexOf('\n') < 0; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentAttachment.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentAttachment.java new file mode 100644 index 0000000000..028abd59d6 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentAttachment.java @@ -0,0 +1,20 @@ +package io.github.hectorvent.floci.services.ses.model; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.quarkus.runtime.annotations.RegisterForReflection; + +/** + * What the sent-email store keeps about one attachment of a Simple send: its metadata as it was + * written into the MIME message and its decoded size. The bytes themselves live in the stored MIME. + */ +@RegisterForReflection +@JsonInclude(JsonInclude.Include.NON_NULL) +public record SentAttachment( + @JsonProperty("FileName") String fileName, + @JsonProperty("ContentType") String contentType, + @JsonProperty("ContentDisposition") String contentDisposition, + @JsonProperty("ContentId") String contentId, + @JsonProperty("ContentDescription") String contentDescription, + @JsonProperty("Size") long size) { +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java index d8ce058be0..4e3d873ea8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java @@ -52,6 +52,17 @@ public class SentEmail { @JsonProperty("Headers") private List headers; + /** The attachments of a Simple send; absent for one without attachments and for raw sends. */ + @JsonProperty("Attachments") + private List attachments; + + /** + * The assembled MIME message of a Simple send with attachments, base64-encoded. Kept apart from + * {@code rawData} so the record still reads as a Simple send. + */ + @JsonProperty("MimeData") + private String mimeData; + @JsonProperty("SentAt") private Instant sentAt; @@ -158,6 +169,12 @@ public class SentEmail { public boolean isRaw() { return rawData != null; } + public List getAttachments() { return attachments; } + public void setAttachments(List attachments) { this.attachments = attachments; } + + public String getMimeData() { return mimeData; } + public void setMimeData(String mimeData) { this.mimeData = mimeData; } + public Instant getSentAt() { return sentAt; } public void setSentAt(Instant sentAt) { this.sentAt = sentAt; } @@ -176,6 +193,8 @@ public class SentEmail { this.bodyText = null; this.bodyHtml = null; this.rawData = null; + this.attachments = null; + this.mimeData = null; } public List getEmailTags() { return emailTags; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesContentScanTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesContentScanTest.java index b1aa96d9b5..ca8c51ed76 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesContentScanTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesContentScanTest.java @@ -230,4 +230,44 @@ class SesContentScanTest { } return out.toString(); } + + @Test + void blockedExtensionsMatchCaseInsensitivelyOnTheLastExtension() { + assertTrue(SesContentScan.isBlockedFileName("setup.exe")); + assertTrue(SesContentScan.isBlockedFileName("RUN.BAT")); + assertTrue(SesContentScan.isBlockedFileName("report.pdf.js")); + assertFalse(SesContentScan.isBlockedFileName("report.pdf")); + assertFalse(SesContentScan.isBlockedFileName("setup.exe.txt")); + assertFalse(SesContentScan.isBlockedFileName("exe")); + assertFalse(SesContentScan.isBlockedFileName(null)); + } + + @Test + void attachmentNamedByDispositionOrContentTypeIsBlocked() { + assertTrue(SesContentScan.hasBlockedAttachment(SmtpRelay.parseMime( + multipart("Content-Disposition: attachment; filename=\"tool.exe\"\r\n")))); + assertTrue(SesContentScan.hasBlockedAttachment(SmtpRelay.parseMime( + multipart("Content-Type: application/octet-stream; name=\"macro.vbs\"\r\n")))); + assertFalse(SesContentScan.hasBlockedAttachment(SmtpRelay.parseMime( + multipart("Content-Disposition: attachment; filename=\"report.pdf\"\r\n")))); + } + + @Test + void blockedAttachmentInsideAForwardedMessageIsFound() { + String inner = new String(multipart("Content-Disposition: attachment; filename=\"x.scr\"\r\n"), + StandardCharsets.US_ASCII); + String outer = "From: a@example.com\r\nTo: b@example.com\r\nSubject: fwd\r\n" + + "MIME-Version: 1.0\r\nContent-Type: message/rfc822\r\n\r\n" + inner; + + assertTrue(SesContentScan.hasBlockedAttachment( + SmtpRelay.parseMime(outer.getBytes(StandardCharsets.US_ASCII)))); + } + + private static byte[] multipart(String attachmentHeaders) { + 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\n" + attachmentHeaders + "\r\nAAAA\r\n--b--\r\n"; + return mime.getBytes(StandardCharsets.US_ASCII); + } } 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 88b387c11c..c9bfa1344f 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 @@ -6,7 +6,9 @@ import io.github.hectorvent.floci.services.ses.model.ConfigurationSet; import io.github.hectorvent.floci.services.ses.model.CustomVerificationEmailTemplate; import io.github.hectorvent.floci.services.ses.model.EmailContent; import io.github.hectorvent.floci.services.ses.model.InsightsEvent; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.SendEmailRequest; +import io.github.hectorvent.floci.services.ses.model.SentAttachment; import io.github.hectorvent.floci.services.ses.model.SentEmail; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -16,6 +18,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.ArgumentCaptor; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -148,6 +152,66 @@ class SesServiceSmtpTest { assertEquals(messageId, relayed.messageId()); } + private static MessageAttachment pdfAttachment(byte[] content) { + return new MessageAttachment("report.pdf", content, "application/pdf", + MessageAttachment.Disposition.ATTACHMENT, null, null, null); + } + + @Test + void sendEmail_withAttachments_relaysThemAndStoresTheAssembledMessage() { + byte[] pdf = "pdf-bytes".getBytes(StandardCharsets.UTF_8); + String messageId = service.sendEmail(SendEmailRequest.builder() + .source("from@example.com") + .toAddresses(List.of("to@example.com")) + .region("us-east-1") + .content(new EmailContent.Simple("Subject", "text", null, List.of(), + List.of(pdfAttachment(pdf)))) + .build()); + + SmtpRelay.RelayMessage relayed = capturedRelay(); + assertEquals(1, relayed.attachments().size()); + assertArrayEquals(pdf, relayed.attachments().get(0).rawContent()); + SentEmail stored = storedEmail(messageId); + assertFalse(stored.isRaw()); + assertEquals(List.of(new SentAttachment("report.pdf", "application/pdf", "ATTACHMENT", null, null, + pdf.length)), stored.getAttachments()); + String mime = new String(Base64.getDecoder().decode(stored.getMimeData()), StandardCharsets.UTF_8); + assertTrue(mime.contains("report.pdf"), mime); + } + + @Test + void sendEmail_withSignatureInAnAttachment_isRejectedAndNotRelayed() { + String messageId = service.sendEmail(SendEmailRequest.builder() + .source("from@example.com") + .toAddresses(List.of("to@example.com")) + .region("us-east-1") + .content(new EmailContent.Simple("Subject", "clean", null, List.of(), + List.of(pdfAttachment(SesContentScan.signature().getBytes(StandardCharsets.US_ASCII))))) + .build()); + + SentEmail stored = storedEmail(messageId); + assertEquals("Bad content", stored.getRejectReason()); + assertNull(stored.getAttachments()); + assertNull(stored.getMimeData()); + assertEquals(List.of("SEND", "REJECT"), insightsEventTypes(messageId)); + verify(smtpRelay, never()).relay(any()); + } + + @Test + void sendEmail_withoutAttachments_storesNoAttachmentFields() { + String messageId = service.sendEmail(SendEmailRequest.builder() + .source("from@example.com") + .toAddresses(List.of("to@example.com")) + .region("us-east-1") + .content(new EmailContent.Simple("Subject", "text", null, List.of())) + .build()); + + SentEmail stored = storedEmail(messageId); + assertNull(stored.getAttachments()); + assertNull(stored.getMimeData()); + assertNull(capturedRelay().attachments()); + } + @Test void sendEmail_storesAndRelays() { String messageId = service.sendEmail(SendEmailRequest.builder() diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesSimpleAttachmentsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesSimpleAttachmentsV2IntegrationTest.java new file mode 100644 index 0000000000..14bb9015dd --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesSimpleAttachmentsV2IntegrationTest.java @@ -0,0 +1,354 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.json.JsonPath; +import io.restassured.response.Response; +import org.apache.james.mime4j.dom.Body; +import org.apache.james.mime4j.dom.Entity; +import org.apache.james.mime4j.dom.Message; +import org.apache.james.mime4j.dom.Multipart; +import org.apache.james.mime4j.dom.SingleBody; +import org.apache.james.mime4j.stream.Field; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * SES V2 SendEmail with {@code Content.Simple.Attachments}: the message is assembled as one MIME + * message, scanned, and captured by the inspection endpoint with its attachments. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class SesSimpleAttachmentsV2IntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request"; + private static final String SENDER = "attach-sender@example.com"; + private static final byte[] PDF_BYTES = "%PDF-1.4 fake report \u0000\u0001ΓΏ".getBytes(StandardCharsets.ISO_8859_1); + private static final byte[] PNG_BYTES = {(byte) 0x89, 'P', 'N', 'G', 0, 1, 2, 3, (byte) 0xfe}; + + @Test + @Order(1) + void createSender() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailIdentity\": \"" + SENDER + "\"}") + .when().post("/v2/email/identities").then().statusCode(200); + } + + @Test + @Order(2) + void simpleSend_withAttachmentAndInlinePart_isCapturedWithBoth() throws IOException { + String messageId = send(""" + {"FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"], "BccAddresses": ["hidden@example.com"]}, + "Content": {"Simple": { + "Subject": {"Data": "With attachments"}, + "Body": {"Text": {"Data": "plain body"}, + "Html": {"Data": "

html body

"}}, + "Headers": [{"Name": "X-Custom", "Value": "custom-value"}], + "Attachments": [ + {"FileName": "report.pdf", "RawContent": "%s", "ContentType": "application/pdf", + "ContentDisposition": "ATTACHMENT", "ContentDescription": "Monthly report"}, + {"FileName": "logo.png", "RawContent": "%s", "ContentType": "image/png", + "ContentDisposition": "INLINE", "ContentId": "logo"} + ]}}} + """.formatted(SENDER, b64(PDF_BYTES), b64(PNG_BYTES))) + .then().statusCode(200).extract().path("MessageId"); + + JsonPath captured = given().when().get("/_aws/ses?id=" + messageId) + .then().statusCode(200) + .body("messages", hasSize(1)) + .body("messages[0].Subject", equalTo("With attachments")) + .body("messages[0].Body.text_part", equalTo("plain body")) + .body("messages[0].Body.html_part", equalTo("

html body

")) + .body("messages[0].Destination.ToAddresses", contains("to@example.com")) + .body("messages[0].Headers.find { it.Name == 'X-Custom' }.Value", equalTo("custom-value")) + .body("messages[0].Attachments", hasSize(2)) + .body("messages[0].Attachments[0].FileName", equalTo("report.pdf")) + .body("messages[0].Attachments[0].ContentType", equalTo("application/pdf")) + .body("messages[0].Attachments[0].ContentDisposition", equalTo("ATTACHMENT")) + .body("messages[0].Attachments[0].ContentDescription", equalTo("Monthly report")) + .body("messages[0].Attachments[0]", not(hasKey("ContentId"))) + .body("messages[0].Attachments[0].Size", equalTo(PDF_BYTES.length)) + .body("messages[0].Attachments[1].FileName", equalTo("logo.png")) + .body("messages[0].Attachments[1].ContentType", equalTo("image/png")) + .body("messages[0].Attachments[1].ContentDisposition", equalTo("INLINE")) + .body("messages[0].Attachments[1].ContentId", equalTo("")) + .body("messages[0].Attachments[1].Size", equalTo(PNG_BYTES.length)) + .body("messages[0].RawData", notNullValue()) + .extract().jsonPath(); + + byte[] mime = Base64.getDecoder().decode(captured.getString("messages[0].RawData")); + String wire = new String(mime, StandardCharsets.UTF_8); + assertTrue(wire.contains("Message-ID: <" + messageId + "@email.amazonses.com>"), wire); + assertTrue(wire.contains("X-Custom: custom-value"), wire); + assertFalse(wire.contains("hidden@example.com"), "Bcc must not appear in the message"); + + Message message = SmtpRelay.parseMime(mime); + assertNotNull(message); + assertEquals("With attachments", message.getSubject()); + List leaves = new ArrayList<>(); + collectLeaves(message, leaves); + + Entity pdf = leafNamed(leaves, "report.pdf"); + assertEquals("application/pdf", pdf.getMimeType()); + assertEquals("attachment", pdf.getDispositionType()); + assertArrayEquals(PDF_BYTES, bytes(pdf)); + assertEquals("Monthly report", headerValue(pdf, "Content-Description")); + + Entity png = leafNamed(leaves, "logo.png"); + assertEquals("image/png", png.getMimeType()); + assertEquals("inline", png.getDispositionType()); + assertEquals("", headerValue(png, "Content-ID")); + assertArrayEquals(PNG_BYTES, bytes(png)); + + assertTrue(leaves.stream().anyMatch(e -> "text/plain".equals(e.getMimeType()) + && new String(bytesUnchecked(e), StandardCharsets.UTF_8).contains("plain body"))); + assertTrue(leaves.stream().anyMatch(e -> "text/html".equals(e.getMimeType()) + && new String(bytesUnchecked(e), StandardCharsets.UTF_8).contains("cid:logo"))); + } + + @Test + @Order(3) + void simpleSend_withoutAttachments_isCapturedAsBefore() { + String messageId = send(""" + {"FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": { + "Subject": {"Data": "No attachments"}, + "Body": {"Text": {"Data": "just text"}}}}} + """.formatted(SENDER)) + .then().statusCode(200).extract().path("MessageId"); + + given().when().get("/_aws/ses?id=" + messageId) + .then().statusCode(200) + .body("messages[0].Subject", equalTo("No attachments")) + .body("messages[0].Body.text_part", equalTo("just text")) + .body("messages[0].Body.html_part", nullValue()) + .body("messages[0]", not(hasKey("Attachments"))) + .body("messages[0]", not(hasKey("RawData"))); + } + + @Test + @Order(4) + void simpleSend_withVirusInAnAttachment_isRejectedLikeARawSend() { + String virus = b64(SesContentScan.signature().getBytes(StandardCharsets.US_ASCII)); + String messageId = send(""" + {"FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": { + "Subject": {"Data": "Infected"}, + "Body": {"Text": {"Data": "clean body"}}, + "Attachments": [{"FileName": "test.txt", "RawContent": "%s"}]}}} + """.formatted(SENDER, virus)) + .then().statusCode(200).extract().path("MessageId"); + + given().when().get("/_aws/ses?id=" + messageId) + .then().statusCode(200) + .body("messages[0].RejectReason", equalTo("Bad content")) + .body("messages[0].Destination.ToAddresses", contains("to@example.com")) + .body("messages[0]", not(hasKey("Attachments"))) + .body("messages[0]", not(hasKey("RawData"))) + .body("messages[0]", not(hasKey("Subject"))) + .body("messages[0]", not(hasKey("Body"))); + } + + @Test + @Order(5) + void simpleSend_attachmentWithoutFileName_isRejected() { + sendWithAttachments("[{\"RawContent\": \"" + b64(PDF_BYTES) + "\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("1 validation error detected: Value at " + + "'content.simple.attachments.1.member.fileName' failed to satisfy constraint: " + + "Member must not be null")); + } + + @Test + @Order(6) + void simpleSend_attachmentWithoutRawContent_isRejected() { + sendWithAttachments("[{\"FileName\": \"a.pdf\", \"RawContent\": \"" + b64(PDF_BYTES) + "\"}," + + "{\"FileName\": \"b.pdf\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("1 validation error detected: Value at " + + "'content.simple.attachments.2.member.rawContent' failed to satisfy constraint: " + + "Member must not be null")); + } + + @Test + @Order(7) + void simpleSend_attachmentWithUnknownDisposition_isRejected() { + sendWithAttachments("[{\"FileName\": \"a.pdf\", \"RawContent\": \"" + b64(PDF_BYTES) + + "\", \"ContentDisposition\": \"EMBEDDED\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("1 validation error detected: Value 'EMBEDDED' at " + + "'content.simple.attachments.1.member.contentDisposition' failed to satisfy " + + "constraint: Member must satisfy enum value set: [ATTACHMENT, INLINE]")); + } + + @Test + @Order(8) + void simpleSend_attachmentWithUnknownTransferEncoding_isRejected() { + sendWithAttachments("[{\"FileName\": \"a.pdf\", \"RawContent\": \"" + b64(PDF_BYTES) + + "\", \"ContentTransferEncoding\": \"UUENCODE\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", containsString("Member must satisfy enum value set: " + + "[BASE64, QUOTED_PRINTABLE, SEVEN_BIT]")); + } + + @Test + @Order(9) + void simpleSend_attachmentLengthLimits_areEnforced() { + String longName = "a".repeat(256); + sendWithAttachments("[{\"FileName\": \"" + longName + "\", \"RawContent\": \"" + b64(PDF_BYTES) + "\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", containsString("'content.simple.attachments.1.member.fileName' failed to " + + "satisfy constraint: Member must have length less than or equal to 255")); + + sendWithAttachments("[{\"FileName\": \"a.png\", \"RawContent\": \"" + b64(PNG_BYTES) + + "\", \"ContentId\": \"\"}]") + .then().statusCode(400) + .body("message", containsString("'content.simple.attachments.1.member.contentId' failed to " + + "satisfy constraint: Member must have length greater than or equal to 1")); + + sendWithAttachments("[{\"FileName\": \"a.png\", \"RawContent\": \"" + b64(PNG_BYTES) + + "\", \"ContentType\": \"" + "x".repeat(79) + "\"}]") + .then().statusCode(400) + .body("message", containsString("'content.simple.attachments.1.member.contentType' failed to " + + "satisfy constraint: Member must have length less than or equal to 78")); + + sendWithAttachments("[{\"FileName\": \"a.png\", \"RawContent\": \"" + b64(PNG_BYTES) + + "\", \"ContentDescription\": \"" + "x".repeat(1001) + "\"}]") + .then().statusCode(400) + .body("message", containsString("'content.simple.attachments.1.member.contentDescription' " + + "failed to satisfy constraint: Member must have length less than or equal to 1000")); + } + + @Test + @Order(10) + void simpleSend_attachmentWithInvalidBase64_isRejected() { + sendWithAttachments("[{\"FileName\": \"a.pdf\", \"RawContent\": \"not base64!!\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")); + } + + @Test + @Order(11) + void simpleSend_attachmentHeaderMembersWithLineBreaks_areRejected() { + for (String member : List.of("ContentType", "ContentDescription", "ContentId")) { + sendWithAttachments("[{\"FileName\": \"a.png\", \"RawContent\": \"" + b64(PNG_BYTES) + + "\", \"" + member + "\": \"x\\r\\nX-Injected: yes\"}]") + .then().statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo(member + " must not contain line breaks.")); + } + } + + @Test + @Order(12) + void simpleSend_attachmentWithUnsupportedExtension_isRefused() { + sendWithAttachments("[{\"FileName\": \"report.pdf\", \"RawContent\": \"" + b64(PDF_BYTES) + "\"}," + + "{\"FileName\": \"Setup.EXE\", \"RawContent\": \"" + b64(PDF_BYTES) + "\"}]") + .then().statusCode(400) + .body("__type", equalTo("MessageRejected")) + .body("message", equalTo("Message contains invalid content.")); + } + + @Test + @Order(13) + void rawSend_attachmentWithUnsupportedExtension_isRefused() { + String mime = "From: " + SENDER + "\r\nTo: to@example.com\r\nSubject: raw\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\nbody\r\n" + + "--b\r\nContent-Type: application/octet-stream\r\n" + + "Content-Disposition: attachment; filename=\"macro.vbs\"\r\n" + + "Content-Transfer-Encoding: base64\r\n\r\nAAAA\r\n--b--\r\n"; + send(""" + {"FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Raw": {"Data": "%s"}}} + """.formatted(SENDER, b64(mime.getBytes(StandardCharsets.US_ASCII)))) + .then().statusCode(400) + .body("__type", equalTo("MessageRejected")) + .body("message", equalTo("Message contains invalid content.")); + } + + @Test + @Order(14) + void cleanUp() { + given().header("Authorization", AUTH) + .when().delete("/v2/email/identities/" + SENDER).then().statusCode(200); + } + + private static Response send(String body) { + return given().contentType("application/json").header("Authorization", AUTH) + .body(body) + .when().post("/v2/email/outbound-emails"); + } + + private static Response sendWithAttachments(String attachmentsJson) { + return send(""" + {"FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": { + "Subject": {"Data": "Invalid"}, + "Body": {"Text": {"Data": "body"}}, + "Attachments": %s}}} + """.formatted(SENDER, attachmentsJson)); + } + + private static String b64(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + + private static void collectLeaves(Entity entity, List leaves) { + Body body = entity.getBody(); + if (body instanceof Multipart multipart) { + for (Entity part : multipart.getBodyParts()) { + collectLeaves(part, leaves); + } + } else { + leaves.add(entity); + } + } + + private static Entity leafNamed(List leaves, String fileName) { + return leaves.stream().filter(e -> fileName.equals(e.getFilename())).findFirst() + .orElseThrow(() -> new AssertionError("no MIME part named " + fileName)); + } + + private static byte[] bytes(Entity entity) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ((SingleBody) entity.getBody()).writeTo(out); + return out.toByteArray(); + } + + private static byte[] bytesUnchecked(Entity entity) { + try { + return bytes(entity); + } catch (IOException e) { + throw new AssertionError(e); + } + } + + private static String headerValue(Entity entity, String name) { + Field field = entity.getHeader().getField(name); + return field == null ? null : field.getBody().trim(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayServerTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayServerTest.java index b64335ccd2..635388ca70 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayServerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SmtpRelayServerTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ses; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.vertx.core.Vertx; import io.vertx.ext.mail.MailClient; @@ -156,6 +157,34 @@ class SmtpRelayServerTest { assertTrue(data.contains("plain body"), data); } + @Test + void structuredRelay_deliversSimpleAttachmentsOverSmtp() throws Exception { + server.reset(); + byte[] pdf = "PDF-PAYLOAD".getBytes(StandardCharsets.UTF_8); + byte[] png = "PNG-PAYLOAD".getBytes(StandardCharsets.UTF_8); + + relayAgainstServer().relay(SmtpRelay.RelayMessage.builder("sender@example.com") + .to(List.of("to@example.com")) + .subject("With attachments") + .bodyText("plain body") + .bodyHtml("") + .messageId("msg-9") + .attachments(List.of( + new MessageAttachment("report.pdf", pdf, "application/pdf", + MessageAttachment.Disposition.ATTACHMENT, null, null, null), + new MessageAttachment("logo.png", png, "image/png", + MessageAttachment.Disposition.INLINE, null, "logo", null))) + .build()); + + assertTrue(server.awaitDelivery(), "the stub server should have accepted a message"); + String data = server.data(); + assertTrue(data.contains("report.pdf"), data); + assertTrue(data.contains(Base64.getEncoder().encodeToString(pdf)), data); + assertTrue(data.contains("Content-ID: "), data); + assertTrue(data.contains(Base64.getEncoder().encodeToString(png)), data); + assertTrue(data.contains("multipart/related"), data); + } + @Test void relay_authenticatesWithTheConfiguredCredentials() throws Exception { server.reset(); 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 470e9004e9..463311ce2d 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 @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.ses; +import io.github.hectorvent.floci.services.ses.model.MessageAttachment; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.vertx.core.Future; @@ -13,6 +14,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.nio.charset.StandardCharsets; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -797,4 +799,77 @@ class SmtpRelayTest { verify(mailClient).sendMail(captor.capture()); return captor.getValue(); } + + private static MessageAttachment attachment(String name, String type, + MessageAttachment.Disposition disposition, String contentId) { + return new MessageAttachment(name, name.getBytes(StandardCharsets.UTF_8), type, disposition, + null, contentId, null); + } + + @Test + void relay_simpleAttachments_inlineWithContentIdIsRelatedAndTheRestAreAttachments() { + SmtpRelay relay = enabledRelay(); + + relay.relay(SmtpRelay.RelayMessage.builder("from@example.com") + .to(List.of("to@example.com")) + .subject("Subject") + .bodyHtml("") + .attachments(List.of( + attachment("report.pdf", "application/pdf", MessageAttachment.Disposition.ATTACHMENT, null), + attachment("logo.png", "image/png", MessageAttachment.Disposition.INLINE, "logo"), + attachment("notes.txt", null, null, null))) + .build()); + + MailMessage sent = captureSent(); + assertEquals(1, sent.getInlineAttachment().size()); + MailAttachment logo = sent.getInlineAttachment().get(0); + assertEquals("", logo.getContentId()); + assertEquals("inline", logo.getDisposition()); + assertEquals("image/png", logo.getContentType()); + assertEquals(2, sent.getAttachment().size()); + assertEquals("attachment", sent.getAttachment().get(0).getDisposition()); + assertEquals("report.pdf", sent.getAttachment().get(0).getName()); + // No ContentType and no ContentDisposition: guessed from the name, relayed as an attachment. + assertEquals("text/plain", sent.getAttachment().get(1).getContentType()); + assertEquals("attachment", sent.getAttachment().get(1).getDisposition()); + } + + @Test + void relay_inlineAttachmentWithoutHtmlBody_travelsAsAnInlineDispositionAttachment() { + SmtpRelay relay = enabledRelay(); + + relay.relay(SmtpRelay.RelayMessage.builder("from@example.com") + .to(List.of("to@example.com")) + .subject("Subject") + .bodyText("text only") + .attachments(List.of( + attachment("logo.png", "image/png", MessageAttachment.Disposition.INLINE, "logo"))) + .build()); + + MailMessage sent = captureSent(); + assertNull(sent.getInlineAttachment()); + assertEquals(1, sent.getAttachment().size()); + assertEquals("inline", sent.getAttachment().get(0).getDisposition()); + } + + @Test + void encodeMime_simpleWithAttachments_isOneParsableMultipartMessage() throws Exception { + byte[] mime = SmtpRelay.encodeMime(SmtpRelay.RelayMessage.builder("from@example.com") + .to(List.of("to@example.com")) + .bcc(List.of("hidden@example.com")) + .subject("Subject") + .bodyText("text") + .bodyHtml("") + .messageId("mid-1") + .attachments(List.of( + attachment("report.pdf", "application/pdf", MessageAttachment.Disposition.ATTACHMENT, null), + attachment("logo.png", "image/png", MessageAttachment.Disposition.INLINE, "logo"))) + .build()); + + String text = new String(mime, StandardCharsets.UTF_8); + assertTrue(text.contains("Message-ID: "), text); + assertEquals(text.indexOf("Message-ID:"), text.lastIndexOf("Message-ID:"), text); + assertFalse(text.contains("hidden@example.com"), text); + assertNotNull(SmtpRelay.parseMime(mime)); + } }