openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.07 kB
JavaScript
import{createHash}from"node:crypto";
//!src/gateway/control-ui-csp.ts
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;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}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 buildPdfPreviewWorkerCspHeader(e){return[`default-src 'none'`,`base-uri 'none'`,`object-src 'none'`,`frame-ancestors `+(e?[e.publicOrigin,...e.frameAncestors].join(` `):`'self'`),`script-src 'none'`,`connect-src 'none'`,`worker-src 'none'`,`form-action 'none'`].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=*`)}export{validateRemoteControlUiFrameAncestors as a,computeInlineScriptHashes as i,buildControlUiCspHeader as n,buildPdfPreviewWorkerCspHeader as r,applyControlUiSecurityHeaders as t};