为什么跨源简单 POST 请求不会触发预检?
Why does a cross-origin simple POST request not trigger a preflight check?
为什么跨源简单 POST 请求不会触发预检? From the Mozilla docs:
A request that doesn’t trigger a CORS preflight—a so-called “simple request”...
The only allowed methods are:
- GET
- HEAD
- POST
...
The only allowed values for the Content-Type header are:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
然而,如果用户访问 evilsite.com,并且他们被诱骗填写一个只有表单 action="http://elsewhere.com" 的表单,并且 elsewhere.com 上的服务器期望有效post 带有 multipart/form-data 的请求(或其他 2 个中的任何一个)不会保护 elsewhere.com 上的服务器吗?这些实际上不应该接受 CORS 预检吗?我在这里错过了什么
...the servers on elsewhere.com
are expecting valid post requests with multipart/form-data
(or any of the other 2 really) wouldn't that NOT protect the servers on elsewhere.com
?
是的,它不会,这将是 elsewhere.com
到 "expect valid post requests" 方面的重大安全故障。这种攻击——Cross-Site Request Forgery——在有或没有 CORS 的情况下都存在,由服务器来防御它。
CORS 的引入是为了在不引入任何 new 安全问题的情况下实现跨源请求。它没有解决这个现有的安全问题,因为这样做会产生成本(预检请求不是免费的)但没有好处(因为服务器仍然必须保护自己免受不使用 CORS 的浏览器的影响)。
我在 this answer 中对此进行了更详细的介绍。
为什么跨源简单 POST 请求不会触发预检? From the Mozilla docs:
A request that doesn’t trigger a CORS preflight—a so-called “simple request”...
The only allowed methods are:
- GET
- HEAD
- POST
...
The only allowed values for the Content-Type header are:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
然而,如果用户访问 evilsite.com,并且他们被诱骗填写一个只有表单 action="http://elsewhere.com" 的表单,并且 elsewhere.com 上的服务器期望有效post 带有 multipart/form-data 的请求(或其他 2 个中的任何一个)不会保护 elsewhere.com 上的服务器吗?这些实际上不应该接受 CORS 预检吗?我在这里错过了什么
...the servers on
elsewhere.com
are expecting valid post requests withmultipart/form-data
(or any of the other 2 really) wouldn't that NOT protect the servers onelsewhere.com
?
是的,它不会,这将是 elsewhere.com
到 "expect valid post requests" 方面的重大安全故障。这种攻击——Cross-Site Request Forgery——在有或没有 CORS 的情况下都存在,由服务器来防御它。
CORS 的引入是为了在不引入任何 new 安全问题的情况下实现跨源请求。它没有解决这个现有的安全问题,因为这样做会产生成本(预检请求不是免费的)但没有好处(因为服务器仍然必须保护自己免受不使用 CORS 的浏览器的影响)。
我在 this answer 中对此进行了更详细的介绍。