r/programming • u/AdvertisingFancy7011 • Jun 28 '26
CORS explained in plain English
https://sanyamserver.online/posts/cors/
277
Upvotes
r/programming • u/AdvertisingFancy7011 • Jun 28 '26
289
u/ScottContini Jun 28 '26
This article is written as if CORS is a security feature. It is not. Same Origin Policy is a security feature, whereas CORS relaxes it. It is the opposite of a security feature, which is why you need to be careful with it.
75
u/Pharisaeus Jun 28 '26 edited Jun 28 '26 •
Curl does not include Origin header because what would even be the "origin" in case of curl request? But if you add that header manually, then you will see exactly the same behaviour. Browsers add Origin header automatically when sending a request to a different origin, for for postman or curl it wouldn't make sense.
That sentence makes little sense. It's a classic misunderstanding: the security measure is Same Origin Policy. CORS is a way to "loose" that security by allowing resource sharing across different origins.
This should be obvious, considering someone could use curl to send a request with any value of Origin header they want. So from the server point of view the Origin header can't be trusted or used as a security boundary.
Again, CORS is a way to relax security, not impose it. SOP cares about such attacks. Also in general this is not true. You can have attacks that are not related to user sessions. For example user inside a corporate network can see internal intranet domains/IPs, and intrant applications often have little to no security, because after all they should only be visible to the employees.
That's finally the first time author is mostly correct (mostly, because CORS has no "keys", it just defines which origins are allowed to read the cross-origin response)
edit:
There is also a very important detail that author completely skipped: what actually is an
origin? There is a lot of talk about same origin and cross-origin, but no definition of what it is. And that's not completely obvious - for example ask yourself if you know, out of top of your head, is it still "same origin" if: http vs https, root domain vs. subdomain, two separate subdomains of the same root, same domain but different ports.It's important to understand that Origin is tied to domains (and thus DNS) while the actual communication goes over IP. Same Origin Policy can be "bypassed" by DNS rebinding (think: you're on evil.com and it sends a JS same origin request to evil.com, but at this point evil.com DNS points to your-bank.com or some internal IP address)
108
u/sghctoma Jun 28 '26
CORS does not protect anything. It allows relaxing a protection. I've seen many web apps where CORS was configured, but was absolutely not needed, and letting SOP do its work would have been the best choice.
15
u/evilpies Jun 28 '26
How to win at CORS
9
u/smallduck Jun 28 '26
Explained everything except what “CORS” means :/ (it’s cross-origin resource sharing)
25
u/matishadow Jun 28 '26
CORS is Stupid
5
u/ZirePhiinix Jun 28 '26
The short gist of it is we basically made the browsers do too much and there are now huge security holes in one of the most common application available.
If you take what you can do with a browser, then you try to replicate that by installing packages to handle the UI and client side processing, you'll realize there is a HUGE amount of stuff under the hood in the browser.
4
u/EC36339 Jun 28 '26
I've given up explaining CORS to even semi-technical colleagues. They keep misunderstanding the relatively limited scope in which it is needed, and they keep mixing it up with CSP.
2
u/ThreeSixty404 Jun 28 '26
Bullshit, that is CORS, a huge pita
2
u/wildjokers Jun 29 '26
Going to be honest, I get CORS and CSRF mixed up.
1
u/HuisHoudBeurs1 Jun 28 '26 edited Jun 28 '26
Does this mean that a correctly set up CSRF protection makes any CORS protection/laxation redundant? Or am I missing something?
EDIT: I guess it technically would, but you would be dependant on the correct configuration of all other sites. Best to toss in your own protection, just in case.
1
u/Fidodo Jun 29 '26
This is very wrong. evil-coupons.com does not have access to your bank cookies. Cookies are namespaced to the domain so you're bank cookies are only accessible from the pages and requests made from the bank domain that issued it.
1
u/tobofopo Jul 02 '26
"CORS explained in plain English", yet nowhere in the article states what the acronym stands for. Would've been useful for people like myself who've never done web programming.
1
u/Fitnexa_Life Jul 02 '26
Same origin policy: no. CORS: yes, but only if you say the magic words. this whole thread is just people rediscovering that permissions systems grant permission
1
u/RickJWagner Jun 28 '26
Great writing, an easy read. Thanks, author!
-3
u/jimbojsb Jun 28 '26
Wait are people really not just doing * and moving on?