首页 > 解决方案 > Chrome 扩展与网站的 Content-Security-Policy “object-src 'self' blob:”

问题描述

我正在编写一个 chrome 扩展,它利用<object>内容脚本中的一个从我的中央服务器加载内容以显示给用户。data在某些网站上,我在 上设置属性的尝试<object>被阻止,出现以下错误:

Refused to load plugin data from 'https://redacted.com/?url=https%3A%2F%2Fwww.mozilla.org%2Fen-US%2Ffirefox%2F' because it violates the following Content Security Policy directive: "default-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com". Note that 'object-src' was not explicitly set, so 'default-src' is used as a fallback.

我的扩展在 stackoverflow.com 上没有问题!请注意,有时它是object-src 'self' blob:相反的。

从我的扩展程序尝试加载的网站的响应标头中,很明显这就是阻止我的扩展程序能够执行其工作的原因(stackoverflow.com 中缺少这些标头,这就是它起作用的原因):

content-security-policy: default-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com; child-src www.googletagmanager.com www.google-analytics.com www.youtube-nocookie.com trackertest.org www.surveygizmo.com accounts.firefox.com accounts.firefox.com.cn www.youtube.com; script-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com 'unsafe-inline' 'unsafe-eval' www.googletagmanager.com www.google-analytics.com tagmanager.google.com www.youtube.com s.ytimg.com; style-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com 'unsafe-inline'; img-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com data: mozilla.org www.googletagmanager.com www.google-analytics.com adservice.google.com adservice.google.de adservice.google.dk creativecommons.org ad.doubleclick.net; frame-src www.googletagmanager.com www.google-analytics.com www.youtube-nocookie.com trackertest.org www.surveygizmo.com accounts.firefox.com accounts.firefox.com.cn www.youtube.com; connect-src 'self' *.mozilla.net *.mozilla.org *.mozilla.com www.googletagmanager.com www.google-analytics.com https://accounts.firefox.com/ https://accounts.firefox.com.cn/

有没有解决的办法?Shadow DOM 会是一种选择吗?我已经尝试添加content_security_policy到我的manifest.json,但这永远不会起作用,因为浏览器尊重父网站在其标题中响应的内容。

提前致谢。

标签: javascriptgoogle-chrome-extensioncontent-security-policy

解决方案


推荐阅读