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 <[email protected]>
This commit is contained in:
Andrei Drăgan RădulețandClaude Fable 5 authored and Julius Marminge committed 2026-07-20 18:08:18 +02:00
1 parent cca513e2ab
commit b18a3296f5
4 files changed
+62 -9

No files matched your search

+16 -2
View File
@@ -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}`;
}
}
}
+42
View File
@@ -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)
@@ -338,9 +338,6 @@ function collectThreadAttachmentRelativePaths(
const relativePaths = new Set<string>();
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;
+4 -4
View File
@@ -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(),