-
Notifications
You must be signed in to change notification settings - Fork 122
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
Vite 5.2 meta tag to support new csp nonce tagging #444
base: main
Are you sure you want to change the base?
Conversation
Hi Tys! Thanks for adding this feature. It's unfortunate that Rails and Vite use different formats for this. I wonder if Vite's helper could be modified to support both, as it's possible that other frameworks use the same format as Rails. |
I agree, it would be nice if Vite and Rails used the same format for the CSP meta tag. One approach would be to modify this look up to be a bit more liberal and look for the csp nonce meta tag via However I think it is important to note that Rails is setting the nonce value in the Vite should stay firm in only accessing the csp nonce via the |
The issue has bubbled through the HackerOne process and now exists as a Rails issue: rails/rails#51580 Once fixed this would address the use of the Alternatively, it may make sense to open a PR against Vite to look for csp-nonce meta tags with either the name or property set. I'll open a PR over there once the rails issue is addressed. I think it makes sense to hold off on fixing it here until those issues work through their respective projects. |
Rails PR address the |
d55cc92
to
ebe50b8
Compare
Description 📖
Prior to Vite 5.2, it was not possible to define a strict CSP (Content Security Policy) for
style-src-elem
&script-src-elem
directives as Vite would dynamically generate and insert these tags into the head but did not add a nonce value needed by the stricter CSP. This can be useful if you're trying to align your development and production CSPs to be similar in scope, hopefully catching early CSP issues.In Vite 5.2 support for client side nonce tagging of assets was added. Generated script & style tags would get tagged with a nonce if a properly crafted
meta
tag is detected in the document. Unfortunately rails default csp_meta_tag is not the format Vite expects.Rails generates:
Vite expects:
This PR adds a new helper
vite_csp_meta_tag
which generates a meta tag which Vite expects, allowing the dynamically added script and style tags to be properly have a nonce set. This in turn allows for a more strict CSP for those directives.See:
https://github.com/vitejs/vite/pull/16052/files
https://vitejs.dev/guide/features.html#content-security-policy-csp
Background 📜
Using Rails and Vite prior to 5.2 did not allow a strict CSP.
The Fix 🔨
Make use Vite 5.2's new CSP tagging support with a properly crafted csp-nonce meta tag.