-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Describe the bug
I am having the same issue as here, but the solution isn't working for me.
I'm using Electron + Vite and am trying to set a strict CSP (one with no unsafe-inline
). Since I am using Electron and am not fetching my resources from a server, the nonce specified for cspNonce
is generated right above the config. I then just use a plugin to inject the nonce into the index.html. This works fine in dev when the entire process is ran every time I run the app, but in production the process is only ran when packaging the app and never again. This is problematic as the nonce will no longer be regenerated, defeating the whole purpose. It would be nice if a strict CSP was supported without having to use a nonce.
// electron.vite.config.ts
let nonce = crypto.randomBytes(16).toString('base64');
const transformHtmlPlugin = data => ({
name: 'transform-html',
transformIndexHtml: {
transform(html)
{
return html.replace(
/<%=\s*(\w+)\s*%>/gi,
(match, p1) => data[p1] || ''
);
}
}
});
export default defineConfig({
renderer: {
plugins: [
transformHtmlPlugin({ nonce: nonce })
],
html: {
cspNonce: nonce
},
},
})
// index.html
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'nonce-<%= nonce %>'" />
Reproduction
https://github.com/TylerWanta/vite-csp-issue
Steps to reproduce
This will be an issue with any vite build with a strict CSP and no cspNonce
specified.
Clone the repo provided
cd vite-csp-issue
npm run dev
You can see the issue in the console but clicking CTRL + Shift + i
System Info
System:
OS: Windows 11 10.0.22631
CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900KF
Memory: 18.28 GB / 31.85 GB
Binaries:
Node: 21.5.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Chromium (123.0.2420.97)
Internet Explorer: 11.0.22621.1
npmPackages:
@vitejs/plugin-vue: ^4.3.1 => 4.6.2
vite: ^5.2.11 => 5.2.11
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.