关于客户端安全,CORS 除了破坏同源策略之外还有其他作用吗?
(如果不是,它实际上会提高客户端安全性吗?)
我正在考虑来自服务器 X 的脚本使用 XHR 从服务器 Y(支持 CORS)获取并运行不受信任的代码的情况。
(显然评估不受信任的代码是不好的™)
(and if not, does it actually improve client side security?)
I'm thinking of the case where a script from server X uses XHR to obtain and run untrusted code from server Y (which supports CORS).
(obviously evaluating untrusted code is bad™)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我根本不使用 CORS 来提高安全性。我使用 CORS 访问不同域上的已知 Web 服务,如果没有 CORS,我将无法访问该服务。在我看来,这与提高安全性无关,而是允许将一个域中的数据委托给另一个域。
I do not use CORS to improve security at all. I use CORS to access a known webservice on a different domain which I would not be allowed to access without CORS. Nothing to do with improving security in my opinion, but to allow data from one domain to be entrusted to another.
CORS 并不是要强化安全性,而是要削弱安全性(但仅在服务器许可的特定条件下)。
如果您想在 AJAX 请求中从另一台服务器访问某些内容,而没有 CORS,则由于“安全”(同源策略)而不允许您访问,这就是它的结束*。通过 CORS,其他服务器可以授予权限以减少安全屏障。
<子>
* 除了像 JSONP 这样的 hack,但这也需要服务器的许可
CORS isn't about hardening security, it's about weakening it (but only under certain conditions with permission from the server).
If you want to access something from another server in an AJAX request, without CORS, you aren't allowed due to "security" (same origin policy), and that is the end of it*. With CORS, the other server can give permission to reduce that security barrier.
* Except for hacks like JSONP, but that also requires permission from the server
CORS 颠覆了同源策略,但是是有选择的。例如,银行网站域根本不会设置 CORS 标头(以保持同源完全有效),因为从其他域下载的 JavaScript 不应该向银行发出 AJAX 请求(或者他们可能只允许他们信任的合作伙伴网站) )。 CDN 可能会设置 Access-Control-Allow-Origin “*”,因为它不关心从另一个域下载的 JavaScript 是否向 CDN 发出 AJAX 请求。
CORS subverts the same-origin policy, but selectively so. For example a bank website domain would not set a CORS header at all (to keep same-origin in full effect) as no JavaScript downloaded from other domains should be making AJAX requests to the bank (or maybe they allow just a partner site they trust). A CDN would probably set Access-Control-Allow-Origin "*" as it doesn't care if JavaScript downloaded from another domain is making AJAX requests to the CDN.