Same-origin policy
Two URLs have the same origin if the protocol, port, and host are the same for both.
URL: http://store.company.com/dir/page.html
Cross-origin network access
- Cross-origin writes 跨源写入,通常是被允许的。
- Cross-origin embedding 跨源嵌入,通常是被允许的
- Cross-origin reads 跨源读取,通常是不被允许的
以下资源是允许被嵌入的:
<script src="..."></script>
<link rel="stylesheet" href="...">
<img>
<video>
and<audio>
<object>
and<embed>
@font-face
<iframe>
. Sites can use theX-Frame-Options
header to prevent corss-origin framing.
允许跨源访问
使用CORS
阻止跨源访问
- 阻止跨域写操作,只要检测请求中的一个不可推测的标记 (CSRF token) 即可,这个标记被称为 Cross-Site Request Forgery (CSRF) 标记。你必须使用这个标记来阻止页面的跨站读操作。
- 阻止资源的跨站读取,需要保证该资源是不可嵌入的。阻止嵌入行为是必须的,因为嵌入资源通常向其暴露信息。
- 阻止跨站嵌入,需要确保你的资源不能通过以上列出的可嵌入资源格式使用。浏览器可能不会遵守
Content-Type
头部定义的类型。例如,如果您在 HTML 文档中指定<script>
标记,则浏览器将尝试将标签内部的 HTML 解析为 JavaScript。 当您的资源不是您网站的入口点时,您还可以使用 CSRF 令牌来防止嵌入。
通常来说,嵌入一个跨域资源是被允许的,但是读取一个跨域资源是被禁止的。