首页 技术 正文
技术 2022年11月22日
0 收藏 335 点赞 4,049 浏览 3844 个字
  • 简单请求不可重定向,包括第一个preflight请求和第二个真正的请求都不行。
  • 简单请求可以重定向任意多次,但如需兼容多数浏览器,只可进行一次重定向。
  • 中间服务器应当同样配置相关 CORS 响应头。

中间服务器设置

当跨域请求被重定向时,中间服务器返回的 CORS 相关的响应头应当与最终服务器保持一致。 任何一级的 CORS 失败都会导致 CORS 失败。这些头字段包括Access-Control-Allow-Origin, Access-Control-Allow-Credentials等。

响应 preflight 的头字段包括Access-Control-Allow-Headers, Access-Control-Allow-Methods等。 因为 preflight 不允许重定向(见下文),所以中间服务器也就不必管这些 preflight 头字段。

如果中间服务器未设置Access-Control-Allow-Origin,在 Chrome 中的错误信息为:

XMLHttpRequest cannot load http://mid.com:4001/redirect.
Redirect from 'http://mid.com:4001/redirect' to 'http://index.com/access-control-allow-origin-wildcard'
has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://index.com:4001' is therefore not allowed access.

如果最终服务器未设置Access-Control-Allow-Origin,在 Chrome 中的错误信息为:

XMLHttpRequest cannot load http://index.com:4001/access-control-allow-origin-not-set.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.

Origin 变成 null 的解释见下文。

重定向preflight 请求

任何非 2xx 状态码都认为 preflight 失败, 所以 preflight 不允许重定向。各浏览器表现一致不再赘述,可参考 W3C:

The following request rules are to be observed while making the preflight request:

  • If the end user cancels the request Apply the abort steps.

  • If the response has an HTTP status code that is not in the 2xx range Apply the network error steps.

– W3C CORS Recommandation: Cross-Origin Request with Preflight

重定向简单请求

对于简单请求浏览器会跳过 preflight 直接发送真正的请求。 该请求被重定向后浏览器会直接访问被重定向后的地址,也可以跟随多次重定向。 但重定向后请求头字段origin会被设为"null"(被认为是 privacy-sensitive context)。 这意味着响应头中的Access-Control-Allow-Origin需要是*或者null(该字段不允许多个值)。

即使浏览器给简单请求设置了非简单头字段(如DNT)时,也应当继续跟随重定向且不校验响应头的DNT (因为它属于User Agent Header,浏览器应当对此知情)。 参考 W3C 对简单请求的处理要求

If the manual redirect flag is unset and the response has an HTTP status code of 301, 302, 303, 307, or 308 Apply the redirect steps. – W3C CORS Recommendation

OSX 下 Chrome 的行为是标准的,即使设置了DNT也会直接跟随重定向。

Safari 的怪异行为

Safari 在设置DNT字段后,会向重定向后的地址首先发起 preflight(可能是它忘记了该头部是自己设置的?)。 这一行为在桌面 Safari 的隐身模式,以及 iOS 很多浏览器中都可以观察到。 Safari 远程调试 iPhone,遇此行为调试器会崩掉(笔者试了3个 Mac+iPhone Pair)。 建议使用tcpdump或者写一个简单的 CORS 服务器来调试。 在 OPTIONS 请求中,会带有Access-Control-Request-Headers来声明需要发送dnt

Access-Control-Request-Headers: 'dnt, accept-language, origin'

这意味着为了 Safari 系列的浏览器(包括 iOS 平台的多数浏览器), 重定向简单 CORS 请求仍然需要实现 OPTIONS 方法(虽然我们发的只是是简单请求)。 并且在Access-Control-Allow-Headers响应头字段添加dnt声明。 否则 Safari 会认为 CORS 失败:

XMLHttpRequest cannot load http://index.com:4001/access-control-allow-origin-wildcard.
Request header field DNT is not allowed by Access-Control-Allow-Headers.

为了轻松地让 CORS preflight 成功,测试环境中可以简单地将请求头Access-Control-Request-Headers的内容直接设置到响应头的Access-Control-Allow-Headers

重定向非简单请求

非简单请求是 preflight 成功后才发送实际的请求。 preflight 后的实际请求不允许重定向,否则会导致 CORS 跨域失败。

虽然在 Chrome 开发版中会对重定向后的地址再次发起 preflight,但该行为并不标准。 W3C Recommendation中提到真正的请求返回301302303307308都会判定为错误:

This is the actual request. Apply the make a request steps and observe the request rules below while making the request. If the response has an HTTP status code of 301, 302, 303, 307, or 308 Apply the cache and network error steps. – W3C CORS Recommendation

在 Chrome 中错误信息是Request requires preflight, which is disallowed to follow cross-origin redirect

XMLHttpRequest cannot load http://mid.com:4001/cross-origin-redirect-with-preflight.
Redirect from 'http://mid.com:4001/cross-origin-redirect-with-preflight' to 'http://dest.com:4001/access-control-allow-origin-wildcard'
has been blocked by CORS policy: Request requires preflight,
which is disallowed to follow cross-origin redirect.

在 Safari 中的错误信息是Cross-origin redirection denied by Cross-Origin Resource Sharing policy.

XMLHttpRequest cannot load http://mid.com:4001/redirect.
Cross-origin redirection denied by Cross-Origin Resource Sharing policy.

多次重定向的讨论

多次重定向涉及的一个关键问题是:preflight 后的请求不允许重定向。因此:

  • 对于简单请求并且没有任何 preflight 的情况:浏览器会一直跟随重定向(当然 HTTP 另有规定的除外,如 POST 被 302 时),直到最后一个请求返回或者中间请求的 CORS 验证失败(比如Access-Control-Allow-Origin设置错误)。
  • 对于简单请求但是浏览器会发起 preflight 的情况(比如 Safari 对 DNT 的处理):因 preflight 后重定向真正的请求会导致 CORS 失败,所以多次重定向是不可行的。
  • 对于非简单请求:浏览器会直接发起 preflight,后续的重定向都是不允许的因此多次重定向不可行。

总之,如果需要兼容大多数浏览器,不论是否为简单请求都不可以多次重定向

来源: https://harttle.land/2016/12/30/cors-redirect.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860