我的情况是广告在 iframe 中有 CSP 定义。在 chrome 中没有问题,但在 firefox 中,加载广告后 CSP 会影响整个页面,我无法加载任何其他资源。
你可以看到这个例子的问题:
<html>
<head>
<script>
function getScript(url) {
let tag = document.createElement('script');
tag.src = url;
tag.onload = () => document.getElementById('console').innerHTML += url + " loaded<br>";
document.body.appendChild(tag);
}
function getFromCdnFirst() {
getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js');
}
function getFromCdnSecond() {
getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js');
}
function getIframeWithCSP() {
var doc = document.getElementById('iframe').contentWindow.document;
doc.open();
doc.write('<html><head><meta http-equiv="Content-Security-Policy" content="script-src https://cdn.ampproject.org/;object-src '+"'none';child-src blob:;frame-src 'none'" + '"></head><body>Test</body>');
doc.close();
}
</script>
</head>
<body>
<iframe id="iframe"></iframe><br>
<button onClick="getFromCdnFirst()">Get script</button><br>
<button onClick="getIframeWithCSP()">Get CSP</button><br>
<button onClick="getFromCdnSecond()">Get script</button><br>
<div id="console">
</div>
</body>
它也可以在这里找到:https://jsfiddle.net/54Luhjan/1/
点击第一个按钮后,js 加载,第二个链接将 CSP 插入 iframe,之后脚本无法加载 - CSP 阻止它。
您知道我该怎么做才能防止外部 CSP 破坏我的页面吗?
这是 Firefox 的错误吗?
最佳答案
我跑了 mozregression在 Firefox 的 beta 版和夜间版上。似乎 jsfiddle 从包含 Mozilla 的 bug 965637 补丁的构建开始,在第三次单击按钮时成功加载了内容。 ,这landed in the trunk on 2019-05-21 (21 May 2019) .
所以这是 Firefox 67 中的一个错误(可能在更早的版本中也是如此),但它已经被修复,修复将包含在 Firefox 69 release scheduled for 2019-09-03 (3 September 2019) 中。 .
不幸的是,修复没有及时进入 Firefox 68 的分支切割,因此该错误仍将存在于 Firefox 68 release, scheduled for 2019-07-09 (9 July 2019) 中。 .
关于html - iframe 中的内容安全策略会影响 Firefox 上的整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575716/