gpt4 book ai didi

iframe - 如何在 iframe 中允许 Geolocation API?

转载 作者:行者123 更新时间:2023-12-04 16:25:45 24 4
gpt4 key购买 nike

我有一个 iframe 标签,其中 src 是不同服务器上的另一个网页。我有能力修改两个站点的标题。在开始实现控制安全策略之前,我能够单击 iframe 内的按钮并检索 GPS 坐标。我相信控制安全策略会阻止我的父站点运行 Geolocation API。
父站点的代码:

<customHeaders>
<add name="Content-Security-Policy" value="frame-src 'self' https://MyChildSite.com" />
</customHeaders>
<html>
<iframe src="https://MyChildSite.com" allow="geolocation"></iframe>
</html>
子站点的代码:
<customHeaders>
<add name="Content-Security-Policy" value="frame-src 'self' https://MyParentSite.com" />
<add name="Feature-Policy" value="geolocation 'self' https://MyParentSite.com" />
</customHeaders>
<html>
<button onclick="getCoordinates()">Get GPS</button>
...list some stuff
</html>
当我通过父站点单击子站点上的按钮时,我没有从坐标中得到我期望的响应。
这个问题有方法解决吗?

最佳答案

,--------------------- 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> |
!______________________________________________________________________________|
  • 为什么allow="geolocation" -> allow="geolocation https://MyChildSite.com请参阅 Directive in the allow= attribute is specified without keys将源自 src=属性。
  • 将功能策略权限传递到嵌套浏览上下文中有一些细节。 iframe 不能委派他自己(或子嵌套的 iframe)比父文档授予的权限更多。

    如果你有一个在 iframe 中运行的脚本,你可以使用 featurePolicy.getAllowlistForFeature界面以获取所有允许来源的列表并查看发生了什么。
  • 您的问题与内容安全策略无关,我认为您在浏览器控制台中甚至没有任何违反 CSP 的情况。


  • 解决方案是在 allow= 中明确指定允许的来源属性:
    <iframe src="https://MyChildSite.com" allow="geolocation 'self' https://MyParentSite.com"></iframe>
    或者,您可以删除 allow=属性(或设置 allow='*'):
    <iframe src="https://MyChildSite.com"></iframe>
    并使用 Feature-Policy: geolocation 'self' https://MyParentSite.com在 iframe 中设置权限。
  • 不要忘记地理定位 API 仅在安全上下文中工作(意味着通过 https: only)。您可以查看 window.isSecureContext属性来执行适当的诊断。

  • PS:我可以请您在您的问题中添加“功能策略”标签,这将有助于其他人。

    关于iframe - 如何在 iframe 中允许 Geolocation API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64582203/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com