r/javascript • u/[deleted] • Jan 27 '18
Understanding CORS - a basic introduction
https://medium.com/@baphemot/understanding-cors-18ad6b478e2b13
u/aajax0 Jan 28 '18
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.
1
Jan 28 '18
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 :)
2
u/evenisto Jan 28 '18
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.
1
u/aajax0 Jan 28 '18
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.
10
u/NoInkling Jan 28 '18
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
2
Jan 28 '18
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:
1
6
u/llanfairpwll123 Jan 28 '18
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?
1
Jan 28 '18 edited Jan 28 '18
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.
1
Jan 28 '18
Does that actually help anything? Can a hacker not just fake the domain when sending requests to the API?
1
Jan 28 '18
Your browser will not allow you to overwrite the
Originheader, 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.
1
Jan 28 '18
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.
0
Jan 28 '18
Yes, but in this case he would need to aquire your credentials, which he might not "pull" out from whatever context his exploit is running in (e.g. due to CSP).
I know it's not a security feature on its own, but is part of a biger-picture security mechanism.
3
u/BehindTheMath Jan 28 '18
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.
6
Jan 28 '18 edited Jan 28 '18
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/htmlin 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
OPTIONSresponse - 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 readIf 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.
3
u/bzBetty Jan 28 '18
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.
1
u/BehindTheMath Jan 28 '18
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.
2
u/bzBetty Jan 28 '18
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.
1
u/llanfairpwll123 Jan 28 '18
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.
6
u/the_ju66ernaut Jan 27 '18
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
1
u/thedude42 Jan 27 '18
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?
3
u/equallyunequal Jan 27 '18
IIRC IIS doesn't handle the OPTIONS verb/method by default
2
u/thedude42 Jan 27 '18
Wowza, thanks, I'll add that to my collection of random Microsoft software knowledge! :)
4
u/equallyunequal Jan 27 '18
It's been a minute since I last configured IIS but the default was only allowing POST and GET
1
u/sopes Jan 28 '18
Testing the limits of my memory here... But I had similar issues and believe it ended up being that WebDAV or some other module was intercepting all of the verbs except GET and POST.
So for me or was as simple as disabling the"offending" modules for the site. (The not so simple part of course was identifying them in the first place)
1
u/the_ju66ernaut Jan 28 '18
Off the top of my head it was managed pipeline mode set incorrectly and something else on the same section
1
1
u/kurosaki1990 Jan 28 '18
Same happened with me but using java app, and i solved it in tomcat configuration.
2
u/Waldiboy Jan 28 '18
Thanks for the article, I recently started programming and ran into this issue while making a chatroom. Very enlightening!
30
u/[deleted] Jan 27 '18
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 :)