mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-25 15:17:23 +00:00
Maintain Release Merge PR / update-release-pr (push) Canceled after 0s
Notify HeyPuter / notify (push) Canceled after 0s
release-please / release-please (push) Canceled after 0s
Backend Tests / changes (push) Canceled after 0s
Backend Tests / typecheck (push) Canceled after 0s
Backend Tests / test shard 1/4 (push) Canceled after 0s
Backend Tests / test shard 2/4 (push) Canceled after 0s
Backend Tests / test shard 3/4 (push) Canceled after 0s
Backend Tests / test shard 4/4 (push) Canceled after 0s
Backend Tests / test (main) (push) Canceled after 0s
Backend Tests / coverage (push) Canceled after 0s
JSON request bodies over 32 KiB are parsed in slices, with the event loop running between them, instead of in one JSON.parse call. Reading, limits, decompression, req.rawBody and the errors stay as express.json has them. Large bodies parse one at a time. Gemini TTS and video replies use the same parser, and their base64 is checked and decoded 1 MiB at a time. A profile picture is sized before its pattern runs, so one of several megabytes gets a 413. Body parsers skip the dav subdomain: a PUT labeled as JSON or form data was read by them and stored empty, or refused. puter.js sends auth_token as the first field of a driver call. A chat body over 50 MiB that opens with a valid token is accepted up to 128 MiB, as one with a header credential is. JSON in a charset other than UTF-8 is now limited to 1 MiB, and a body over 32 KiB nested deeper than 10,000 levels is refused.
178 lines
5.9 KiB
TypeScript
178 lines
5.9 KiB
TypeScript
/*
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
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 `<svg` root; SVG has no fixed magic
|
|
* number.
|
|
*/
|
|
export const SVG_SNIFF_WINDOW = 8 * 1024;
|
|
|
|
/** How far into an EBML header to look for the DocType string. */
|
|
const EBML_SNIFF_WINDOW = 64;
|
|
|
|
/**
|
|
* ISO Base Media brands treated as MP4 video. HEIF, M4A audio and JPEG 2000
|
|
* share the `ftyp` box, so the brand decides.
|
|
*/
|
|
const MP4_BRANDS = new Set([
|
|
'avc1',
|
|
'dash',
|
|
'iso2',
|
|
'iso4',
|
|
'iso5',
|
|
'iso6',
|
|
'isom',
|
|
'mmp4',
|
|
'mp41',
|
|
'mp42',
|
|
'mp71',
|
|
'M4V ',
|
|
'M4VH',
|
|
'M4VP',
|
|
'M4P ',
|
|
]);
|
|
|
|
/**
|
|
* Decode padded, standard-alphabet base64 (the form data URLs accept), or null.
|
|
* `Buffer.from` skips characters outside the alphabet, so any of them leaves
|
|
* the output shorter than the length predicts. Avoids regexps, which cost a
|
|
* full extra pass over multi-MB payloads.
|
|
*/
|
|
export function decodeStrictBase64(value: string): Buffer | null {
|
|
if (value.length === 0 || value.length % 4 !== 0) return null;
|
|
// Node decodes a code unit above 0xFF by its low byte (`ī` as `+`), so
|
|
// only ASCII is decoded.
|
|
if (Buffer.byteLength(value, 'utf8') !== value.length) return null;
|
|
// Node also decodes the URL-safe alphabet.
|
|
if (value.includes('-') || value.includes('_')) return null;
|
|
const padding = value.endsWith('==') ? 2 : value.endsWith('=') ? 1 : 0;
|
|
const bytes = Buffer.from(value, 'base64');
|
|
return bytes.length === (value.length / 4) * 3 - padding ? bytes : null;
|
|
}
|
|
|
|
/** Base64 characters decoded per turn; a multiple of four. */
|
|
const BASE64_SLICE_CHARS = 1024 * 1024;
|
|
|
|
/**
|
|
* `decodeStrictBase64` for payloads of many megabytes: decodes a slice per turn
|
|
* of the event loop, so the whole string never holds it.
|
|
*/
|
|
export async function decodeStrictBase64InSlices(
|
|
value: string,
|
|
): Promise<Buffer | null> {
|
|
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 `<svg` somewhere in text.
|
|
if (!head.trimStart().startsWith('<')) return false;
|
|
return /<svg[\s/>]/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;
|
|
}
|