Skip to content

Commit ef08fd6

Browse files
authored
fix(vite): use TrustedScriptURL for overlay injection under Trusted Types CSP (#1094)
1 parent 3b0bd01 commit ef08fd6

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

packages/vite/src/overlay.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,20 @@ link.href = `${overlayDir}/devtools-overlay.css`
4141

4242
// create script
4343
const script = document.createElement('script')
44-
script.src = `${overlayDir}/devtools-overlay.mjs`
44+
const scriptUrl = `${overlayDir}/devtools-overlay.mjs`
45+
// Under a `require-trusted-types-for 'script'` CSP, assigning a string to
46+
// `script.src` is blocked. Wrap the URL in a TrustedScriptURL via a named
47+
// policy so apps can opt-in by allowing `vue-devtools` in their CSP's
48+
// `trusted-types` directive.
49+
if (typeof window !== 'undefined' && window.trustedTypes && typeof window.trustedTypes.createPolicy === 'function') {
50+
const policy = window.trustedTypes.createPolicy('vue-devtools', {
51+
createScriptURL: input => input,
52+
})
53+
script.src = policy.createScriptURL(scriptUrl)
54+
}
55+
else {
56+
script.src = scriptUrl
57+
}
4558
script.type = 'module'
4659

4760
// append to head

0 commit comments

Comments
 (0)