Files
java-ai-agents/mcp-apps/harness/csp.mjs
T

19 lines
832 B
JavaScript

// The Content-Security-Policy a strict host builds, following "Restrictive Default" in the 2026-01-26 spec.
const clean = (list) => (list ?? []).filter((d) => typeof d === "string" && !/[;\r\n'" ]/.test(d)).join(" ");
export function buildCsp(csp = {}) {
const resources = clean(csp.resourceDomains);
return [
"default-src 'none'",
`script-src 'self' 'unsafe-inline' ${resources}`,
`style-src 'self' 'unsafe-inline' ${resources}`,
`img-src 'self' data: ${resources}`,
`media-src 'self' data: ${resources}`,
`font-src ${resources || "'none'"}`,
`connect-src ${clean(csp.connectDomains) || "'none'"}`,
`frame-src ${clean(csp.frameDomains) || "'none'"}`,
"object-src 'none'",
`base-uri ${clean(csp.baseUriDomains) || "'self'"}`,
].map((d) => d.trim().replace(/\s+/g, " ")).join("; ");
}