// READING A PUBLISHED ARCHIVE, AS UNTRUSTED INPUT (demo epic D1 re-review, item 3). // // An archive is someone else's server and someone else's JSON. The editor // reads one through common/lib/remoteArchives-server.ts (fetchOnOrigin, // readBounded, and archiveRecordOf's "a manifest the corpus names elsewhere is // not followed"); this is the same three rules for the render's cue walk and // the other readers of createJsonCache (cues.mjs), in plain JS because these // scripts run under bare node with no tsx. Change one, change the other. // // - a document is read on the origin it was asked on: a redirect is followed // by hand, up to three, and only to the same scheme, host and port; // - no document past 32 MB is read (a shard page is up to 8 MB); // - a channel's transcripts manifest is the one corpus.json names only when // that is on the corpus's own origin; otherwise the contract's URL. export const MAX_ARCHIVE_DOCUMENT_BYTES = 32 * 1024 * 1024; const MAX_REDIRECTS = 3; /** Whether `url` is on `origin`'s own scheme, host and port. */ export function sameOrigin(url, origin) { try { return new URL(url).origin === new URL(origin).origin; } catch { return false; } } /** * A GET that stays on `url`'s origin. `fetchImpl(url, init)` is asked with * `redirect: "manual"`; a 3xx with a Location is followed only on the same * origin, at most three times, and one that leaves it throws before it is * followed. */ export async function fetchOnOrigin(fetchImpl, url, init = {}) { let at = url; for (let hop = 0; ; hop += 1) { const res = await fetchImpl(at, { ...init, redirect: "manual" }); const status = Number(res?.status); if (!(status >= 300 && status <= 399) || status === 304) return res; const location = res.headers?.get?.("location"); if (!location) return res; const next = new URL(location, at).href; if (!sameOrigin(next, url)) { throw new Error(`GET ${at} redirects to ${new URL(next).origin}: an archive's documents are read from its own origin only`); } if (hop >= MAX_REDIRECTS) throw new Error(`GET ${url} redirects more than ${MAX_REDIRECTS} times`); at = next; } } /** * A response's JSON, refused past `max` bytes: by its Content-Length first, * then as the body streams. A response with no stream (a test's stub) is read * as it offers itself, its text measured when it has one. */ export async function readJsonBounded(res, url, max = MAX_ARCHIVE_DOCUMENT_BYTES) { const over = () => new Error(`GET ${url} is over ${Math.round(max / 1024 / 1024)} MB: an archive document that large is not read`); const declared = Number(res.headers?.get?.("content-length")); if (Number.isFinite(declared) && declared > max) { await res.body?.cancel?.().catch(() => {}); throw over(); } if (res.body && typeof res.body.getReader === "function") { const reader = res.body.getReader(); const chunks = []; let total = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; total += value.byteLength; if (total > max) { await reader.cancel().catch(() => {}); throw over(); } chunks.push(value); } return JSON.parse(Buffer.concat(chunks).toString("utf8")); } if (typeof res.text === "function") { const text = await res.text(); if (Buffer.byteLength(text) > max) throw over(); return JSON.parse(text); } return res.json(); } /** * Where a channel's transcripts manifest is read: the URL its corpus.json * entry names when that is on `origin` itself (a relative one is resolved * against it), else the contract's `/transcripts//manifest.json` * — a manifest named on another origin is never followed. */ export function transcriptsManifestUrl(origin, entry) { const base = new URL(origin).origin; const contract = `${base}/transcripts/${encodeURIComponent(String(entry?.slug ?? ""))}/manifest.json`; const named = entry?.manifests?.transcripts; if (typeof named !== "string" || !named.trim()) return contract; let resolved; try { resolved = new URL(named.trim(), `${base}/`); } catch { return contract; } if (resolved.origin !== base || resolved.username || resolved.password) return contract; return resolved.href; }