Spaces:
Running
Running
Use per-request CSP nonce instead of script-src unsafe-inline (part 2)
Browse files25 unmodified lines26272829303132333435369 unmodified lines464748495051294 unmodified lines34634734834935035135282 unmodified lines43543643743843944044112 unmodified lines45445545645745845925 unmodified lines"/googlecae40e84854ca067.html",]);const PANEL_SECURITY_HEADERS = [["Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' ws: wss:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests",],["Strict-Transport-Security", "max-age=31536000; includeSubDomains"],["X-Content-Type-Options", "nosniff"],["X-Frame-Options", "SAMEORIGIN"],9 unmodified lines}}const PUBLIC_SEO_PATHS = new Set(["/","/home",294 unmodified linesif (edgeCache && cacheKey) {const cachedResp = await edgeCache.match(cacheKey);if (cachedResp) {return cachedResp;}}82 unmodified linesrespHeaders.set("Cache-Control", cacheControlRule);}if (isPanelHost && !incomingUrl.pathname.startsWith("/port/")) {applyPanelSecurityHeaders(respHeaders);}12 unmodified lines}}return finalResp;},};25 unmodified lines26272829303132333435369 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182838485868788294 unmodified lines38338438538638738838939039182 unmodified lines47447547647747847948048112 unmodified lines49449549649749849925 unmodified lines"/googlecae40e84854ca067.html",]);const PANEL_BASE_CSP ="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' ws: wss:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests";const PANEL_SECURITY_HEADERS = [["Content-Security-Policy", PANEL_BASE_CSP],["Strict-Transport-Security", "max-age=31536000; includeSubDomains"],["X-Content-Type-Options", "nosniff"],["X-Frame-Options", "SAMEORIGIN"],9 unmodified lines}}function generateCspNonce() {const bytes = new Uint8Array(16);crypto.getRandomValues(bytes);let bin = "";for (let i = 0; i < bytes.length; i++) {bin += String.fromCharCode(bytes[i]);}return btoa(bin);}function buildPanelCsp(nonce) {if (!nonce) return PANEL_BASE_CSP;return PANEL_BASE_CSP.replace("script-src 'self'", `script-src 'self' 'nonce-${nonce}'`);}function applyPanelHtmlNonceCsp(resp) {const ct = (resp.headers.get("content-type") || "").toLowerCase();if (!ct.includes("text/html") || typeof HTMLRewriter === "undefined") {return resp;}const nonce = generateCspNonce();const headers = new Headers(resp.headers);headers.set("Content-Security-Policy", buildPanelCsp(nonce));const rewritten = new HTMLRewriter().on("script", {element(el) {el.setAttribute("nonce", nonce);},}).transform(resp);return new Response(rewritten.body, {status: rewritten.status,statusText: rewritten.statusText,headers,});}const PUBLIC_SEO_PATHS = new Set(["/","/home",294 unmodified linesif (edgeCache && cacheKey) {const cachedResp = await edgeCache.match(cacheKey);if (cachedResp) {return isPanelHost && !incomingUrl.pathname.startsWith("/port/")? applyPanelHtmlNonceCsp(cachedResp): cachedResp;}}82 unmodified linesrespHeaders.set("Cache-Control", cacheControlRule);}const applyPanelSec = isPanelHost && !incomingUrl.pathname.startsWith("/port/");if (applyPanelSec) {applyPanelSecurityHeaders(respHeaders);}12 unmodified lines}}return applyPanelSec ? applyPanelHtmlNonceCsp(finalResp) : finalResp;},};
worker.js+47-7