Skip to content

Commit 46431b9

Browse files
committed
update
1 parent b6ad07a commit 46431b9

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

examples/with-strict-csp/src/middleware.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ export default createMiddleware({
77
onRequest: event => {
88
const nonce = randomBytes(16).toString("base64");
99

10-
event.locals.nonce = nonce;
10+
if (isProd) {
11+
event.locals.nonce = nonce;
12+
}
1113

1214
// Notes:
13-
// 1. SolidStart uses `eval` for data serialization, which may require you to add 'unsafe-eval' to your CSP.
15+
// 1. SolidStart uses `eval` for data serialization, which may require you to include the 'unsafe-eval' directive in your CSP.
1416
// For more information, see: https://github.com/solidjs/solid-start/issues/1825
15-
// 2. In development, Vite inlines small CSS files to enhance performance, so you will need to include 'unsafe-inline' in development mode.
16-
// 3. During the build process, Vite inlines small assets as data URLs. Therefore, it's necessary to add data: to the relevant directives (e.g., img-src, font-src, etc.)
17+
// 2. In development, Vite inlines small CSS files to improve performance, so you'll need to include the 'unsafe-inline' directive in development.
18+
// 3. During the build process, Vite inlines small assets as data URLs.
19+
// Therefore, it's necessary to add `data:` to the relevant directives (e.g., img-src, font-src, etc.).
1720
// For more details, see: https://vite.dev/config/build-options.html#build-assetsinlinelimit
1821
const csp = `
1922
default-src 'self';
20-
script-src 'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval';
21-
style-src 'self' ${isProd ? "" : "'unsafe-inline'"};
22-
img-src 'self' ${isProd ? "" : "data:"};
23+
script-src ${
24+
isProd
25+
? // Note: The `https:` and `'unsafe-inline'` directives do not reduce the effectiveness of the CSP.
26+
// They are only fallbacks for older browsers that don't support `'strict-dynamic'`.
27+
`'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval' https: 'unsafe-inline'`
28+
: "'self' 'unsafe-inline' 'unsafe-eval' https: http:"
29+
};
30+
style-src ${isProd ? `'nonce-${nonce}'` : "'self' 'unsafe-inline'"};
31+
img-src 'self' data:;
2332
object-src 'none';
2433
base-uri 'none';
2534
frame-ancestors 'none';

0 commit comments

Comments
 (0)