added CSP

This commit is contained in:
Chris Kruining 2024-11-07 09:49:21 +01:00
parent dc30ebb35e
commit 3a79fd4488
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
2 changed files with 39 additions and 17 deletions

View file

@ -3,6 +3,9 @@ import { VitePWA } from 'vite-plugin-pwa'
export default defineConfig({ export default defineConfig({
vite: { vite: {
html: {
cspNonce: 'KAAS_IS_AWESOME',
},
plugins: [ plugins: [
VitePWA({ VitePWA({
mode: 'development', mode: 'development',

View file

@ -4,20 +4,39 @@ import { installIntoGlobal } from "iterator-helpers-polyfill";
installIntoGlobal(); installIntoGlobal();
export default createHandler(() => ( export default createHandler(({ nonce }) => {
<StartServer return (
document={({ assets, children, scripts }) => ( <StartServer
<html lang="en"> document={({ assets, children, scripts }) => {
<head> return (
<meta charset="utf-8" /> <html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" /> <head>
{assets} <meta charset="utf-8" />
</head> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<body> <meta property="csp-nonce" nonce={nonce} />
{children} {assets}
{scripts} </head>
</body> <body>
</html> {children}
)} {scripts}
/> </body>
)); </html>
);
}} />
);
}, event => {
const nonce = crypto.randomUUID();
const base = `'self' 'nonce-${nonce}'`;
const policies = {
default: base,
connect: `${base} ws://localhost:*`,
style: `'self' data: https://fonts.googleapis.com 'unsafe-inline'`,
// style: `${base} data: https://fonts.googleapis.com`,
font: `${base} https://*.gstatic.com`,
} as const;
event.response.headers.append('Content-Security-Policy', Object.entries(policies).map(([p, v]) => `${p}-src ${v}`).join('; '))
return { nonce };
});