问题描述
我有一个Web应用程序,我想在具有不同域的Web应用程序中的iframe中显示。由于我添加了内容安全策略标头,因此我的应用程序拒绝在iframe中显示。我看到我需要添加frame-ancestors选项,但是我看到的所有示例都使用特定的域。如何允许所有域使用它?是“框架祖先*”。足够?谢谢!
解决方法
简要-是的,*
允许除data:
以外的所有iframe来源。
请注意,元标记<meta http-equiv='Content-Security-Policy' content="...">
中的 frame-ancestors is not supported(但是您似乎使用HTTP标头来传递CSP,因此不建议您这样做)。
但是,如果您真的希望允许所有框架祖先-更可靠,则完全不指定 frame-ancestors 指令,因为到目前为止,Mozilla Firefox带有一些错误。
PS:您没有在浏览器控制台中附加错误打印屏幕-可能是iframe被CSP以外的其他原因阻止了?
在显示了CSP详细信息之后更新
<html>
parent page issues CSP: default-src 'self';
since frame-src omitted,it fallback to default-src and result be: frame-src 'self'
<iframe src=''></iframe>
</html>
允许 iframe与加载父页面时使用相同的scheme://host:port
。
“自”的技巧很棘手,因为如果通过HTTP:加载父对象,则通过HTTPS:的iframe将在CSP2-浏览器中被阻止。 CSP3-浏览器do upgrade(请参阅第3段)HTTP:到HTTPS :,所以一切正常。
如果父页面发布了frame-ancestors *
政策,则意味着您允许将其嵌入到iframe中,以访问其他任何网页。
X-Frame-Options HTTP标头提供相同的功能,但是如果发出了frame-ancestor,则为it's overridden。
- frame-ancestor指令不影响嵌入到发布此CSP的页面中的
<iframe>
。它会影响该页面的嵌入位置。 - 但是
<iframe>
可以发布带有规则frame-ancestors domain1.com domain2.com
的自己的CSP,以将其嵌入其他网页。
这就是它的工作方式。您可以使用test of frame-ancestors来阐明不同<iframe src=/srcdoc=
的详细信息。
因此,如果您从自己的域/子域中嵌入iframe,则使用起来更安全:
frame-ancestors 'self';
或者如果您使用子域:
frame-ancestors http://example.com https://example.com http://*.example.com https://*.example.com;