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

Content Security Policy inside iframe affects whole page on 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 加载,第二个 link 将 CSP 插入 iframe,之后无法加载脚本 - CSP 阻止它。

你知道我该怎么做才能防止外部 CSP 破坏我的页面吗?

这是 Firefox 的错误吗?

我运行mozregression over beta and nightly builds of Firefox. It seems like the jsfiddle successfully loads things on the third button click starting with builds that contain the patches from Mozilla's bug 965637, which 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 的 b运行ch 版本,因此该错误仍将存在于 Firefox 68 release, scheduled for 2019-07-09 (9 July 2019).