Email for professionals and teams
fix(security): harden attachment filenames and Content-Disposition (#222)
Hieu Nguyen (Jack)ea68b2c1c5ea
diff --git a/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts b/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.tsindex 51e073e..d373b02 100644--- a/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts+++ b/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts@@ -1,4 +1,10 @@+// eslint-disable-next-line no-control-regex+const CONTROL_AND_BIDI = /[\u0000-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/g;+export function getAttachmentContentDisposition(filename: string, inline: boolean): string {- const safeFilename = filename.replace(/["\\\r\n]/g, "_");- return `${inline ? "inline" : "attachment"}; filename="${safeFilename}"`;+ const cleaned = filename.replace(CONTROL_AND_BIDI, "").trim() || "attachment";+ // Header values must be ByteStrings: ASCII fallback for old clients, the real name as RFC 5987.+ const fallback = cleaned.replace(/[^\x20-\x7e]/g, "_").replace(/["\\%]/g, "_");+ const encoded = encodeURIComponent(cleaned).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);+ return `${inline ? "inline" : "attachment"}; filename="${fallback}"; filename*=UTF-8''${encoded}`;}diff --git a/src/lib/email/attachments.ts b/src/lib/email/attachments.tsindex 5e892b8..a647359 100644--- a/src/lib/email/attachments.ts+++ b/src/lib/email/attachments.ts@@ -2,6 +2,7 @@ import { and, eq, isNull } from "drizzle-orm";import { getDb } from "@/db";import { messageAttachments, messages } from "@/db/schema";import { newId } from "@/lib/ids";+import { truncateUtf8 } from "@/lib/email/inbound-limits";import { getMailboxAccessLevel } from "@/lib/mailboxes/access";import type { SessionUser } from "@/lib/auth/types";import type {@@ -35,9 +36,17 @@ export function normalizeAttachmentContent(return new TextEncoder().encode(content).buffer;}-function sanitizeFilename(filename: string): string {- const normalized = filename.trim().replace(/[/\\\0]/g, "_");- return normalized || "attachment";+const MAX_FILENAME_BYTES = 180;++export function sanitizeFilename(filename: string): string {+ // Control and bidi characters are dropped so a name cannot reorder or hide its extension.+ // eslint-disable-next-line no-control-regex+ const normalized = filename.replace(/[\u0000-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/g, "").trim().replace(/[/\\]/g, "_");+ if (!normalized || /^\.+$/.test(normalized)) return "attachment";+ if (new TextEncoder().encode(normalized).byteLength <= MAX_FILENAME_BYTES) return normalized;+ // Keep a short extension so the file type survives truncation.+ const extension = normalized.match(/\.[A-Za-z0-9]{1,10}$/)?.[0] ?? "";+ return `${truncateUtf8(normalized.slice(0, normalized.length - extension.length), MAX_FILENAME_BYTES - extension.length)}${extension}`;}export function validateAttachments(attachments: AttachmentContent[]): void {diff --git a/src/lib/jmap/handler.ts b/src/lib/jmap/handler.tsindex d2e8d37..6dd3157 100644--- a/src/lib/jmap/handler.ts+++ b/src/lib/jmap/handler.ts@@ -73,6 +73,7 @@ export async function handleJmapRequest(request: Request, env: CloudflareEnv): P"Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(name)}`,"Cache-Control": "private, max-age=3600","Content-Security-Policy": "default-src 'none'; sandbox",+ "X-Content-Type-Options": "nosniff",...corsHeaders(),},});