Repository Public
1import { HttpError } from "@/lib/errors";2/**3 * Server-side sanity check of an editor document before it is stored: it must be a ProseMirror `doc`, bounded in size and depth, and every4 * node must have a string type. (Rendering is done by Tiptap from JSON — never as HTML — so this is about integrity, not sanitising markup.)5 */6const MAX_BYTES = 8 * 1024 * 1024, MAX_DEPTH = 60, MAX_NODES = 200_000;7export function assertDoc(doc: unknown): void {8 if (!doc || typeof doc !== "object" || (doc as any).type !== "doc" || !Array.isArray((doc as any).content ?? [])) throw new HttpError(400, "Invalid document", "bad_document");9 let nodes = 0;10 const walk = (n: any, depth: number) => {11 if (++nodes > MAX_NODES) throw new HttpError(413, "Document has too many blocks", "bad_document");12 if (depth > MAX_DEPTH) throw new HttpError(400, "Document is nested too deeply", "bad_document");13 if (!n || typeof n !== "object" || typeof n.type !== "string" || n.type.length > 64) throw new HttpError(400, "Invalid document node", "bad_document");14 if (n.text !== undefined && typeof n.text !== "string") throw new HttpError(400, "Invalid text node", "bad_document");15 if (n.content !== undefined) { if (!Array.isArray(n.content)) throw new HttpError(400, "Invalid document node", "bad_document"); for (const c of n.content) walk(c, depth + 1); }16 if (n.marks !== undefined && (!Array.isArray(n.marks) || n.marks.some((m: any) => !m || typeof m.type !== "string"))) throw new HttpError(400, "Invalid marks", "bad_document");17 // javascript: and data: URLs are never valid link or media targets18 const href = n.attrs?.href ?? n.attrs?.src; if (typeof href === "string" && /^\s*(javascript|vbscript|data:text\/html)/i.test(href)) throw new HttpError(400, "Unsafe URL in document", "bad_document");19 for (const m of n.marks ?? []) { const h = m.attrs?.href; if (typeof h === "string" && /^\s*(javascript|vbscript|data:text\/html)/i.test(h)) throw new HttpError(400, "Unsafe URL in document", "bad_document"); }20 };21 walk(doc, 0);22 if (JSON.stringify(doc).length > MAX_BYTES) throw new HttpError(413, "Document is too large (8 MB max)", "bad_document");23}24