html - iframe 中的内容安全策略会影响 Firefox 上的整个页面
问题描述
我有一种情况,广告在 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 的错误吗?
解决方案
我在Firefox的 beta 和 nightly 版本上运行mozregression 。似乎 jsfiddle 在第三次单击按钮时成功加载了内容,从包含 Mozilla错误 965637的补丁的构建开始,该补丁于 2019-05-21 (2019 年 5 月 21 日) 登陆主干。
所以这是 Firefox 67 中的一个错误(也可能在早期版本中),但它已经被修复,修复将包含在计划于 2019 年 9 月 3 日(2019 年 9 月 3 日)发布的 Firefox 69 中。
但不幸的是,该修复未能及时进入 Firefox 68 的分支削减,因此该错误仍将存在于计划于 2019 年 7 月 9 日(2019 年 7 月 9 日)发布的 Firefox 68 版本中。
推荐阅读
- python - 无法将容器中的 python 标准输出日志获取到“oc 日志”
- javascript - 无需额外的 ajax 请求即可将 laravel 数据转换为 vuex 状态?
- if-statement - 我应该将方法包装在 if 语句中还是将 if 语句放入方法本身?
- mysql - MySql 时差,意外行为
- python - 尝试使用 Python 发布请求时出现错误 405
- flutter - 如何使容器的填充可以点击?
- mysql - 在 mySQL 中从一个生成多行
- python-3.x - 如何将 s3 图像响应读取为对象
- scala - Spark - 拆分后如何将新列附加到原始数据帧?
- sql - 如何在 SQL 中设置 auto_increment 的初始值