简体   繁体   English

什么是文件 URI 的同源策略?

[英]What is the Same-Origin Policy for File URIs?

Perhaps my searching(Google-Fu) has deserted me, but I can't find a good description of the same-origin policy for file URIs other than this outdated Mozilla page .也许我的搜索(Google-Fu)已经抛弃了我,但除了这个过时的 Mozilla 页面之外,我找不到文件 URI 的同源策略的良好描述。 Can anyone point me to an explanation of the same-origin policy for file URIs?谁能指出我对文件 URI 的同源策略的解释? In particular, if I have a script loaded from (say) file:///C:/Users/Joe/Test/test.html, what files is that script allowed to access using XMLHttpRequest?特别是,如果我从(比如说)file:///C:/Users/Joe/Test/test.html 加载了一个脚本,那么该脚本允许使用 XMLHttpRequest 访问哪些文件? And how should I specify the URI, ie, as relative to the script's URI?我应该如何指定 URI,即相对于脚本的 URI?

Note that I'm not asking for a way to get around cross-origin restrictions, just an understanding of where I need resources to reside so that I can load them without triggering a cross-origin error.请注意,我并不是在寻求绕过跨域限制的方法,只是了解我需要资源驻留的位置,以便我可以在不触发跨域错误的情况下加载它们。

The same-origin policy for file:/// URIs is implementation-dependent. file:/// URI 的同源策略是依赖于实现的。

The W3C's CORS spec gets its definition of an "origin" from IETF RFC 6454 "The Web Origin Concept" . W3C 的 CORS 规范从 IETF RFC 6454 "The Web Origin Concept"中获得了“起源”的定义。 In section 4 "Origin of a URI" it reads:第 4 节“URI 的来源”中,它写道:

  1. If uri-scheme is "file", the implementation MAY return an implementation-defined value.如果 uri-scheme 是“文件”,实现可以返回一个实现定义的值。

NOTE: Historically, user agents have granted content from the file scheme a tremendous amount of privilege.注意:从历史上看,用户代理已授予文件方案中的内容大量特权。 However, granting all local files such wide privileges can lead to privilege escalation attacks.但是,授予所有本地文件如此广泛的权限可能会导致权限提升攻击。 Some user agents have had success granting local files directory-based privileges, but this approach has not been widely adopted.一些用户代理已经成功地授予本地文件基于目录的权限,但这种方法并没有被广泛采用。 Other user agents use globally unique identifiers for each file URI, which is the most secure option.其他用户代理对每个文件 URI 使用全局唯一标识符,这是最安全的选项。

Looking up the behavior (and the reasoning behind it) for specific browsers is not easy.查找特定浏览器的行为(及其背后的原因)并不容易。 I actually think the old Mozilla wiki page you referenced is one of the better resources on this topic.实际上,我认为您引用的旧 Mozilla wiki 页面是有关该主题的更好资源之一。 Here's a fairly helpful discussion ; 这是一个相当有帮助的讨论 general guidance is to assume the browser may treat all file:/// URIs as totally unique origins.一般指导是假设浏览器可以将所有file:/// URI 视为完全唯一的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM