Email for professionals and teams
fix(security): bound text-line floods and preflight every parseRawMime caller (#214)
Hieu Nguyen (Jack)ac1deac173eb
diff --git a/src/lib/email/inbound-attachments.ts b/src/lib/email/inbound-attachments.tsindex 9995175..8160d11 100644--- a/src/lib/email/inbound-attachments.ts+++ b/src/lib/email/inbound-attachments.ts@@ -3,8 +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 { MimeLimitError, parseRawMime } from "@/lib/email/parse";import type { AttachmentContent } from "@/lib/email/attachment-types";export function inboundAttachmentLimitReason(attachments: AttachmentContent[]): string | null {@@ -26,12 +25,11 @@ export function inboundAttachmentLimitReason(attachments: AttachmentContent[]):}export async function inboundAttachmentLimitReasonFromRaw(raw: ArrayBuffer): Promise<string | null> {- const preflight = mimePreflightReason(raw);- if (preflight) return preflight;try {const parsed = await parseRawMime(raw);return inboundAttachmentLimitReason(parsed.attachments);} catch (error) {+ if (error instanceof MimeLimitError) return error.message;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 6bd2504..2e68da8 100644--- a/src/lib/email/inbound.ts+++ b/src/lib/email/inbound.ts@@ -4,7 +4,6 @@ import { messages, outboundJobs, users } from "@/db/schema";import { newId } from "@/lib/ids";import { buildSnippet, parseRawMime } from "@/lib/email/parse";import { boundParsedEmail } from "@/lib/email/inbound-limits";-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";@@ -95,10 +94,8 @@ export async function processInboundMessage(}const buffer = await raw.arrayBuffer();- const preflightReason = mimePreflightReason(buffer);let parsed: Awaited<ReturnType<typeof parseRawMime>>;try {- if (preflightReason) throw new Error(preflightReason);parsed = boundParsedEmail(await parseRawMime(buffer));} catch (error) {// Deterministic: a retry would repeat the same expensive parse, so drop the message.diff --git a/src/lib/email/mime-preflight.ts b/src/lib/email/mime-preflight.tsindex 79c6b08..4877307 100644--- a/src/lib/email/mime-preflight.ts+++ b/src/lib/email/mime-preflight.ts@@ -1,13 +1,15 @@/*** 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).+ * memory (header floods, thousands of parts, endless nested message/rfc822, line floods in text parts).* 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,+ // Lines outside base64 parts: postal-mime spends memory per line there, so 262k lines of+ // any length exhaust a 128 MB heap. Base64 attachment lines are chunked and stay cheap.+ maxTextLines: 40_000,maxNestingDepth: 20,} as const;@@ -43,8 +45,11 @@ export function mimePreflightReason(raw: ArrayBuffer | Uint8Array): string | nulconst bytes = raw instanceof Uint8Array ? raw : new Uint8Array(raw);let boundaryLines = 0;let nestedMessages = 0;- let blankLines = 0;+ let textLines = 0;let headerEnded = false;+ // Tracks the current part's header block and transfer encoding to tell base64 from text lines.+ let inPartHeaders = true;+ let partIsBase64 = false;let lineStart = 0;while (lineStart < bytes.length) {@@ -53,25 +58,34 @@ export function mimePreflightReason(raw: ArrayBuffer | Uint8Array): string | nullet contentEnd = lineEnd;if (contentEnd > lineStart && bytes[contentEnd - 1] === CARRIAGE_RETURN) contentEnd--;+ const isBoundary = contentEnd - lineStart > 2 && bytes[lineStart] === DASH && bytes[lineStart + 1] === DASH;if (contentEnd === lineStart) {headerEnded = true;- if (++blankLines > MIME_LIMITS.maxBlankLines) {- return "Message rejected: malformed MIME structure (too many empty lines).";- }+ inPartHeaders = false;} 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) {+ } else if (isBoundary) {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.";+ inPartHeaders = true;+ partIsBase64 = false;+ lineStart = lineEnd + 1;+ continue;+ } else {+ // Not limited to part headers: a nested message restarts its own header block.+ 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.";+ }+ }+ if (inPartHeaders && startsWithAscii(bytes, lineStart, contentEnd, "content-transfer-encoding:") && lineContainsAscii(bytes, lineStart, contentEnd, "base64")) {+ partIsBase64 = true;}}+ if (!inPartHeaders && !partIsBase64 && ++textLines > MIME_LIMITS.maxTextLines) {+ return "Message rejected: malformed MIME structure (too many lines).";+ }lineStart = lineEnd + 1;}return null;diff --git a/src/lib/email/parse.ts b/src/lib/email/parse.tsindex 19bbbbb..53639f9 100644--- a/src/lib/email/parse.ts+++ b/src/lib/email/parse.ts@@ -1,7 +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 { MIME_LIMITS, mimePreflightReason } 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 +26,13 @@ export type ParsedEmail = {attachments: AttachmentContent[];};+/** Mail that can never parse within bounds; retrying it only repeats the work. */+export class MimeLimitError extends Error {}+export async function parseRawMime(raw: ArrayBuffer): Promise<ParsedEmail> {+ // Every caller (inbound, import, JMAP) gets the cheap structural check before any decode.+ const limitReason = mimePreflightReason(raw);+ if (limitReason) throw new MimeLimitError(limitReason);// 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, {diff --git a/tests/mime-bounds.test.mjs b/tests/mime-bounds.test.mjsindex 99f9894..a0daf80 100644--- a/tests/mime-bounds.test.mjs+++ b/tests/mime-bounds.test.mjs@@ -1,44 +1,59 @@import assert from "node:assert/strict";-import { test } from "node:test";+import { after, test } from "node:test";import { execFileSync } from "node:child_process";import { build } from "esbuild";-import { mkdtempSync, writeFileSync } from "node:fs";+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";const dir = mkdtempSync(join(tmpdir(), "mime-bounds-"));+after(() => rmSync(dir, { recursive: true, force: true }));+const root = process.cwd();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";+import { MimeLimitError, parseRawMime } from "${root}/src/lib/email/parse";+import { htmlToReadableText } from "${root}/src/lib/email/reply-content-utils";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 b64Part = 'Content-Type: multipart/mixed; boundary="b"\\r\\n\\r\\n--b\\r\\nContent-Type: application/pdf\\r\\nContent-Transfer-Encoding: base64\\r\\nContent-Disposition: attachment; filename=a.pdf\\r\\n\\r\\n';+const hostile = {+ blank: hdr + "\\r\\n" + "\\r\\n".repeat(512 * 1024),+ shortLines: hdr + "\\r\\n" + "a\\r\\n".repeat(262144),+ spaceLines: hdr + "\\r\\n" + " \\r\\n".repeat(262144),+ textLines: hdr + "\\r\\n" + ("a".repeat(78) + "\\r\\n").repeat(262144),+ lfLines: hdr + "\\n" + "a\\n".repeat(262144),+ nested: hdr + "Content-Type: message/rfc822\\r\\n\\r\\n".repeat(5000),+ parts: 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: "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;+const out = { rejected: {}, ok: {} };+for (const [name, text] of Object.entries(hostile)) {+ try {+ await parseRawMime(enc.encode(text).buffer);+ out.rejected[name] = null;+ } catch (error) {+ out.rejected[name] = error instanceof MimeLimitError ? error.message : "other: " + error;+ }+}+// A 25 MB-class base64 attachment is ~340k short lines and must still parse.+const legit = hdr + b64Part + ("QUFB".repeat(19) + "\\r\\n").repeat(340000) + "--b--\\r\\n";+const parsed = await parseRawMime(enc.encode(legit).buffer);+out.ok.attachments = parsed.attachments.length;+out.ok.plain = (await parseRawMime(enc.encode(hdr + "\\r\\nhello\\r\\n").buffer)).text?.trim();out.htmlText = htmlToReadableText("<style>a{}</style><p>Hi & bye</p>").trim();+out.htmlBomb = htmlToReadableText("<style>".repeat(20000) + "<p>hi</p>").includes("hi");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" });+await build({ entryPoints: [entry], bundle: true, outfile: bundle, platform: "node", format: "esm", alias: { "@": join(root, "src") }, logLevel: "silent", banner: { js: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);" } });-test("hostile MIME is rejected cheaply inside a 128 MB heap", () => {+test("hostile MIME is rejected, legit mail parses, all 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`);+ for (const [name, reason] of Object.entries(out.rejected)) {+ assert.match(String(reason), /^Message rejected:/, `${name} was not rejected: ${reason}`);+ }+ assert.equal(out.ok.attachments, 1);+ assert.equal(out.ok.plain, "hello");assert.equal(out.htmlText, "Hi & bye");+ assert.equal(out.htmlBomb, true);});