为什么我无法跨站点和跨框架获取对象 URL(又名 "blob URL")?

Why can't I fetch an object URL (aka "blob URL") cross-site and cross-frame?

我正在开发一个 iframeable 组件,现在偶然发现了一些看起来像安全问题的东西。

我有两个网站:

站点 A 将站点 B 嵌入到 IFrame 中。站点 B 需要数据才能使用,这应该由站点 A 提供。但是,现在站点 B 仅通过 URLs 接受数据,并且由于原始 URL 可能需要身份验证,因此会获取数据由站点 A 转换为对象 URL(使用 URL.createObjectURL),然后通过 postMessage 提供给站点 B。站点 B 现在应该从 URL 获取数据并使用它。

但是,站点 B 获取对象 URL 被浏览器阻止:

据我所知,这些消息未连接到 CSP 或 CORS。但是,我不明白问题是什么以及它是否可以修复——我的印象是对象 URLs 目前没有任何跨站点问题,这正是为什么正在使用。

那么为什么会发生这种情况,我能做些什么呢?

参见 https://github.com/w3c/FileAPI/issues/135 and the references therein. There is a same origin restriction in practice, but this isn't reflected in specifications as of yet. It's unlikely we'll remove that restriction as blob: URLs make it easy to create memory leaks. (In fact, we want to place more restrictions on them: https://github.com/w3c/FileAPI/issues/153。)

如果您有 postMessage() API,则需要对其进行扩展,以便向 URL 或对象发送消息。向 Blob 实例本身发送消息应该有效。