Inline script triggers CSP error in chrome extension
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 279
- PR merge metrics
- No merged PRs in 30d
Description
When using vite plugin vue devtools to develop a chrome extension page, the extension reported the following error:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost: * http://127.0.0.1: *". Either the 'unsafe-inline' keyword, a hash ('sha256-FhCISuQOxB+ZEYLZlvQzFWx4//lRqFQPUeFSEkdvsT0='), or a nonce ('nonce-...') is required to enable inline execution.
It seems to be triggered by the following code:
function injectVueHookToIframe(iframe) {
if (iframe.__vdevtools__injected) return
try {
iframe.__vdevtools__injected = true
const inject = () => {
try {
iframe.contentWindow.__VUE_DEVTOOLS_IFRAME__ = iframe
const script = iframe.contentDocument.createElement('script')
script.textContent = `;(${detectIframeApp.toString()})(window, true)`
iframe.contentDocument.documentElement.appendChild(script)
script.parentNode.removeChild(script)
} catch (e) {}
}
inject()
iframe.addEventListener('load', () => inject())
} catch (e) {}
}
The problem is:
// This line attempts to inject inline script blocks
script.textContent = `;(${detectIframeApp.toString()})(window, true)`
iframe.contentDocument.documentElement.appendChild(script)
In chrome extension Manifest V3, there is no way to declare inline script as safe, CSP will prevent code injection and throw errors.
Can the injection method of the code be changed to external code?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating injectVueHookToIframe and reproducing the issue in a Chrome extension using Manifest V3 CSP. Trace the inline script creation and determine an external-code injection approach that avoids CSP violations; done means the Vue devtools iframe hook works without the reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- devtools, frontend, security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100