Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a content-security-policy in development #2142

Merged
merged 19 commits into from
Apr 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ const previewMetaTag = [
},
]

const headers = Object.fromEntries(
vercelConfig.headers[0].headers.map(({ key, value }) => [key, value])
)
// vercel config is source of truth for headers
const vercelHeaders = vercelConfig.headers[0].headers
const headers = Object.fromEntries(vercelHeaders.map((h) => [h.key, h.value]))
const cspNonce = randomBytes(8).toString('hex')
const cspWithNonce = `${headers['content-security-policy']}; script-src 'nonce-${cspNonce}' 'self'`

// see https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
Expand Down Expand Up @@ -110,19 +111,13 @@ export default defineConfig(({ mode }) => ({
apiMode === 'dogfood' && basicSsl(),
],
html: {
cspNonce:
mode === 'production'
? // don't include a placeholder nonce in production
undefined
: // use a CSP nonce to avoid needing to permit 'unsafe-inline' in dev mode
cspNonce,
// don't include a placeholder nonce in production.
// use a CSP nonce in dev to avoid needing to permit 'unsafe-inline'
cspNonce: mode === 'production' ? undefined : cspNonce,
},
server: {
port: 4000,
headers: {
...headers,
'content-security-policy': `${headers['content-security-policy']}; script-src 'nonce-${cspNonce}' 'self'`,
},
headers: { ...headers, 'content-security-policy': cspWithNonce },
// these only get hit when MSW doesn't intercept the request
proxy: {
'/v1': {
Expand All @@ -141,9 +136,7 @@ export default defineConfig(({ mode }) => ({
},
},
},
preview: {
headers: headers,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, lol, this was me testing to see if prettier or eslint would fix this (it apparently didn't)

preview: { headers },
test: {
environment: 'jsdom',
setupFiles: ['test/unit/setup.ts'],
Expand Down
Loading