Cross-global fetch usage - Web APIs 编辑

This article explains an edge case that occurs with fetch (and potentially other APIs exhibiting the same kind of resource retrieval behavior). When a cross-origin fetch involving a relative URL is initiated from an <iframe>, the relative URL used to be resolved against the current global location, rather than the iframe's location.

The edge case

Many sites will never come up against this edge case. To see it:

  • You need a same-origin iframe
  • That same-origin iframe needs to have a location with a different base URL
  • You have to use the fetch function cross-global, e.g. frame.contentWindow.fetch()
  • The URL passed to fetch needs to be relative

The problem

In the past we would resolve the relative URL against the current global, for example:

let absolute = new URL(relative, window.location.href)

This is not a problem as such. It is just that different APIs that exhibit this kind of behavior were doing it inconsistently with the behavior defined in the spec, which could lead to problems further down the line.

The solution

In Firefox 60 onwards, Mozilla resolves the relative URL against the global that owns the fetch() function being used (see bug 1432272). So in the case described above, it is resolved against the iframe's location:

let absolute = new URL(relative, frame.contentWindow.location.href)

There is a lot of discussion in progress about getting new specs to align with this behavior change, to mitigate potential problems going forward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:93 次

字数:2120

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文