mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(ses): move the content scan into the mime package (#8986)
This commit is contained in:
1 parent
f094103b31
commit
57e4859f32
13 files changed
+133
-61
No files matched your search
@@ -10,6 +10,8 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.ses.address.SesAddressLength;
|
||||
import io.floci.aws.services.ses.address.SesAddressSyntax;
|
||||
import io.floci.aws.services.ses.mime.SesContentScan;
|
||||
import io.floci.aws.services.ses.mime.SesMimeParser;
|
||||
import io.floci.aws.services.ses.model.BulkEmailEntry;
|
||||
import io.floci.aws.services.ses.model.BulkEmailEntryResult;
|
||||
import io.floci.aws.services.ses.model.ConfigurationSet;
|
||||
@@ -450,7 +452,7 @@ public class SesService {
|
||||
if (content.hasAttachments()) {
|
||||
mime = SmtpRelay.encodeMime(simpleRelayMessage(request, content, effectiveReturnPath, messageId,
|
||||
request.toAddresses(), request.ccAddresses(), null));
|
||||
Message parsedMime = SmtpRelay.parseMime(mime);
|
||||
Message parsedMime = SesMimeParser.parse(mime);
|
||||
rejectBlockedAttachments(parsedMime);
|
||||
SesContentScan.Result scan = SesContentScan.scan(mime, parsedMime);
|
||||
if (scan == SesContentScan.Result.UNREADABLE) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.services.ses.mime.SesMimeParser;
|
||||
import io.floci.aws.services.ses.model.MessageAttachment;
|
||||
import io.floci.aws.services.ses.model.MessageHeader;
|
||||
import io.floci.aws.services.ses.model.MessageTag;
|
||||
@@ -729,22 +730,11 @@ public class SmtpRelay {
|
||||
return new ParsedRawMessage(new byte[0], null, RawMessageHeaders.empty());
|
||||
}
|
||||
byte[] mimeBytes = tryBase64Decode(rawMessage);
|
||||
Message message = parseMime(mimeBytes);
|
||||
Message message = SesMimeParser.parse(mimeBytes);
|
||||
return new ParsedRawMessage(mimeBytes, message,
|
||||
message == null ? RawMessageHeaders.empty() : headersOf(message));
|
||||
}
|
||||
|
||||
/** Parses MIME bytes, or returns {@code null} for anything the parser cannot read. */
|
||||
static Message parseMime(byte[] mimeBytes) {
|
||||
try {
|
||||
return new DefaultMessageBuilder().parseMessage(new ByteArrayInputStream(mimeBytes));
|
||||
} catch (Exception | StackOverflowError ignored) {
|
||||
// The caller treats an unparseable message as such; there is nothing to diagnose here,
|
||||
// and mime4j nests a reader per forwarded message, so a hostile depth overflows too.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static RawMessageHeaders headersOf(Message message) {
|
||||
try {
|
||||
String subject = message.getSubject() != null ? message.getSubject() : "";
|
||||
|
||||
+7
-15
@@ -1,4 +1,4 @@
|
||||
package io.floci.aws.services.ses;
|
||||
package io.floci.aws.services.ses.mime;
|
||||
|
||||
import org.apache.james.mime4j.codec.DecodeMonitor;
|
||||
import org.apache.james.mime4j.codec.DecoderUtil;
|
||||
@@ -32,7 +32,7 @@ import java.util.Set;
|
||||
* logged, and is exposed to tests only through {@link #signature()} so no test source carries it
|
||||
* either. A message that trips the scan is likewise never persisted with its body.
|
||||
*/
|
||||
final class SesContentScan {
|
||||
public final class SesContentScan {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SesContentScan.class);
|
||||
|
||||
@@ -68,12 +68,12 @@ final class SesContentScan {
|
||||
}
|
||||
|
||||
/** True when any of the given texts carries the test signature. Nulls are skipped. */
|
||||
static boolean containsTestVirus(String... texts) {
|
||||
public static boolean containsTestVirus(String... texts) {
|
||||
return containsTestVirus(Arrays.asList(texts));
|
||||
}
|
||||
|
||||
/** True when any of the given texts carries the test signature. Nulls are skipped. */
|
||||
static boolean containsTestVirus(Iterable<String> texts) {
|
||||
public static boolean containsTestVirus(Iterable<String> texts) {
|
||||
for (String text : texts) {
|
||||
if (text != null && text.contains(SIGNATURE_TEXT)) {
|
||||
return true;
|
||||
@@ -83,7 +83,7 @@ final class SesContentScan {
|
||||
}
|
||||
|
||||
/** The outcome of scanning a raw message. */
|
||||
enum Result {
|
||||
public enum Result {
|
||||
CLEAN,
|
||||
REJECTED,
|
||||
/** The parser could not read the message, so only the wire bytes were checked. */
|
||||
@@ -91,7 +91,7 @@ final class SesContentScan {
|
||||
}
|
||||
|
||||
/** Scans a raw message from its wire bytes and the parse of them, {@code null} when it failed. */
|
||||
static Result scan(byte[] mime, Message parsed) {
|
||||
public static Result scan(byte[] mime, Message parsed) {
|
||||
if (mime == null || mime.length == 0) {
|
||||
return Result.CLEAN;
|
||||
}
|
||||
@@ -113,20 +113,12 @@ final class SesContentScan {
|
||||
}
|
||||
}
|
||||
|
||||
/** Scans a raw message, parsing it first; the parse is shared with the send path elsewhere. */
|
||||
static Result scan(byte[] mime) {
|
||||
if (mime == null || mime.length == 0) {
|
||||
return Result.CLEAN;
|
||||
}
|
||||
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) {
|
||||
public static boolean hasBlockedAttachment(Message parsed) {
|
||||
if (parsed == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.floci.aws.services.ses.mime;
|
||||
|
||||
import org.apache.james.mime4j.dom.Message;
|
||||
import org.apache.james.mime4j.message.DefaultMessageBuilder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
/** The lenient MIME parse the send path runs once and shares between header extraction and the content scan. */
|
||||
public final class SesMimeParser {
|
||||
|
||||
private SesMimeParser() {
|
||||
}
|
||||
|
||||
/** Parses MIME bytes, or returns {@code null} for anything the parser cannot read. */
|
||||
public static Message parse(byte[] mimeBytes) {
|
||||
try {
|
||||
return new DefaultMessageBuilder().parseMessage(new ByteArrayInputStream(mimeBytes));
|
||||
} catch (Exception | StackOverflowError ignored) {
|
||||
// The caller treats an unparseable message as such; there is nothing to diagnose here,
|
||||
// and mime4j nests a reader per forwarded message, so a hostile depth overflows too.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user