File tree 3 files changed +31
-0
lines changed
packages/vite/src/node/plugins
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -1189,6 +1189,13 @@ export function injectNonceAttributeTagHook(
1189
1189
parseRelAttr ( attr . value ) . some ( ( a ) => processRelType . has ( a ) ) ,
1190
1190
) )
1191
1191
) {
1192
+ const alreadyContainsNonce = node . attrs . some (
1193
+ ( { name } ) => name === 'nonce' ,
1194
+ )
1195
+ if ( alreadyContainsNonce ) {
1196
+ return
1197
+ }
1198
+
1192
1199
// if the closing of the start tag includes a `/`, the offset should be 2 so the nonce
1193
1200
// is appended prior to the `/`
1194
1201
const appendOffset =
Original file line number Diff line number Diff line change @@ -27,6 +27,20 @@ test('dynamic js', async () => {
27
27
)
28
28
} )
29
29
30
+ test ( 'inline js' , async ( ) => {
31
+ await expectWithRetry ( ( ) => page . textContent ( '.inline-js' ) ) . toBe (
32
+ 'inline-js: ok' ,
33
+ )
34
+ } )
35
+
36
+ test ( 'nonce attributes are not repeated' , async ( ) => {
37
+ const htmlSource = await page . content ( )
38
+ expect ( htmlSource ) . not . toContain ( / n o n c e = " " [ ^ > ] * n o n c e = " " / )
39
+ await expectWithRetry ( ( ) => page . textContent ( '.double-nonce-js' ) ) . toBe (
40
+ 'double-nonce-js: ok' ,
41
+ )
42
+ } )
43
+
30
44
test ( 'meta[property=csp-nonce] is injected' , async ( ) => {
31
45
const meta = await page . $ ( 'meta[property=csp-nonce]' )
32
46
expect ( await ( await meta . getProperty ( 'nonce' ) ) . jsonValue ( ) ) . not . toBe ( '' )
Original file line number Diff line number Diff line change 11
11
< p class ="dynamic "> dynamic</ p >
12
12
< p class ="js "> js: error</ p >
13
13
< p class ="dynamic-js "> dynamic-js: error</ p >
14
+ < p class ="inline-js "> inline-js: error</ p >
15
+ < p class ="double-nonce-js "> double-nonce-js: error</ p >
16
+ < script >
17
+ document . querySelector ( '.inline-js' ) . textContent = 'inline-js: ok'
18
+ </ script >
19
+ < script nonce ="#$NONCE$# ">
20
+ // this test case is to ensure that the nonce isn't being
21
+ // double-applied if an existing attribute is present.
22
+ document . querySelector ( '.double-nonce-js' ) . textContent = 'double-nonce-js: ok'
23
+ </ script >
You can’t perform that action at this time.
0 commit comments