Email for professionals and teams
fix #214
Hieu Nguyen (Jack)8377967ee8da
diff --git a/src/lib/email/inbound-attachments.ts b/src/lib/email/inbound-attachments.tsindex 1acc086..9995175 100644--- a/src/lib/email/inbound-attachments.ts+++ b/src/lib/email/inbound-attachments.ts@@ -3,6 +3,7 @@ import {MAX_ATTACHMENT_SIZE,MAX_TOTAL_ATTACHMENT_SIZE,} from "@/lib/email/attachments";+import { mimePreflightReason } from "@/lib/email/mime-preflight";import { parseRawMime } from "@/lib/email/parse";import type { AttachmentContent } from "@/lib/email/attachment-types";@@ -25,6 +26,13 @@ export function inboundAttachmentLimitReason(attachments: AttachmentContent[]):}export async function inboundAttachmentLimitReasonFromRaw(raw: ArrayBuffer): Promise<string | null> {- const parsed = await parseRawMime(raw);- return inboundAttachmentLimitReason(parsed.attachments);+ const preflight = mimePreflightReason(raw);+ if (preflight) return preflight;+ try {+ const parsed = await parseRawMime(raw);+ return inboundAttachmentLimitReason(parsed.attachments);+ } catch (error) {+ console.warn("Inbound MIME parse failed", error);+ return "Message rejected: the message could not be parsed.";+ }}diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.tsindex 7ebef78..76d7b5f 100644--- a/src/lib/email/inbound.ts+++ b/src/lib/email/inbound.ts@@ -3,6 +3,7 @@ import { getDb } from "@/db";import { domains, mailboxAliases, mailboxes, messages, outboundJobs, users } from "@/db/schema";import { newId } from "@/lib/ids";import { buildSnippet, parseRawMime } from "@/lib/email/parse";+import { mimePreflightReason } from "@/lib/email/mime-preflight";import { resolveInboundAddress, resolveInboxRuleDestination } from "@/lib/email/routing";import { dispatchWebhooks } from "@/lib/email/webhooks";import { getMessageContactNames, upsertContactFromAddress } from "@/lib/contacts/service";@@ -102,7 +103,17 @@ export async function processInboundMessage(}const buffer = await raw.arrayBuffer();- const parsed = await parseRawMime(buffer);+ const preflightReason = mimePreflightReason(buffer);+ let parsed: Awaited<ReturnType<typeof parseRawMime>>;+ try {+ if (preflightReason) throw new Error(preflightReason);+ parsed = await parseRawMime(buffer);+ } catch (error) {+ // Deterministic: a retry would repeat the same expensive parse, so drop the message.+ console.warn(`Inbound MIME rejected for ${payload.to}`, error);+ await env.BUCKET.delete(payload.rawR2Key);+ return;+ }const attachmentLimitReason = inboundAttachmentLimitReason(parsed.attachments);if (attachmentLimitReason) {console.warn(`Inbound attachment limit reached for ${payload.to}: ${attachmentLimitReason}`);diff --git a/src/lib/email/mime-preflight.ts b/src/lib/email/mime-preflight.tsnew file mode 100644index 0000000..79c6b08--- /dev/null+++ b/src/lib/email/mime-preflight.ts@@ -0,0 +1,78 @@+/**+ * Cheap, linear raw-byte scan that rejects MIME shapes known to make the parser burn CPU or+ * memory (header floods, thousands of parts, endless nested message/rfc822, blank-line floods).+ * Runs before any decode, so a hostile message costs one pass over the bytes.+ */+export const MIME_LIMITS = {+ maxHeaderBytes: 64 * 1024,+ maxBoundaryLines: 1_000,+ maxNestedMessages: 20,+ maxBlankLines: 20_000,+ maxNestingDepth: 20,+} as const;++const NEWLINE = 10;+const CARRIAGE_RETURN = 13;+const DASH = 45;+const MESSAGE_RFC822 = "message/rfc822";++function startsWithAscii(bytes: Uint8Array, start: number, end: number, prefix: string): boolean {+ if (end - start < prefix.length) return false;+ for (let i = 0; i < prefix.length; i++) {+ if ((bytes[start + i] | 0x20) !== prefix.charCodeAt(i)) return false;+ }+ return true;+}++function lineContainsAscii(bytes: Uint8Array, start: number, end: number, needle: string): boolean {+ const limit = Math.min(end, start + 256) - needle.length;+ for (let i = start; i <= limit; i++) {+ let matched = true;+ for (let j = 0; j < needle.length; j++) {+ if ((bytes[i + j] | 0x20) !== needle.charCodeAt(j)) {+ matched = false;+ break;+ }+ }+ if (matched) return true;+ }+ return false;+}++export function mimePreflightReason(raw: ArrayBuffer | Uint8Array): string | null {+ const bytes = raw instanceof Uint8Array ? raw : new Uint8Array(raw);+ let boundaryLines = 0;+ let nestedMessages = 0;+ let blankLines = 0;+ let headerEnded = false;++ let lineStart = 0;+ while (lineStart < bytes.length) {+ let lineEnd = bytes.indexOf(NEWLINE, lineStart);+ if (lineEnd === -1) lineEnd = bytes.length;+ let contentEnd = lineEnd;+ if (contentEnd > lineStart && bytes[contentEnd - 1] === CARRIAGE_RETURN) contentEnd--;++ if (contentEnd === lineStart) {+ headerEnded = true;+ if (++blankLines > MIME_LIMITS.maxBlankLines) {+ return "Message rejected: malformed MIME structure (too many empty lines).";+ }+ } else if (!headerEnded && lineEnd > MIME_LIMITS.maxHeaderBytes) {+ return "Message rejected: header block exceeds 64 KiB.";+ } else if (contentEnd - lineStart > 2 && bytes[lineStart] === DASH && bytes[lineStart + 1] === DASH) {+ if (++boundaryLines > MIME_LIMITS.maxBoundaryLines) {+ return "Message rejected: too many MIME parts.";+ }+ } else if (+ startsWithAscii(bytes, lineStart, contentEnd, "content-type:") &&+ lineContainsAscii(bytes, lineStart, contentEnd, MESSAGE_RFC822)+ ) {+ if (++nestedMessages > MIME_LIMITS.maxNestedMessages) {+ return "Message rejected: too many nested messages.";+ }+ }+ lineStart = lineEnd + 1;+ }+ return null;+}diff --git a/src/lib/email/parse.ts b/src/lib/email/parse.tsindex ac7370f..19bbbbb 100644--- a/src/lib/email/parse.ts+++ b/src/lib/email/parse.ts@@ -1,6 +1,7 @@import PostalMime from "postal-mime";import { formatPostalAddress, formatPostalAddressList } from "@/lib/email/address";import { normalizeMessageId, parseMessageIdList } from "@/lib/email/threading";+import { MIME_LIMITS } from "@/lib/email/mime-preflight";import { normalizeAttachmentContent } from "@/lib/email/attachments";import { getLatestEmailContent, htmlToReadableText } from "@/lib/email/reply-content-utils";import type { AttachmentContent } from "@/lib/email/attachment-types";@@ -26,7 +27,13 @@ export type ParsedEmail = {};export async function parseRawMime(raw: ArrayBuffer): Promise<ParsedEmail> {- const email = await PostalMime.parse(raw);+ // forceRfc822Attachments keeps postal-mime from re-parsing inline message/rfc822 with fresh+ // options, which would reset the nesting cap at every level.+ const email = await PostalMime.parse(raw, {+ maxNestingDepth: MIME_LIMITS.maxNestingDepth,+ maxHeadersSize: MIME_LIMITS.maxHeaderBytes,+ forceRfc822Attachments: true,+ });const date = email.date ? new Date(email.date) : null;return {subject: email.subject ?? null,diff --git a/src/lib/email/reply-content-utils.ts b/src/lib/email/reply-content-utils.tsindex 4f88d48..141ddf2 100644--- a/src/lib/email/reply-content-utils.ts+++ b/src/lib/email/reply-content-utils.ts@@ -62,12 +62,37 @@ export function getLatestEmailContent(content: string | null | undefined): strinreturn splitRepliedEmailContent(content).latestContent;}+const MAX_READABLE_HTML = 250_000;++function stripBlocks(html: string, names: string[]): string {+ const lower = html.toLowerCase();+ const missingClose = new Set<string>();+ let out = "";+ let cursor = 0;+ const open = /<(style|script|head)\b[^>]*>/gi;+ let match: RegExpExecArray | null;+ while ((match = open.exec(html))) {+ const name = match[1].toLowerCase();+ if (match.index < cursor || !names.includes(name)) continue;+ // An unclosed block would otherwise rescan to the end of the input for every opener.+ const close = missingClose.has(name) ? -1 : lower.indexOf(`</${name}`, match.index + match[0].length);+ if (close === -1) {+ missingClose.add(name);+ continue;+ }+ const closeEnd = html.indexOf(">", close);+ out += `${html.slice(cursor, match.index)} `;+ cursor = closeEnd === -1 ? html.length : closeEnd + 1;+ open.lastIndex = cursor;+ }+ return out + html.slice(cursor);+}+export function htmlToReadableText(html: string | null | undefined): string {- return (html ?? "")- .replace(/<(style|script|head)\b[^>]*>[\s\S]*?<\/\1>/gi, " ")+ return stripBlocks((html ?? "").slice(0, MAX_READABLE_HTML), ["style", "script", "head"]).replace(/<br\s*\/?>/gi, "\n").replace(/<\/(p|div|li|tr|blockquote|h[1-6])>/gi, "\n")- .replace(/<[^>]+>/g, " ")+ .replace(/<[^<>]*>/g, " ").replace(/ /g, " ").replace(/&/g, "&").replace(/</g, "<")diff --git a/tests/mime-bounds.test.mjs b/tests/mime-bounds.test.mjsnew file mode 100644index 0000000..99f9894--- /dev/null+++ b/tests/mime-bounds.test.mjs@@ -0,0 +1,44 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import { execFileSync } from "node:child_process";+import { build } from "esbuild";+import { mkdtempSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++const dir = mkdtempSync(join(tmpdir(), "mime-bounds-"));+const entry = join(dir, "entry.ts");+writeFileSync(entry, `+import { mimePreflightReason } from "${process.cwd()}/src/lib/email/mime-preflight";+import { htmlToReadableText } from "${process.cwd()}/src/lib/email/reply-content-utils";+import PostalMime from "${process.cwd()}/node_modules/postal-mime/src/postal-mime.js";+const enc = new TextEncoder();+const hdr = "From: [email protected]\\r\\nTo: [email protected]\\r\\nSubject: x\\r\\n";+const cases = {+ blank: enc.encode(hdr + "\\r\\n" + "\\r\\n".repeat(512 * 1024)),+ nested: enc.encode(hdr + "Content-Type: message/rfc822\\r\\n\\r\\n".repeat(5000)),+ parts: enc.encode(hdr + 'Content-Type: multipart/mixed; boundary="b"\\r\\n\\r\\n' + "--b\\r\\nContent-Type: text/plain\\r\\n\\r\\nx\\r\\n".repeat(20000)),+ headers: enc.encode("X-Pad: " + "a".repeat(200 * 1024) + "\\r\\n\\r\\nbody"),+};+const out = {};+for (const [name, raw] of Object.entries(cases)) out[name] = mimePreflightReason(raw);+out.ok = mimePreflightReason(enc.encode(hdr + "\\r\\nhello\\r\\n"));+const t = Date.now();+htmlToReadableText("<style>".repeat(40000) + "<p>hi</p>");+out.htmlMs = Date.now() - t;+out.htmlText = htmlToReadableText("<style>a{}</style><p>Hi & bye</p>").trim();+console.log(JSON.stringify(out));+`);+const bundle = join(dir, "entry.mjs");+await build({ entryPoints: [entry], bundle: true, outfile: bundle, platform: "node", format: "esm", alias: { "@": join(process.cwd(), "src") }, logLevel: "silent" });++test("hostile MIME is rejected cheaply inside a 128 MB heap", () => {+ const out = JSON.parse(execFileSync(process.execPath, ["--max-old-space-size=128", bundle], { encoding: "utf8" }));+ assert.match(out.blank, /empty lines/);+ assert.match(out.nested, /nested/);+ assert.match(out.parts, /too many MIME parts/);+ assert.match(out.headers, /header block/);+ assert.equal(out.ok, null);+ assert.ok(out.htmlMs < 1000, `html strip took ${out.htmlMs}ms`);+ assert.equal(out.htmlText, "Hi & bye");+});