Skip to content

Commit

Permalink
add csp-nonce meta tag
Browse files Browse the repository at this point in the history
* don't need nonce on scripts, just loading 'self'
* add csp-nonce meta tag
* manual chunk of emotion_sheet to make backend easily able to modify code to inject the meta nonce

see: gregtwallace/certwarden-backend@f127749
  • Loading branch information
gregtwallace committed Oct 31, 2023
1 parent ac50f7d commit 7ea6ec1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>

<meta property="csp-nonce" nonce="{SERVER-CSP-NONCE}" />

<link rel="stylesheet" href="/fonts/roboto.css" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
Expand Down
20 changes: 17 additions & 3 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import react from '@vitejs/plugin-react-swc';

// https://vitejs.dev/config/
export default defineConfig({
build: {
rollupOptions: {
output: {
manualChunks: (id) => {
if (id.includes('node_modules')) {
// if emotion/sheet
if (id.includes('emotion/sheet')) {
return 'emotion_sheet';
}
}
},
},
},
},
plugins: [
react(),
// add preload attribute and nonce place holder
Expand All @@ -15,11 +29,11 @@ export default defineConfig({
// add nonce?
if (
p1 === 'style' ||
p1 === 'script' ||
// p1 === 'script' ||
// if link, only nonce for stylesheet and modulepreload
(p1 === 'link' &&
(p2.includes('rel="stylesheet"') ||
p2.includes('rel="modulepreload"')))
p2.includes('rel="stylesheet"'))
// || p2.includes('rel="modulepreload"')
) {
p2 = `nonce="{SERVER-CSP-NONCE}" ${p2}`;
}
Expand Down

0 comments on commit 7ea6ec1

Please sign in to comment.