From 2c7ce50d3b8684e41a36caaf5942420278f97466 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Thu, 23 Jan 2025 13:22:22 +0000 Subject: [PATCH] add a straightforward guard to turn this off if we need to Signed-off-by: John Cowen --- packages/kuma-gui/vite.plugins.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/kuma-gui/vite.plugins.ts b/packages/kuma-gui/vite.plugins.ts index 39f0cf9c7..eb8e9e1a7 100644 --- a/packages/kuma-gui/vite.plugins.ts +++ b/packages/kuma-gui/vite.plugins.ts @@ -38,7 +38,11 @@ export const kumaIndexHtmlVars = (): Plugin => { transformIndexHtml: (template) => interpolate(template, htmlVars), } } -const server = (template: string = './index.html', vars: Partial = {}) => async (server: PreviewServer | ViteDevServer) => { +const server = ( + template: string = './index.html', + vars: Partial = {}, + csp: boolean = true, +) => async (server: PreviewServer | ViteDevServer) => { server.middlewares.use('/', async (req, res, next) => { const url = req.originalUrl || '' const baseGuiPath = vars.baseGuiPath || '/gui' @@ -67,16 +71,19 @@ const server = (template: string = './index.html', vars: Partial = }).filter(([_, value]) => typeof value !== 'undefined')), } satisfies KumaHtmlVars, ) - res.setHeader('Content-Security-Policy', [ - "default-src 'self'", - "script-src 'self'", - "script-src-elem 'self'", - "img-src 'self' data: ", - "style-src 'self' 'unsafe-inline'", - // in production connect-src would use kuma's environment variable for - // setting the location of the HTTP API (or just use the default) - "connect-src 'self' localhost:5681 https://kuma.io", - ].join(';')) + if (csp) { + res.setHeader('Content-Security-Policy', [ + "default-src 'self'", + "script-src 'self'", + "script-src-elem 'self'", + "img-src 'self' data: ", + "style-src 'self' 'unsafe-inline'", + // in production connect-src would use kuma's environment variable for + // setting the location of the HTTP API (or just use the default) + "connect-src 'self' localhost:5681 https://kuma.io", + ].join(';')) + } + res.end(body) } else { next()