【发布时间】:2016-08-04 14:50:48
【问题描述】:
我已将扩展程序的 CSP 设置为允许从 localhost 加载(理论上):
"content_security_policy": "script-src 'self' 'unsafe-eval' https://localhost:* ws://localhost:* https://*.mysite.com; object-src 'self'",
我有一个web_accessible_resource 尝试加载和执行远程脚本:
<html>
<head>
<title>Sign in</title>
</head>
<body>
<script src="./auth.js"></script>
</body>
</html>
auth.js的(简化)内容:
(function(doc, script) {
script = doc.createElement('script')
script.type = 'text/javascript'
script.async = true
script.src = 'https://localhost:3333/remote-server/auth.js'
doc.getElementsByTagName('head')[0].appendChild(script)
}(document))
但是,我收到以下错误:
Refused to load the script 'https://localhost:3333/remote-server/auth.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'".
这似乎不尊重扩展程序的 CSP。我试过直接在 HTML 中添加下面的 header,但仍然没有任何乐趣。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-eval' https://localhost:* ws://localhost:* https://*.mysite.com">
我需要在其他地方注明 CSP 吗?
更新
更改资源的 HTML 以直接加载远程脚本也没有解决问题:
<html>
<head>
<title>Sign in</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval' https://localhost:* ws://localhost:* https://*.mysite.com">
</head>
<body>
<script src="https://localhost:3333/remote-server/auth.js"></script>
</body>
</html>
仍然导致:
Refused to load the script 'https://localhost:3333/remote-server/auth.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'".
仍然没有引用<meta>标签的内容
更新 2
页面加载方式
chrome.windows.create({
url: 'chrome-extension://my-extension/auth.html',
type: 'popup',
height: 680,
width: 500
}, (windw) => console.log(windw))
【问题讨论】:
-
那么问题仍然存在,那么我如何允许远程加载
web_accessible_resources 呢?如果扩展程序的 CSP 不适用,怎么办? -
你发现了吗?在主网页上禁用 CSP 的最佳方法是什么?我找到了这个来源,但我的尝试失败了。这似乎只在整个页面重新加载后才有效。谢谢! github.com/PhilGrayson/chrome-csp-disable/blob/master/…
标签: javascript google-chrome-extension content-security-policy