Skip to content

Email for professionals and teams

fix(security): cap stored inbound fields so mail is not lost at DB limits (#217)

Hieu Nguyen (Jack)a0a6f3d7c90b
diff --git a/src/lib/email/inbound-limits.ts b/src/lib/email/inbound-limits.ts
new file mode 100644
index 0000000..0d854eb
--- /dev/null
+++ b/src/lib/email/inbound-limits.ts
@@ -0,0 +1,54 @@
+import type { ParsedEmail } from "@/lib/email/parse";
+
+/** Column budgets that hold on D1 (2 MB rows), MySQL varchar keys and PostgreSQL btree entries. */
+export const INBOUND_LIMITS = {
+ textBodyBytes: 450_000,
+ htmlBodyBytes: 900_000,
+ messageIdChars: 512,
+ subjectChars: 998,
+ addressChars: 998,
+ addressListChars: 8_000,
+ referencesCount: 20,
+} as const;
+
+const encoder = new TextEncoder();
+const decoder = new TextDecoder("utf-8", { fatal: false });
+
+export function truncateUtf8(value: string, maxBytes: number): string {
+ // UTF-8 needs at most 4 bytes a character, so anything shorter than a quarter always fits.
+ if (value.length * 4 <= maxBytes) return value;
+ const bytes = encoder.encode(value);
+ if (bytes.byteLength <= maxBytes) return value;
+ // A cut inside a multi-byte sequence decodes to a replacement character; drop it.
+ return decoder.decode(bytes.subarray(0, maxBytes)).replace(/�+$/u, "");
+}
+
+function capChars(value: string | null, max: number): string | null {
+ return value && value.length > max ? value.slice(0, max) : value;
+}
+
+function boundedId(value: string | null): string | null {
+ return value && value.length <= INBOUND_LIMITS.messageIdChars ? value : null;
+}
+
+/**
+ * Keep every stored field inside what all three databases accept, so a hostile or merely odd
+ * message is stored (the full original stays in raw MIME) instead of failing the insert and
+ * being retried until it is dropped.
+ */
+export function boundParsedEmail(parsed: ParsedEmail): ParsedEmail {
+ return {
+ ...parsed,
+ subject: capChars(parsed.subject, INBOUND_LIMITS.subjectChars),
+ text: parsed.text === null ? null : truncateUtf8(parsed.text, INBOUND_LIMITS.textBodyBytes),
+ html: parsed.html === null ? null : truncateUtf8(parsed.html, INBOUND_LIMITS.htmlBodyBytes),
+ messageId: boundedId(parsed.messageId),
+ fromAddr: capChars(parsed.fromAddr, INBOUND_LIMITS.addressChars),
+ toAddr: capChars(parsed.toAddr, INBOUND_LIMITS.addressListChars),
+ ccAddr: capChars(parsed.ccAddr, INBOUND_LIMITS.addressListChars),
+ bccAddr: capChars(parsed.bccAddr, INBOUND_LIMITS.addressListChars),
+ replyTo: capChars(parsed.replyTo, INBOUND_LIMITS.addressListChars),
+ inReplyTo: boundedId(parsed.inReplyTo),
+ references: parsed.references.filter((id) => id.length <= INBOUND_LIMITS.messageIdChars).slice(-INBOUND_LIMITS.referencesCount),
+ };
+}
diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.ts
index 76d7b5f..708e1c6 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 { 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";
@@ -107,7 +108,7 @@ export async function processInboundMessage(
let parsed: Awaited<ReturnType<typeof parseRawMime>>;
try {
if (preflightReason) throw new Error(preflightReason);
- parsed = await parseRawMime(buffer);
+ parsed = boundParsedEmail(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);
@@ -168,10 +169,14 @@ export async function processInboundMessage(
? "spam"
: destination.status;
const folderId = status === "spam" ? null : destination.folderId;
+ // A contact the database cannot hold (e.g. an over-long address) must not lose the mail.
const contact = await upsertContactFromAddress(env, {
userId: decision.mailbox.userId,
address: fromAddr,
source: "inbound",
+ }).catch((error) => {
+ console.error(`Contact upsert failed for ${messageId}`, error);
+ return null;
});
const sentByOwner = await isOwnAddress(db, decision.mailbox.userId, getEmailAddress(fromAddr) || fromAddr).catch(() => false);
let threadId: string;