mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-23 14:17:06 +00:00
* feat: let the Contact Us form carry screenshots and recordings The form doubles as our bug-report channel, and the bugs worth reporting are often the ones that need a picture: a visual glitch, or something that takes five steps to reach. Up to 5 images or videos may now ride along with the message, delivered as attachments on the support email. Nothing the client says about a file is believed. The endpoint takes bare base64 and re-derives all three of the things that matter from the decoded bytes: - Type, sniffed from magic numbers and matched against an allow-list of PNG/JPEG/GIF/WebP and MP4/QuickTime/WebM. A declared MIME is never read, so it cannot smuggle anything past the list. SVG is excluded on purpose (it carries script, and support tooling renders what it is sent), as are the non-video brands that share MP4's `ftyp` box -- HEIC, M4A, JPEG 2000. - The file name, reduced to a display label with the extension taken from the sniffed type. PNG bytes named `payload.html` arrive as `payload.png`; a name can never carry the CR/LF that would break out of a Content-Disposition header, nor the bidi overrides that make `report<RLO>gnp.exe` render as `report.exe.mp4`. - Size: 10 MB per file, 15 MB per submission, counted on decoded bytes. Encoded length is capped before decoding, so an oversized payload costs a length check rather than a 10 MB allocation. The total stays well under the 25 MB most providers enforce, since the outgoing mail base64s these again. Base64 is decoded strictly, reusing the round-tripping decoder that already guards app icons -- `Buffer.from(s, 'base64')` silently drops characters it does not recognise, and the round-trip is what rejects bytes smuggled after the payload. That decoder and the image sniffer move from appIcon.ts to a new mediaSniff.ts, which gains the video counterpart. One request is now worth megabytes of parsing and outbound mail, so the existing per-user rate limit gains a per-IP backstop (which the per-user counter cannot see through freshly minted accounts), a concurrency cap, and a Content-Length gate that refuses an impossible body before anything decodes it. Payloads are not stored. They ride the email; the new `feedback.attachments` column records names, types and sizes only, so an abusive submission stays attributable once the mail has been dealt with. Also fixes the form posting an empty message when Send was pressed with nothing typed, and surfaces submit failures instead of leaving the button disabled with no explanation. * fix: stream Contact Us attachments as multipart instead of base64 JSON Base64 in a JSON body went through the global JSON parser before the route ran: raw buffer, rawBody copy, decoded string, parsed strings, then decoded Buffers plus a re-encode for the strict check. A max-size submission peaked around 110-135 MB of heap for 15 MB of files, and the route's Content-Length check ran after all of it. Attachments now arrive as multipart/form-data, which the global parser skips. Auth, rate limit, concurrency and the Content-Length 413 all run before the body is read, and busboy enforces the count, per-file and total caps while streaming, so only the decoded file bytes are held (~16 MB peak). On a broken limit the reader stops and the response closes the connection. JSON stays supported for message-only posts; a JSON `attachments` field is rejected. * fix: deliver Contact Us 413s instead of resetting the upload Closing the socket on a mid-stream limit sent the 413 and then reset the connection with the client still uploading, so the client could see a reset instead of the error. After a limit trips, the rest of the body is now read and discarded, bounded by the body budget; past the budget the request is destroyed. Adds an HTTP-level test through the full middleware stack (FormData upload, unauthenticated refusal, 413 on declared length before the body is sent, 413 on a chunked upload over the per-file cap), trims comments to the AGENTS.md length rule, and drops a box-drawing test divider. --------- Co-authored-by: Daniel Salazar <daniel.salazar@puter.com>
319 lines
11 KiB
TypeScript
319 lines
11 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 Busboy from 'busboy';
|
|
import type { IncomingHttpHeaders } from 'node:http';
|
|
import type { Readable } from 'node:stream';
|
|
import { sniffImageMime, sniffVideoMime } from './mediaSniff.js';
|
|
|
|
/**
|
|
* Screenshots and recordings attached to Contact Us, which end up in support's
|
|
* inbox. Type comes from the bytes and the file name is rebuilt, so nothing the
|
|
* sender declared reaches the mail.
|
|
*/
|
|
|
|
/** Max files on one submission. */
|
|
export const MAX_ATTACHMENTS = 5;
|
|
|
|
/** Max decoded size of any one file. */
|
|
export const MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024;
|
|
|
|
/**
|
|
* Max size of all files on one submission; base64 in the mail stays under 25
|
|
* MB.
|
|
*/
|
|
export const MAX_TOTAL_ATTACHMENT_BYTES = 15 * 1024 * 1024;
|
|
|
|
/** Max characters kept from the caller's file name, before the extension. */
|
|
export const MAX_ATTACHMENT_NAME_LENGTH = 80;
|
|
|
|
/**
|
|
* Accepted sniffed MIME types and the extension each is sent under. SVG is
|
|
* excluded: it carries script.
|
|
*/
|
|
export const ATTACHMENT_MIME_EXTENSIONS: Readonly<Record<string, string>> = {
|
|
'image/png': 'png',
|
|
'image/jpeg': 'jpg',
|
|
'image/gif': 'gif',
|
|
'image/webp': 'webp',
|
|
'video/mp4': 'mp4',
|
|
'video/quicktime': 'mov',
|
|
'video/webm': 'webm',
|
|
};
|
|
|
|
/**
|
|
* Stripped from file names: control characters (CR/LF could break out of
|
|
* `Content-Disposition`), bidi overrides that disguise an extension, and
|
|
* quotes.
|
|
*/
|
|
const UNSAFE_NAME_CHARS_REGEX =
|
|
/[\u0000-\u001F\u007F-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069"'\\;]/g;
|
|
|
|
const mb = (bytes: number): number => Math.round(bytes / (1024 * 1024));
|
|
const TOO_LARGE_CLAUSE = `is too large (max ${mb(MAX_ATTACHMENT_BYTES)} MB per file)`;
|
|
|
|
/** One validated attachment, in the shape nodemailer takes. */
|
|
export interface ValidatedAttachment {
|
|
/** Safe display name; extension always matches `contentType`. */
|
|
filename: string;
|
|
/** Sniffed, allow-listed MIME type. */
|
|
contentType: string;
|
|
content: Buffer;
|
|
size: number;
|
|
}
|
|
|
|
/** Stored on the feedback row in place of the payloads. */
|
|
export interface AttachmentMetadata {
|
|
name: string;
|
|
type: string;
|
|
size: number;
|
|
}
|
|
|
|
export type AttachmentVerdict =
|
|
| { ok: true; attachment: ValidatedAttachment }
|
|
| { ok: false; reason: string };
|
|
|
|
export type ContactSubmissionVerdict =
|
|
| {
|
|
ok: true;
|
|
/** The `message` field as sent; the caller validates it. */
|
|
message: string | undefined;
|
|
attachments: ValidatedAttachment[];
|
|
}
|
|
| { ok: false; status: 400 | 413; reason: string };
|
|
|
|
/**
|
|
* Reduce a caller-supplied file name to a safe display label and give it the
|
|
* extension implied by `extension` (derived from the sniffed type, never from
|
|
* the name). Falls back to `attachment-<n>` when nothing usable survives.
|
|
*/
|
|
export function sanitizeAttachmentName(
|
|
raw: unknown,
|
|
index: number,
|
|
extension: string,
|
|
): string {
|
|
let base = '';
|
|
if (typeof raw === 'string') {
|
|
base = (raw.split(/[/\\]/).pop() ?? '')
|
|
.normalize('NFC')
|
|
.replace(UNSAFE_NAME_CHARS_REGEX, '')
|
|
// Drop the caller's extension — the real one is appended below.
|
|
.replace(/\.[A-Za-z0-9]{1,10}$/, '')
|
|
// No hidden files or `..` if the name is ever written to disk.
|
|
.replace(/^[.\s]+/, '')
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
.slice(0, MAX_ATTACHMENT_NAME_LENGTH)
|
|
.trim();
|
|
}
|
|
if (base.length === 0) base = `attachment-${index + 1}`;
|
|
return `${base}.${extension}`;
|
|
}
|
|
|
|
/** Check a file's sniffed type against the allow-list; give it a safe name. */
|
|
export function validateAttachment(
|
|
bytes: Buffer,
|
|
rawName: unknown,
|
|
index: number,
|
|
): AttachmentVerdict {
|
|
const label = `attachment ${index + 1}`;
|
|
if (bytes.length === 0) {
|
|
return { ok: false, reason: `${label} is empty` };
|
|
}
|
|
const sniffed = sniffImageMime(bytes) ?? sniffVideoMime(bytes);
|
|
const extension = sniffed ? ATTACHMENT_MIME_EXTENSIONS[sniffed] : undefined;
|
|
if (!sniffed || !extension) {
|
|
return {
|
|
ok: false,
|
|
reason: `${label} is not a supported image or video`,
|
|
};
|
|
}
|
|
return {
|
|
ok: true,
|
|
attachment: {
|
|
filename: sanitizeAttachmentName(rawName, index, extension),
|
|
contentType: sniffed,
|
|
content: bytes,
|
|
size: bytes.length,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Read a multipart submission: one `message` field plus `attachments` files.
|
|
* Caps are enforced as parts arrive. On failure the rest of the body is
|
|
* discarded so the error still reaches the client, up to `maxBodyBytes`; past
|
|
* that the request is destroyed. `reason` never echoes caller input.
|
|
*/
|
|
export function readContactSubmission(
|
|
req: Readable & { headers: IncomingHttpHeaders },
|
|
{
|
|
maxMessageBytes,
|
|
maxBodyBytes,
|
|
}: { maxMessageBytes: number; maxBodyBytes: number },
|
|
): Promise<ContactSubmissionVerdict> {
|
|
return new Promise((resolve) => {
|
|
let parser: Busboy.Busboy;
|
|
try {
|
|
parser = Busboy({
|
|
headers: req.headers,
|
|
// Browsers send non-ASCII file names as raw UTF-8.
|
|
defParamCharset: 'utf8',
|
|
// Busboy trips a limit on reaching it, hence the + 1.
|
|
limits: {
|
|
fields: 1,
|
|
fieldSize: maxMessageBytes + 1,
|
|
files: MAX_ATTACHMENTS,
|
|
fileSize: MAX_ATTACHMENT_BYTES + 1,
|
|
},
|
|
});
|
|
} catch {
|
|
// Missing or malformed boundary.
|
|
resolve({
|
|
ok: false,
|
|
status: 400,
|
|
reason: 'malformed multipart body',
|
|
});
|
|
return;
|
|
}
|
|
|
|
let message: string | undefined;
|
|
const received: { name: string; chunks: Buffer[]; size: number }[] = [];
|
|
let totalBytes = 0;
|
|
let settled = false;
|
|
|
|
const fail = (status: 400 | 413, reason: string) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
req.unpipe(parser);
|
|
received.length = 0;
|
|
req.resume();
|
|
resolve({ ok: false, status, reason });
|
|
};
|
|
|
|
parser.on('field', (name, value, info) => {
|
|
if (name !== 'message') {
|
|
return fail(400, 'unexpected form field');
|
|
}
|
|
if (info.valueTruncated) {
|
|
return fail(400, '`message` is too long');
|
|
}
|
|
message = value;
|
|
});
|
|
|
|
parser.on('file', (name, stream, info) => {
|
|
// Busboy errors an unfinished file stream when the body ends
|
|
// early; unhandled, that crashes the process.
|
|
stream.on('error', () =>
|
|
fail(400, 'attachment upload was interrupted'),
|
|
);
|
|
if (settled) {
|
|
stream.resume();
|
|
return;
|
|
}
|
|
if (name !== 'attachments') {
|
|
stream.resume();
|
|
return fail(400, 'unexpected form field');
|
|
}
|
|
|
|
const label = `attachment ${received.length + 1}`;
|
|
const entry = {
|
|
name: info.filename,
|
|
chunks: [] as Buffer[],
|
|
size: 0,
|
|
};
|
|
received.push(entry);
|
|
stream.on('limit', () => fail(413, `${label} ${TOO_LARGE_CLAUSE}`));
|
|
stream.on('data', (chunk: Buffer) => {
|
|
if (settled) return;
|
|
totalBytes += chunk.length;
|
|
if (totalBytes > MAX_TOTAL_ATTACHMENT_BYTES) {
|
|
return fail(
|
|
413,
|
|
`attachments are too large in total (max ${mb(MAX_TOTAL_ATTACHMENT_BYTES)} MB)`,
|
|
);
|
|
}
|
|
entry.chunks.push(chunk);
|
|
entry.size += chunk.length;
|
|
});
|
|
});
|
|
|
|
parser.on('fieldsLimit', () => fail(400, 'unexpected form field'));
|
|
parser.on('filesLimit', () =>
|
|
fail(400, `too many attachments (max ${MAX_ATTACHMENTS})`),
|
|
);
|
|
parser.on('error', () => fail(400, 'malformed multipart body'));
|
|
req.on('error', () => fail(400, 'attachment upload was interrupted'));
|
|
|
|
parser.on('close', () => {
|
|
if (settled) return;
|
|
settled = true;
|
|
const attachments: ValidatedAttachment[] = [];
|
|
for (const [i, entry] of received.entries()) {
|
|
const verdict = validateAttachment(
|
|
Buffer.concat(entry.chunks, entry.size),
|
|
entry.name,
|
|
i,
|
|
);
|
|
if (verdict.ok === false) {
|
|
resolve({ ok: false, status: 400, reason: verdict.reason });
|
|
return;
|
|
}
|
|
attachments.push(verdict.attachment);
|
|
}
|
|
resolve({ ok: true, message, attachments });
|
|
});
|
|
|
|
req.pipe(parser);
|
|
|
|
let bytesRead = 0;
|
|
req.on('data', (chunk: Buffer) => {
|
|
bytesRead += chunk.length;
|
|
if (bytesRead <= maxBodyBytes) return;
|
|
fail(413, 'request body is too large');
|
|
req.destroy();
|
|
});
|
|
});
|
|
}
|
|
|
|
/** Names and sizes for the stored feedback row — never the payloads. */
|
|
export function attachmentMetadata(
|
|
attachments: ValidatedAttachment[],
|
|
): AttachmentMetadata[] {
|
|
return attachments.map((a) => ({
|
|
name: a.filename,
|
|
type: a.contentType,
|
|
size: a.size,
|
|
}));
|
|
}
|
|
|
|
/** Body manifest, so a recipient can tell if a gateway stripped the files. */
|
|
export function attachmentSummary(attachments: ValidatedAttachment[]): string {
|
|
const lines = attachments.map(
|
|
(a) => `- ${a.filename} (${a.contentType}, ${formatBytes(a.size)})`,
|
|
);
|
|
return [`Attachments (${attachments.length}):`, ...lines].join('\n');
|
|
}
|
|
|
|
function formatBytes(bytes: number): string {
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
}
|