Skip to content

Commit 8e54bbd

Browse files
feat: write cspNonce to style tags (#16419)
1 parent af72eab commit 8e54bbd

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

docs/guide/features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ To deploy CSP, certain directives or configs must be set due to Vite's internals
699699

700700
### [`'nonce-{RANDOM}'`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#nonce-base64-value)
701701

702-
When [`html.cspNonce`](/config/shared-options#html-cspnonce) is set, Vite adds a nonce attribute with the specified value to the output script tag and link tag for stylesheets. Note that Vite will not add a nonce attribute to other tags, such as `<style>`. Additionally, when this option is set, Vite will inject a meta tag (`<meta property="csp-nonce" nonce="PLACEHOLDER" />`).
702+
When [`html.cspNonce`](/config/shared-options#html-cspnonce) is set, Vite adds a nonce attribute with the specified value to any `<script>` and `<style>` tags, as well as `<link>` tags for stylesheets and module preloading. Additionally, when this option is set, Vite will inject a meta tag (`<meta property="csp-nonce" nonce="PLACEHOLDER" />`).
703703

704704
The nonce value of a meta tag with `property="csp-nonce"` will be used by Vite whenever necessary during both dev and after build.
705705

packages/vite/src/node/plugins/html.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ export function injectNonceAttributeTagHook(
11841184

11851185
if (
11861186
nodeName === 'script' ||
1187+
nodeName === 'style' ||
11871188
(nodeName === 'link' &&
11881189
attrs.some(
11891190
(attr) =>

playground/csp/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<link rel="stylesheet" href="./linked.css" />
2-
<style nonce="#$NONCE$#">
2+
<style>
33
.inline {
44
color: green;
55
}

0 commit comments

Comments
 (0)