html - iframe 中的内容安全策略会影响 Firefox 上的整个页面

标签 html security firefox content-security-policy

我的情况是广告在 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/

相关文章:

javascript - 使用 .show ("slow"时不会淡入)?

mysql - 将 MySQL 密码查询移植到 Postgres

security - 用户登录后如何使用 JSF 2 限制对某些页面的访问?

Javascript 点击 href 链接

javascript - AJAX 成功不适用于 IE 和 Firefox

滚动时 JavaScript 数字发生变化

html - 溢出 :Visible outside the container

javascript - 当浏览器调整大小时,图像上的文本不起作用

security - 考虑摩尔定律的密码强度检查器?

python - 我在哪里可以从 Firefox 获取有关 :support 的原始信息