File tree 3 files changed +38
-9
lines changed
packages/vite/src/node/plugins
3 files changed +38
-9
lines changed Original file line number Diff line number Diff line change @@ -1180,24 +1180,29 @@ export function injectNonceAttributeTagHook(
1180
1180
return
1181
1181
}
1182
1182
1183
+ const { nodeName, attrs, sourceCodeLocation } = node
1184
+
1183
1185
if (
1184
- node . nodeName === 'script' ||
1185
- ( node . nodeName === 'link' &&
1186
- node . attrs . some (
1186
+ nodeName === 'script' ||
1187
+ ( nodeName === 'link' &&
1188
+ attrs . some (
1187
1189
( attr ) =>
1188
1190
attr . name === 'rel' &&
1189
1191
parseRelAttr ( attr . value ) . some ( ( a ) => processRelType . has ( a ) ) ,
1190
1192
) )
1191
1193
) {
1194
+ // If we already have a nonce attribute, we don't need to add another one
1195
+ if ( attrs . some ( ( { name } ) => name === 'nonce' ) ) {
1196
+ return
1197
+ }
1198
+
1199
+ const startTagEndOffset = sourceCodeLocation ! . startTag ! . endOffset
1200
+
1192
1201
// if the closing of the start tag includes a `/`, the offset should be 2 so the nonce
1193
1202
// is appended prior to the `/`
1194
- const appendOffset =
1195
- html [ node . sourceCodeLocation ! . startTag ! . endOffset - 2 ] === '/' ? 2 : 1
1203
+ const appendOffset = html [ startTagEndOffset - 2 ] === '/' ? 2 : 1
1196
1204
1197
- s . appendRight (
1198
- node . sourceCodeLocation ! . startTag ! . endOffset - appendOffset ,
1199
- ` nonce="${ nonce } "` ,
1200
- )
1205
+ s . appendRight ( startTagEndOffset - appendOffset , ` nonce="${ nonce } "` )
1201
1206
}
1202
1207
} )
1203
1208
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