UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

78 lines (77 loc) • 3.39 kB
import { createHash } from "node:crypto"; //#region src/gateway/control-ui-csp.ts /** The contextual host policy admits explicit secure origins and the supported embedding scheme. */ function validateRemoteControlUiFrameAncestors(e) { if (!Array.isArray(e) || e.length > 16 || e.join(" ").length > 4096) throw new Error("Remote Control UI frame ancestors must contain at most 16 bounded origins"); for (const t of e) { if (t === "codex-sandbox:") continue; const e = typeof t === "string" ? URL.parse(t) : null; if (!e || e.protocol !== "https:" || e.origin !== t || e.hostname.includes("*") && !/^https:\/\/\*\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(t) || e.username || e.password) throw new Error("Remote Control UI frame ancestors must be exact HTTPS origins, https://*.<domain>, or codex-sandbox:"); } return Object.freeze([...new Set(e)]); } const SCRIPT_ATTRIBUTE_NAME_RE = /\s([^\s=/>]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?/g; /** * Compute SHA-256 CSP hashes for inline `<script>` blocks in an HTML string. * Only scripts without a `src` attribute are considered inline. */ function computeInlineScriptHashes(e) { const t = []; const n = /<script(?:\s[^>]*)?>([^]*?)<\/script>/gi; let r; while ((r = n.exec(e)) !== null) { const e = r[0].slice(0, r[0].indexOf(">") + 1); if (Array.from(e.matchAll(SCRIPT_ATTRIBUTE_NAME_RE)).some((e) => e[1]?.toLowerCase() === "src")) continue; const n = r[1]; if (!n) continue; const i = createHash("sha256").update(n, "utf8").digest("base64"); t.push(`sha256-${i}`); } return t; } /** Build the CSP header applied to Gateway-served Control UI HTML. */ function buildControlUiCspHeader(e) { const t = e?.inlineScriptHashes; const n = ["'self'"]; if (t?.length) n.push(...t.map((e) => `'${e}'`)); if (e?.allowWasm) n.push("'wasm-unsafe-eval'"); const r = [ "'self'", "ws:", "wss:", "data:", "blob:", "https://api.openai.com", "https://tweakcn.com" ]; if (e?.portalHost) { const t = URL.parse(`http://${e.portalHost}`); if (t?.hostname && !t.username && !t.password && t.pathname === "/" && !t.search && !t.hash) r.push(`http://${t.hostname}:*`, `https://${t.hostname}:*`); } return [ "default-src 'self'", "base-uri 'none'", "object-src 'none'", `frame-ancestors ${e?.frameAncestors?.length ? e.frameAncestors.join(" ") : "'none'"}`, "frame-src 'self' blob: http: https:", `script-src ${n.join(" ")}`, "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com", "img-src 'self' data: blob: https:", "media-src 'self' data: blob:", "font-src 'self' https://fonts.gstatic.com", "worker-src 'self'", `connect-src ${r.join(" ")}` ].join("; "); } function applyControlUiSecurityHeaders(e, t) { t?.signal.throwIfAborted(); t?.assertCurrent(); if (t?.frameAncestors.length) e.removeHeader("X-Frame-Options"); else e.setHeader("X-Frame-Options", "DENY"); e.setHeader("Content-Security-Policy", buildControlUiCspHeader({ frameAncestors: t?.frameAncestors })); e.setHeader("X-Content-Type-Options", "nosniff"); e.setHeader("Referrer-Policy", "no-referrer"); e.setHeader("Permissions-Policy", "camera=(self), microphone=*, geolocation=*, clipboard-write=*"); } //#endregion export { validateRemoteControlUiFrameAncestors as i, buildControlUiCspHeader as n, computeInlineScriptHashes as r, applyControlUiSecurityHeaders as t };