import { env } from "cloudflare:workers"; const DEV_FORGE_HOST = "localhost"; const DEV_EDITOR_HOST = "editor.localhost"; function within(host: string, domain: string | undefined): boolean { const base = (domain ?? "") .trim() .toLowerCase() .replace(/^\.+|\.+$/g, ""); return base !== "" && (host === base || host.endsWith(`.${base}`)); } /** * Why an EDITOR_ORIGIN is unsafe, or null. It must be its own host: never the forge host (cookies and * pages), and never under SITES_DOMAIN or PAGES_DOMAIN, where user content could frame or impersonate it. */ export function editorOriginProblem(origin: URL, forgeHost: string): string | null { const host = origin.hostname.toLowerCase(); if (host === forgeHost.toLowerCase()) return "EDITOR_ORIGIN must not be the forge host"; if (within(host, env.SITES_DOMAIN)) return "EDITOR_ORIGIN must not be under SITES_DOMAIN (user content)"; if (within(host, env.PAGES_DOMAIN)) return "EDITOR_ORIGIN must not be under PAGES_DOMAIN (user content)"; if (origin.protocol !== "https:" && !within(host, "localhost")) return "EDITOR_ORIGIN must use https"; return null; } let validated: { raw: string; url: URL | null } | null = null; /** The configured editor origin, validated once per isolate; an unsafe value disables the editor. */ function configured(): URL | null { if (!env.EDITOR_ORIGIN) return null; if (validated?.raw === env.EDITOR_ORIGIN) return validated.url; let url: URL | null = null; try { url = new URL(env.EDITOR_ORIGIN); const problem = editorOriginProblem(url, new URL(env.PUBLIC_URL).hostname); if (problem) { console.error(`Editor disabled: ${problem} (got ${url.origin})`); url = null; } } catch { console.error("Editor disabled: EDITOR_ORIGIN is not a valid URL"); url = null; } validated = { raw: env.EDITOR_ORIGIN, url }; return url; } /** True when EDITOR_ORIGIN is set but rejected, so callers can refuse instead of falling back to dev hosts. */ function misconfigured(): boolean { return Boolean(env.EDITOR_ORIGIN) && configured() === null; } /** * The editor runs on its own origin so its scripts (VS Code, Open VSX extensions) can never reach forge * cookies or pages. Production sets EDITOR_ORIGIN, ideally on a separate registrable domain; local dev maps * `localhost:` to `editor.localhost:` (browsers resolve `*.localhost` to loopback, and the * forge's host-only cookies are never sent to another host). */ export function editorOriginFor(forgeUrl: URL): string | null { const explicit = configured(); if (explicit) return explicit.origin; if (misconfigured() || forgeUrl.hostname !== DEV_FORGE_HOST) return null; const url = new URL(forgeUrl.origin); url.hostname = DEV_EDITOR_HOST; return url.origin; } export function isEditorOrigin(url: URL): boolean { const explicit = configured(); if (explicit) return url.origin === explicit.origin; return !misconfigured() && url.hostname === DEV_EDITOR_HOST; } /** The forge origin an editor-origin page talks to and accepts messages from. */ export function forgeOriginFor(editorUrl: URL): string { if (configured()) return new URL(env.PUBLIC_URL).origin; const url = new URL(editorUrl.origin); url.hostname = DEV_FORGE_HOST; return url.origin; }