From b18a3296f56f99a1692bb4e5c1619381f8a65db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Dr=C4=83gan=20R=C4=83dule=C8=9B?= Date: Thu, 16 Jul 2026 20:59:44 +0200 Subject: [PATCH] feat(server): persist non-image file attachments - inferFileExtension with a conservative SAFE_FILE_EXTENSIONS allowlist (.bin fallback) so resolve-by-id keeps working and risky extensions (.html/.svg-for-files) never land on the same-origin asset route - Normalizer accepts any MIME for type:"file" uploads; image gate and size limits unchanged - attachment GC keep-set includes file attachments (previously any non-image attachment would be pruned from disk) Co-Authored-By: Claude Fable 5 --- apps/server/src/attachmentStore.ts | 18 +++++++- apps/server/src/imageMime.ts | 42 +++++++++++++++++++ .../Layers/ProjectionPipeline.ts | 3 -- apps/server/src/orchestration/Normalizer.ts | 8 ++-- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index 3d5b531db2..857e39d322 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -8,9 +8,16 @@ import { normalizeAttachmentRelativePath, resolveAttachmentRelativePath, } from "./attachmentPaths.ts"; -import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts"; +import { + inferFileExtension, + inferImageExtension, + SAFE_FILE_EXTENSIONS, + SAFE_IMAGE_FILE_EXTENSIONS, +} from "./imageMime.ts"; -const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"]; +const ATTACHMENT_FILENAME_EXTENSIONS = [ + ...new Set([...SAFE_IMAGE_FILE_EXTENSIONS, ...SAFE_FILE_EXTENSIONS, ".bin"]), +]; const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80; const ATTACHMENT_ID_THREAD_SEGMENT_PATTERN = "[a-z0-9_]+(?:-[a-z0-9_]+)*"; const ATTACHMENT_ID_UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; @@ -63,6 +70,13 @@ export function attachmentRelativePath(attachment: ChatAttachment): string { }); return `${attachment.id}${extension}`; } + case "file": { + const extension = inferFileExtension({ + mimeType: attachment.mimeType, + fileName: attachment.name, + }); + return `${attachment.id}${extension}`; + } } } diff --git a/apps/server/src/imageMime.ts b/apps/server/src/imageMime.ts index 66ce6096e8..5b4c590db4 100644 --- a/apps/server/src/imageMime.ts +++ b/apps/server/src/imageMime.ts @@ -112,6 +112,48 @@ export function parseBase64DataUrl( return { mimeType, base64 }; } +export const SAFE_FILE_EXTENSIONS = new Set([ + ".csv", + ".diff", + ".docx", + ".gz", + ".json", + ".jsonl", + ".log", + ".md", + ".patch", + ".pdf", + ".pptx", + ".tar", + ".toml", + ".tsv", + ".txt", + ".xlsx", + ".xml", + ".yaml", + ".yml", + ".zip", +]); + +export function inferFileExtension(input: { mimeType: string; fileName?: string }): string { + const fileName = input.fileName?.trim() ?? ""; + const extensionMatch = /\.([a-z0-9]{1,8})$/i.exec(fileName); + const fileNameExtension = extensionMatch ? `.${extensionMatch[1]!.toLowerCase()}` : ""; + if (SAFE_FILE_EXTENSIONS.has(fileNameExtension)) { + return fileNameExtension; + } + + // Mime.getExtension returns dot-less extensions ("pdf"), the sets hold + // dotted ones (".pdf"). + const fromMimeExtension = Mime.getExtension(input.mimeType); + const dottedMimeExtension = fromMimeExtension ? `.${fromMimeExtension}` : null; + if (dottedMimeExtension && SAFE_FILE_EXTENSIONS.has(dottedMimeExtension)) { + return dottedMimeExtension; + } + + return ".bin"; +} + export function inferImageExtension(input: { mimeType: string; fileName?: string }): string { const key = input.mimeType.toLowerCase(); const fromMime = Object.hasOwn(IMAGE_EXTENSION_BY_MIME_TYPE, key) diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts index f12df85094..7b58620eb2 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts @@ -338,9 +338,6 @@ function collectThreadAttachmentRelativePaths( const relativePaths = new Set(); for (const message of messages) { for (const attachment of message.attachments ?? []) { - if (attachment.type !== "image") { - continue; - } const attachmentThreadSegment = parseThreadSegmentFromAttachmentId(attachment.id); if (!attachmentThreadSegment || attachmentThreadSegment !== threadSegment) { continue; diff --git a/apps/server/src/orchestration/Normalizer.ts b/apps/server/src/orchestration/Normalizer.ts index 24c65900b2..9df1f06e75 100644 --- a/apps/server/src/orchestration/Normalizer.ts +++ b/apps/server/src/orchestration/Normalizer.ts @@ -109,16 +109,16 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => (attachment) => Effect.gen(function* () { const parsed = parseBase64DataUrl(attachment.dataUrl); - if (!parsed || !parsed.mimeType.startsWith("image/")) { + if (!parsed || (attachment.type === "image" && !parsed.mimeType.startsWith("image/"))) { return yield* new OrchestrationDispatchCommandError({ - message: `Invalid image attachment payload for '${attachment.name}'.`, + message: `Invalid ${attachment.type} attachment payload for '${attachment.name}'.`, }); } const bytes = Buffer.from(parsed.base64, "base64"); if (bytes.byteLength === 0 || bytes.byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) { return yield* new OrchestrationDispatchCommandError({ - message: `Image attachment '${attachment.name}' is empty or too large.`, + message: `Attachment '${attachment.name}' is empty or too large.`, }); } @@ -130,7 +130,7 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => } const persistedAttachment = { - type: "image" as const, + type: attachment.type, id: attachmentId, name: attachment.name, mimeType: parsed.mimeType.toLowerCase(),