add 25 MB per-file attachment size guard to MessageFormatter

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-14 13:44:09 +01:00
parent 0ba309c525
commit 961f87359a
2 changed files with 40 additions and 5 deletions
@@ -71,10 +71,21 @@ public enum MessageFormatter: Sendable {
}
/// Format a multipart/mixed message with attachments.
/// Throws `MessageFormatterError.attachmentTooLarge` if any file exceeds 25 MB.
public static func formatMultipart(
_ message: OutgoingMessage,
attachments: [(filename: String, mimeType: String, data: Data)]
) -> String {
) throws -> String {
let maxSize = 25 * 1024 * 1024 // 25 MB
for attachment in attachments {
if attachment.data.count > maxSize {
throw MessageFormatterError.attachmentTooLarge(
filename: attachment.filename,
size: attachment.data.count
)
}
}
let boundary = "=_MagnumOpus_\(UUID().uuidString)"
var headers: [(String, String)] = []
@@ -184,3 +195,7 @@ public enum MessageFormatter: Sendable {
return result
}
}
public enum MessageFormatterError: Error {
case attachmentTooLarge(filename: String, size: Int)
}