Inline script triggers CSP error in chrome extension

未关闭
#945 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript

调研方向

首先定位 injectVueHookToIframe,并在使用 Manifest V3 CSP 的 Chrome 扩展中复现该问题。跟踪内联脚本的创建过程,并确定一种可避免 CSP 违规的外部代码注入方案;当 Vue devtools iframe hook 在没有报告的错误的情况下正常工作时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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?

主要语言
TypeScript
星标
2.9k
派生
291
平均合并
4 天 6 分钟
30 天内合并 PR
27

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vuejs/devtools 的其他 Issue

查看 vuejs/devtools 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。