/* * Copyright (C) 2024-present Puter Technologies Inc. * * This file is part of Puter. * * Puter is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import { setImmediate as nextTurn } from 'node:timers/promises'; // Identify uploaded media by its bytes. A caller-supplied MIME type or file // extension is never trusted. /** * How far into a payload to look for an ` { if (value.length <= BASE64_SLICE_CHARS) return decodeStrictBase64(value); if (value.length % 4 !== 0) return null; const padding = value.endsWith('==') ? 2 : value.endsWith('=') ? 1 : 0; const bytes = Buffer.allocUnsafe((value.length / 4) * 3 - padding); let written = 0; for (let at = 0; at < value.length; at += BASE64_SLICE_CHARS) { const slice = decodeStrictBase64( value.slice(at, at + BASE64_SLICE_CHARS), ); // Padding inside the string leaves a slice short, and the total with it. if (!slice || written + slice.length > bytes.length) return null; written += slice.copy(bytes, written); await nextTurn(); } return written === bytes.length ? bytes : null; } function looksLikeSvg(bytes: Buffer): boolean { let head = bytes.subarray(0, SVG_SNIFF_WINDOW).toString('utf8'); if (head.charCodeAt(0) === 0xfeff) head = head.slice(1); // Must open as markup, not just mention `]/i.test(head); } /** * Canonical image MIME type for `bytes`, or null. Recognized is not accepted: * this can return script-capable `image/svg+xml`, so callers allow-list. */ export function sniffImageMime(bytes: Buffer): string | null { if ( bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a ) { return 'image/png'; } if ( bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff ) { return 'image/jpeg'; } if (bytes.length >= 6) { const head = bytes.subarray(0, 6).toString('latin1'); if (head === 'GIF87a' || head === 'GIF89a') return 'image/gif'; } if ( bytes.length >= 12 && bytes.subarray(0, 4).toString('latin1') === 'RIFF' && bytes.subarray(8, 12).toString('latin1') === 'WEBP' ) { return 'image/webp'; } if (looksLikeSvg(bytes)) return 'image/svg+xml'; return null; } /** * Canonical video MIME type for `bytes` (MP4, QuickTime, WebM, Matroska), or * null. */ export function sniffVideoMime(bytes: Buffer): string | null { // ISO Base Media: a `ftyp` box at offset 4, major brand at offset 8. if ( bytes.length >= 12 && bytes.subarray(4, 8).toString('latin1') === 'ftyp' ) { const brand = bytes.subarray(8, 12).toString('latin1'); if (brand === 'qt ') return 'video/quicktime'; return MP4_BRANDS.has(brand) ? 'video/mp4' : null; } // EBML: Matroska and WebM share the magic; the DocType tells them apart. if ( bytes.length >= 4 && bytes[0] === 0x1a && bytes[1] === 0x45 && bytes[2] === 0xdf && bytes[3] === 0xa3 ) { const head = bytes.subarray(0, EBML_SNIFF_WINDOW).toString('latin1'); if (head.includes('webm')) return 'video/webm'; if (head.includes('matroska')) return 'video/x-matroska'; } return null; }