/** * Training-attachment storage. Files are written under `UPLOAD_DIR` (default * `/tmp/uploads`, a named Docker volume in the deployment) and only metadata is * stored in Postgres. Event photos and videos use the configured media storage * instead (see `media-storage.ts`); they are never written here. */ import path from "node:path"; import { mkdir } from "node:fs/promises"; export const UPLOAD_DIR = process.env.UPLOAD_DIR ?? "/tmp/uploads"; export const MAX_UPLOAD_BYTES = 25 * 1024 * 1024; const MIME_BY_EXTENSION: Record = { ".pdf": "application/pdf", ".doc": "application/msword", ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".txt": "text/plain", ".md": "text/markdown", ".markdown": "text/markdown", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".avif": "image/avif", }; const ALLOWED_MIME_PREFIXES = ["image/"]; const ALLOWED_MIME_TYPES = [ "application/pdf", "text/plain", "text/markdown", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ]; export function isAllowedUpload(mimeType: string): boolean { // SVG is an active document: served inline it can run script in this origin. if (mimeType === "image/svg+xml") return false; return ( ALLOWED_MIME_PREFIXES.some((prefix) => mimeType.startsWith(prefix)) || ALLOWED_MIME_TYPES.includes(mimeType) ); } /** * Browsers sometimes leave File.type empty for a PDF or document. Fall back to * the extension so a legitimate manual is not rejected as octet-stream. */ export function resolveUploadMimeType( filename: string, declaredType: string, ): string { const declared = declaredType.split(";")[0].trim().toLowerCase(); if (declared && declared !== "application/octet-stream") return declared; return MIME_BY_EXTENSION[path.extname(filename).toLowerCase()] ?? "application/octet-stream"; } function startsWith(bytes: Uint8Array, signature: number[]): boolean { return signature.every((value, index) => bytes[index] === value); } function ascii(bytes: Uint8Array, start: number, length: number): string { return String.fromCharCode(...bytes.slice(start, start + length)); } /** * Verify that the bytes plausibly match the declared MIME type. Client-provided * `File.type` is advisory; without this an upload could claim `image/png` and * deliver an HTML/SVG payload. Text formats have no magic number, so they are * accepted when the content is NUL-free (a binary masquerading as text fails). */ export function uploadSignatureMatches( mimeType: string, bytes: Uint8Array, ): boolean { const head = bytes.slice(0, 16); const hasNul = head.includes(0); if (mimeType === "application/pdf") return ascii(head, 0, 5) === "%PDF-"; if (mimeType === "image/png") { return startsWith(head, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); } if (mimeType === "image/jpeg") return startsWith(head, [0xff, 0xd8, 0xff]); if (mimeType === "image/gif") { return ascii(head, 0, 4) === "GIF8"; } if (mimeType === "image/webp") { return ascii(head, 0, 4) === "RIFF" && ascii(head, 8, 4) === "WEBP"; } if (mimeType.startsWith("image/")) return !hasNul; if ( mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) { return startsWith(head, [0x50, 0x4b, 0x03, 0x04]); } if (mimeType === "application/msword") { return startsWith(head, [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1]); } return !hasNul; } /** A random on-disk name: UUIDv7 + a sanitized extension from the original. */ export function storedNameFor(originalName: string, id: string): string { const ext = path.extname(originalName).toLowerCase().replace(/[^.a-z0-9]/g, ""); return `${id}${ext}`; } export async function ensureUploadDir(): Promise { await mkdir(UPLOAD_DIR, { recursive: true }); } /** Resolve a stored name inside UPLOAD_DIR, refusing path traversal. */ export function uploadPath(storedName: string): string { return path.join(UPLOAD_DIR, path.basename(storedName)); }