Sco*_*son 2 iframe geolocation content-security-policy feature-policy
我有一个 iframe 标签,其中 src 是不同服务器上的另一个网页。我有能力修改两个站点的标题。在开始实施控制安全策略之前,我能够单击 iframe 内的按钮并检索 GPS 坐标。我相信控制安全策略会阻止我的父站点运行 Geolocation API。
父站点的代码:
<customHeaders>
<add name="Content-Security-Policy" value="frame-src 'self' https://MyChildSite.com" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
<html>
<iframe src="https://MyChildSite.com" allow="geolocation"></iframe>
</html>
Run Code Online (Sandbox Code Playgroud)
子站点的代码:
<customHeaders>
<add name="Content-Security-Policy" value="frame-src 'self' https://MyParentSite.com" />
<add name="Feature-Policy" value="geolocation 'self' https://MyParentSite.com" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
<html>
<button onclick="getCoordinates()">Get GPS</button>
...list some stuff
</html>
Run Code Online (Sandbox Code Playgroud)
当我通过父站点单击子站点上的按钮时,我没有从坐标中得到我期望的响应。这个问题有方法解决吗?
,--------------------- parent https://MyParentSite.com ------------------------,
|Content-Security-Policy: frame-src 'self' https://MyChildSite.com |
| * aboved CSP do allow <iframe src="https://MyChildSite.com" |
| |
| |
| <iframe src="https://MyChildSite.com" allow="geolocation"> |
| |
| ,-------------------- nested https://MyChildSite.com --------------------, |
| |Content-Security-Policy: frame-src 'self' https://MyChildSite.com | |
| | 1. aboved CSP do nothing, it will apply to subnested iframes only | |
| | | |
| | 2. allow="geolocation" -> allow="geolocation https://MyChildSite.com" | |
| | which is EQUAL to: | |
| | Feature-Policy: geolocation https://MyChildSite.com | |
| | | |
| | Therefore header: | |
| | | |
| |Feature-Policy: geolocation 'self' https://MyParentSite.com | |
| | will failed to allow https://MyParentSite.com, iframe can not extend | |
| | permissions, given by parent document, see para 2. above. | |
| | As result within iframe you will have only: | |
| | Feature-Policy: geolocation https://MyChildSite.com | |
| | | |
| |________________________________________________________________________| |
| |
| </iframe> |
!______________________________________________________________________________|
Run Code Online (Sandbox Code Playgroud)
为什么allow="geolocation"->allow="geolocation https://MyChildSite.com请参阅指令中的 allow= 属性指定没有键将从src=属性中获取来源。
将功能策略权限传递到嵌套浏览上下文中有一些细节。iframe 不能委托自己(或子嵌套的 iframe)比父文档授予的权限更多。
如果您有一个在 iframe 中运行的脚本,您可以使用featurePolicy.getAllowlistForFeature接口来获取所有允许来源的列表并查看发生了什么。
您的问题与内容安全策略无关,我认为您在浏览器控制台中甚至没有任何违反 CSP 的情况。
解决方案是在allow=属性中明确指定允许的来源:
<iframe src="https://MyChildSite.com" allow="geolocation 'self' https://MyParentSite.com"></iframe>
Run Code Online (Sandbox Code Playgroud)
或者,您可以删除allow=属性(或设置 allow='*'):
<iframe src="https://MyChildSite.com"></iframe>
Run Code Online (Sandbox Code Playgroud)
并Feature-Policy: geolocation 'self' https://MyParentSite.com在 iframe 中使用来设置权限。
| 归档时间: |
|
| 查看次数: |
1477 次 |
| 最近记录: |