Set Content-Security-Policy
This commit is contained in:
1 parent
af0a8c9fee
commit
870d55b44f
7 files changed
+67
-25
No files matched your search
+20
-1
@@ -2,8 +2,27 @@
|
||||
import vue from '@vitejs/plugin-vue';
|
||||
import { defineConfig } from 'vite';
|
||||
|
||||
const DEV_STYLE_SRC = "style-src 'self'";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
plugins: [
|
||||
vue(),
|
||||
// index.html's Content-Security-Policy allows no inline styles, the dev server injects the CSS
|
||||
// through JavaScript. A nonce lets only those style elements through, so the dev server enforces
|
||||
// the same policy as the production build, which links the CSS as files.
|
||||
{
|
||||
name: 'dev-csp',
|
||||
apply: 'serve',
|
||||
config: () => ({ html: { cspNonce: 'dev' } }),
|
||||
transformIndexHtml(html) {
|
||||
if (!html.includes(DEV_STYLE_SRC)) throw new Error(`dev-csp: index.html has no "${DEV_STYLE_SRC}"`);
|
||||
return html.replace(DEV_STYLE_SRC, `${DEV_STYLE_SRC} 'nonce-dev'`);
|
||||
},
|
||||
},
|
||||
],
|
||||
// The dev server would inline small images like gitea.svg as data: URLs, which index.html's
|
||||
// Content-Security-Policy blocks. The production build links them as files either way.
|
||||
build: { assetsInlineLimit: 0 },
|
||||
// Playwright runs e2e.spec.ts and backend/, which Vitest would pick up too.
|
||||
test: { include: ['src/**/*.test.ts'] },
|
||||
});
|
||||
Reference in new issue
Block a user