Skip to content

Commit 3c85c6b

Browse files
gregtwallacesapphi-red
andauthoredMar 31, 2024··
fix: csp nonce injection when no closing tag (#16281) (#16282)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent f1e67b6 commit 3c85c6b

File tree

1 file changed

+6
-1
lines changed
  • packages/vite/src/node/plugins

1 file changed

+6
-1
lines changed
 

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,13 @@ export function injectNonceAttributeTagHook(
11891189
parseRelAttr(attr.value).some((a) => processRelType.has(a)),
11901190
))
11911191
) {
1192+
// if the closing of the start tag includes a `/`, the offset should be 2 so the nonce
1193+
// is appended prior to the `/`
1194+
const appendOffset =
1195+
html[node.sourceCodeLocation!.startTag!.endOffset - 2] === '/' ? 2 : 1
1196+
11921197
s.appendRight(
1193-
node.sourceCodeLocation!.startTag!.endOffset - 1,
1198+
node.sourceCodeLocation!.startTag!.endOffset - appendOffset,
11941199
` nonce="${nonce}"`,
11951200
)
11961201
}

0 commit comments

Comments
 (0)
Please sign in to comment.