我遇到的情况是,广告在iframe中具有CSP定义.在chrome中没有问题,但是在firefox中,加载广告后,CSP影响了整个页面,并且我无法加载任何其他资源.
您可以通过以下示例看到问题:
@H_403_8@<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错误吗?
最佳答案
我在Beta和每晚生成的Firefox上运行了mozregression.似乎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)中.