【问题标题】:Content Security Policy frame-ancestors incorrectly blocks on safari from filesystem内容安全策略框架祖先错误地阻止来自文件系统的 safari
【发布时间】:2021-01-21 23:45:45
【问题描述】:

当我注意到我的应用程序中使用的 iframe 没有在 ios 上正确加载时,我正在为 android 和 ios 开发一个 cordova 应用程序。事实证明,尽管从 web 服务器正确设置了 frame-ancestors HTTP 标头,但如果通过文件系统打开,safari 将错误地拒绝加载 iframe。

Refused to load http://to-delete-test.azurewebsites.net/ because it does not appear in the frame-ancestors directive of the Content Security Policy.

测试页面的 CSP Header 是:

Content-Security-Policy: frame-ancestors * data: blob: filesystem: about: ws: wss:

显示此错误的示例 HTML 文件(必须从文件系统加载):

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        * {
            margin: 0; padding: 0; box-sizing: border-box;
        }
        iframe {
            height: 100vh; width: 100%;
        }
    </style>
</head>
<body>
    <iframe src="http://to-delete-test.azurewebsites.net"></iframe>
</body>
</html>

有没有办法可以修改页眉以允许通过 iframe 加载我的网站?

【问题讨论】:

  • 我不知道“通过文件系统打开”是什么意思,但我知道frame-ancestors 指令肯定是does not support data: blob:ws:wss: 方案源。看起来 about:filesystem: 也不支持。无论如何,您的 frame-ancestors 应该没有任何限制。检查 Dev Tool -> Network -> Response Header 你实际拥有什么 CSP。
  • @granty 感谢您的回复。 “通过文件系统打开”是指打开从设备文件系统提供的 html 文件,因此 URL 类似于file:///path/to/file/index.html。在 iOS 或 OSX 上的 safari 中,如果有 frame-ancestors CSP 标头,则无论标头的值是什么,即使包含 *,帧似乎都不会加载。 Cordova 应用程序从其文件系统加载网页,因此似乎无法安全地允许 Cordova 应用程序从框架加载,同时还限制其他站点在框架中加载页面。

标签: cordova safari content-security-policy


【解决方案1】:

在 cmets 中提供更多信息后,情况变得清晰。 由于安全原因,无论 CSP 为何,都禁止在嵌套浏览上下文(iframe、对象、嵌入)中打开文件:- URL,如 file:///path/to/file/index.html&lt;a href='file:///path/to/file/index.html'&gt; 也禁止导航。

您无法通过使用 CSP 来解决此限制。此外,frame-ancestors 指令不支持非网络方案,因为它失去了意义。

显示的控制台消息看起来也属于 Safari,这有点误导。 Chrome 应该显示真正的违规原因,例如:拒绝加载 iframe file:///path/to/file/index.html,因为它违反了以下内容安全政策指令:...。

注意。 Chrome 浏览器允许在加载图像/样式/脚本等时使用file:/// 方案访问本地文件系统:

<img src='file:///c:/img.png'>
<script src='file:///c:/scripr.js'></script>
<link rel='stylesheet' href='file:///c:/style.css'>

甚至支持file: scheme in CSP
但 AFAIK 浏览器扩展不应使用通过 file:///-URL 的直接访问。扩展必须使用自己打包的资源或File API

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2018-06-05
    • 2019-05-09
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多