What is the difference between CORS and CSPs?

What is the difference between CORS and CSPs?

问题

From my perspective, the technologies referred to as Cross-Origin Resource Sharing (CORS) and Content Security Policies (CSPs) seem to be very similar in purpose and implementation.

Both seem to allow you to whitelist the origins of resources which an uncompromised version of your webpage incorporates, via HTTP response headers. The only difference I can see is that CSPs seem to be more fine-grained in what you can approve in your HTTP response.

回答1

CORS allows the Same Origin Policy to be relaxed for a domain.

e.g. normally if the user logs into both example.com and example.org, the Same Origin Policy prevents example.com from making an AJAX request to example.org/current_user/full_user_details and gaining access to the response.

This is the default policy of the web and prevents the user's data from being leaked when logged into multiple sites at the same time.

Now with CORS, example.org could set a policy to say it will allow the origin https://example.com to read responses made by AJAX. This would be done if both example.com and example.org are ran by the same company and data sharing between the origins is to be allowed in the user's browser. It only affects the client-side of things, not the server-side.

CSPs on the other hand set a policy of what content can run on the current site. For example, if JavaScript can be executed inline, or which domains .js files can be loaded from. This can be beneficial to act as another line of defence against XSS attacks, where the attacker will try and inject script into the HTML page. Normally output would be encoded, however say the developer had forgotten only on one output field. Because the policy is preventing in-line script from executing, the attack is thwarted. 

 

回答2

None of the answers above give a clear and concise difference between CSP and CORS. Here is my way of thinking about them:

Let's say we have abc.com website that wants to send a request to def.net.

  1. When user visits abc.com in browser, abc.com server returns abc.com HTTP response, CSP restriction within this response can prevent abc.com in browser from issuing request to def.net.
  2. If there is no CSP restriction within abc.com HTTP response, then abc.com in browser can send a request to def.net.
  3. Upon receiving the request, def.net server responds with def.net HTTP response, CORS restriction within this response can prevent abc.com in browser from loading it. (Note that by default, Same-origin policy will restrict the response from loading, unless otherwise specified by CORS)

So CSP protects abc.com and same-origin policy (the lack of CORS) protects def.net in the example above.

 

CSRF, CORS, and HTTP Security headers Demystified

The difference from CORS is that CORS prevents a third party from accessing a server,

while CSP prevents a website itself from loading content from a third party, as a defence against XSS.

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(62)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-06-04 Type Interceptors
2019-06-04 160. Intersection of Two Linked Lists
2019-06-04 save change is not permitted
2019-06-04 获取系统的documents路径
2017-06-04 CSS Text
点击右上角即可分享
微信分享提示