Skip to content

Email for professionals and teams

fix(security): cap Content-Disposition filename in the helper (#222)

Hieu Nguyen (Jack)aeb9ff8d5ef7
diff --git a/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts b/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts
index d373b02..84ca0f1 100644
--- a/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts
+++ b/src/app/api/messages/[messageId]/attachments/[attachmentId]/utils.ts
@@ -1,8 +1,11 @@
+import { truncateUtf8 } from "@/lib/email/inbound-limits";
+
// 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 cleaned = filename.replace(CONTROL_AND_BIDI, "").trim() || "attachment";
+ // Rows stored before filenames were capped can still be long; keep the header bounded.
+ const cleaned = truncateUtf8(filename.replace(CONTROL_AND_BIDI, "").trim(), 180) || "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()}`);