Skip to content

Commit

Permalink
Add Content-Security-Policy (#1229)
Browse files Browse the repository at this point in the history
The mode-watcher bug causing a flash of light mode has been fixed so we
can now add a CSP.

Still need to add an additional directive due to a Svelte bug
  • Loading branch information
SapiensAnatis authored Jan 18, 2025
1 parent 8073fbc commit dd64dff
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Website/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const config: PlaywrightTestConfig = {
timeout: 120000
},
use: {
baseURL: 'http://localhost:3001'
baseURL: 'http://localhost:3001',
bypassCSP: true
},
updateSnapshots: process.env.UPDATE_SNAPSHOTS ? 'all' : 'missing',
ignoreSnapshots: !process.env.CI,
Expand Down
3 changes: 3 additions & 0 deletions Website/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<meta property="og:image" content="%sveltekit.assets%/favicon.ico" />
<title>Dawnshard</title>
%sveltekit.head%
<script nonce="%sveltekit.nonce%">
%modewatcher.snippet%
</script>
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
Expand Down
18 changes: 17 additions & 1 deletion Website/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { randomUUID } from 'node:crypto';

import type { Handle, HandleFetch, HandleServerError } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import { generateSetInitialModeExpression } from 'mode-watcher';

import { env } from '$env/dynamic/private';
import { PUBLIC_ENABLE_MSW } from '$env/static/public';
Expand Down Expand Up @@ -50,12 +52,24 @@ export const handleFetch: HandleFetch = ({ request, event, fetch }) => {
return fetch(request);
};

export const handle: Handle = ({ event, resolve }) => {
const handleHeadScript: Handle = ({ event, resolve }) => {
return resolve(event, {
transformPageChunk: ({ html }) => {
return html.replace('%modewatcher.snippet%', generateSetInitialModeExpression({}));
}
});
};

const handleLogger: Handle = ({ event, resolve }) => {
event.locals.logger = createLogger({
requestPath: new URL(event.request.url).pathname,
requestId: randomUUID()
});

return resolve(event);
};

const handleAuth: Handle = ({ event, resolve }) => {
const idToken = event.cookies.get(Cookies.IdToken);

if (!idToken) {
Expand All @@ -77,6 +91,8 @@ export const handle: Handle = ({ event, resolve }) => {
return resolve(event);
};

export const handle = sequence(handleHeadScript, handleLogger, handleAuth);

export const handleError: HandleServerError = ({ error, event, status, message }) => {
event.locals.logger.error({ error, status, message }, 'Unhandled error occurred: {message}');
};
2 changes: 1 addition & 1 deletion Website/src/routes/(main)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<meta property="og:url" content={data.urlOrigin} />
</svelte:head>

<ModeWatcher />
<ModeWatcher disableHeadScriptInjection />
<Header hasValidJwt={data.hasValidJwt} />
<SideNav hasValidJwt={data.hasValidJwt} />
<Toaster richColors />
Expand Down
9 changes: 9 additions & 0 deletions Website/src/routes/csp/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async ({ locals, request }) => {
const violation = await request.json();

locals.logger.error({ violation }, 'CSP violation reported: {violation}');

return new Response(null, { status: 200 });
};
22 changes: 16 additions & 6 deletions Website/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const scriptCsp = [
'self',
// https://github.com/sveltejs/svelte/issues/14014
'unsafe-hashes',
'sha256-7dQwUgLau1NFCCGjfn9FsYptB6ZtWxJin6VohGIu20I='
];

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
Expand All @@ -13,13 +20,16 @@ const config = {
$shadcn: './src/lib/shadcn',
$static: './static',
$main: './src/routes/(main)'
},
csp: {
directives: {
'script-src': scriptCsp
},
reportOnly: {
'script-src': scriptCsp,
'report-uri': ['/csp']
}
}
// Blocked by https://github.com/svecosystem/mode-watcher/issues/92
// csp: {
// directives: {
// 'script-src': ['self']
// }
// }
}
};

Expand Down

0 comments on commit dd64dff

Please sign in to comment.