Understanding CORS - a basic introduction
medium.com/@baphemot/understanding-cors-18ad6...Posted Jan 27, 2018, 4:23 PM UTC
Posted Jan 27, 2018, 4:23 PM UTC
The explanation of what CORS is and why it exists is wrong. Its purpose is not to prevent "rogue JS" calling malicious APIs. In fact CORS does not add any extra security to any web site. CORS is a actually a relaxation of the same origin policy, while still giving any server/API a naive way of controlling what websites can access it.
Its purpose is not to prevent "rogue JS" calling malicious APIs
It's to prevent malicious JS calling legitimate APIs and executing procedures on your behalf.
True, just not malicious JS injected to the original web app code. In the original version of the article it could easily be interpreted that way.
Thanks, I'll update accordingly to make that more apparent. I would however agrue that it provides some added security over not having Same Origin and CORS at all :)
I think you could be a bit clearer about what the underlying cause of the issue is (the same origin policy), and that CORS is a mechanism to solve that in a somewhat safe/flexible way.
Also a small typo: rouge -> rogue
Thanks, fixed the typo and re-worded the into to:
Before CORS became standarized there was no way to call an API endpoint under different domain for security reasons. This was (and to some degree still is) blocked by the Same-Origin Policy.
CORS is a mechanism which aims to allow requests made on behalf of you and at the same time block some requests made by rogue JS and is triggered whenever you are making an HTTP request to:
Well, there was a way... JSONP. But it was a somewhat hacky workaround.
I still feel confused about the whole concept. I’ve read every MDN page and article about CORS, and I already get all the definitions I just don’t understand why each header/rule/etc is required. I really feel like I’d need real example attacks against which these mechanisms defend.
For example, I’ve written a small server before but I’ve never had to worry about CORS afaict, was I doing something wrong?
By default, you don't need the rules - you only need them to protect your users. If you deal with sensitive data, or don't plan on making the API public, you should probably implement an secure CORS setup with ACA-Origin pointing to your only domain.
Does that actually help anything? Can a hacker not just fake the domain when sending requests to the API?
Your browser will not allow you to overwrite the Origin header, so he's only able to mess with your requests if he's able to inject code into the original domain.
If he has your credential (e.g. session cookie) he can still fake the calls by not using browser at all.
Assuming he sticks to using normal browsers. HTTP clients like Postman don't care.
I don't think it does much for security, it's just something you need to do to make conventional browsers play along.
I recently had to deal with a CORS issue using a .net web api 2 project. I tried attacking this problem from any and all angles. Checking headers, http method, the code in the web api config and nothing worked. It turned out to be a configuration issue with IIS and my app pool. My point is this problem can stem from a lot of different things not just the probable ones like missing headers etc
What was the configuration in IIS called?
From what I hear you saying, it seems like this setting would be looking for the Origin header and actually doing a kind of server-side CORS validation, like since it sees the Origin header it knows the browser supports CORS, so it imposes some policy you have to configure on the app pool in IIS?
IIRC IIS doesn't handle the OPTIONS verb/method by default
Wowza, thanks, I'll add that to my collection of random Microsoft software knowledge! :)
It's been a minute since I last configured IIS but the default was only allowing POST and GET
Off the top of my head it was managed pipeline mode set incorrectly and something else on the same section
Jsonp
Same happened with me but using java app, and i solved it in tomcat configuration.
If the server does not respond with specific headers the subsequent GET or POST request will still be send, the data still received but the browser will not allow JavaScript to access the response (if you explicitly want to add cookies etc. and the server does not respond properly, the request will not be sent!).
Can you provide a source for this? That sounds like a tremendous security hole. If this is true, rogue JS should be able to send an AJAX request to my bank to transfer money out of my account, even if it can't access the response.
Sure. Open your console right now, and type:
fetch('https://example.com')
You will see
Failed to load https://example.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.reddit.com' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Now, assuming you are using Chrome, you will see the Response tab empty (this is a recent change!) but still see
content-encoding:gzip
content-length:606
content-type:text/html
in the response headers. Firefox will also show the response in the Response tab.
This is technically OK because you're making a call with no credentials (unless you shot yourself in the foot and pass any credentials in the URL, not header) - the server is not able to authenticate you and should not perform any dangerous actions.
As for your concerns about rogue JS making a bank transfer, this is the second part. Assume you have to send a token:
fetch('https://example.com', { headers: { 'x-my-token': 'test' }})
The OPTIONS response - preflight - will be blocked and no actual GET response snet afterwards. I might have worded that poorly in the article, I've updated the section to read
If the server does not respond with specific headers the subsequent “simple” GET or POST request will still be send, the data still received but the browser will not allow JavaScript to access the response.
If you explicitly want to add cookies, custom headers and other features making the request not longer a “simple request”, and the server does not respond properly, the request will not be sent.
that's what CSRF tokens are made to prevent.
however i believe it's standard for an OPTIONS request to be sent first which prevents the POST/GET request.
From what I understand from the part I quoted, even if the actual request doesn't match the preflight response, it will be sent anyway.
[deleted]
#1
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method
#2
Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.
#3 Servers can check the referrer on the request to ensure it's in the whitelist before executing the request.
#4 CSRF tokens should also be used.
Doesn’t that last bit mean that the rogue JS won’t have access to your account though? Since you’d need cookies to access your session, the request wouldn’t be sent.
Thanks for the article, I recently started programming and ran into this issue while making a chatroom. Very enlightening!
[deleted]
Hi there. This is the latest article in my "Understanding ..." "series" in which I try to explain why CORS is needed, and why the error you see in the console is not really an error :)