CORS is a double checked security, both in client and in server, right?
No. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS. There is no server-side checking in the CORS protocol, as defined in https://fetch.spec.whatwg.org/.
How is this checked in server?
CORS is not enforced on the server side. Browsers are the sole enforcement point. Servers participate in the protocol simply by setting particular response headers.
Specifically, when a server receives a request, the CORS protocol doesn’t require servers to check whether or not it’s a cross-origin request. Servers may check for the presence of the Origin
request header and do something different based on whether it’s found or what its value is, but the CORS protocol doesn’t depend on whether servers do that or not.
Moreover, the CORS protocol definitely doesn’t require servers to block requests or to do anything else specific with requests. Instead, servers simply include the Access-Control-Allow-Origin
response header in responses.
Browsers always receive the responses just as they normally would—but if they’re responses for cross-origin requests, then the browsers enforce the CORS protocol by refusing to expose the response the frontend/client-side JavaScript code running in a web app unless the response includes an Access-Control-Allow-Origin
response header that indicates the server is opting in to allowing the request cross-origin.
Not allowing cross domain requests is used because I don’t want to get requests from outside my web application for security reasons.... right?
The CORS protocol doesn’t block all requests to any resource. Unless your web resource is running inside an intranet or otherwise behind a firewall of some kind, than any arbitrary non-browser tools/applications can still get to your web resource. CORS doesn’t block them.
CORS isn’t particularly useful for the case of web resources that are already publicly accessible over HTTP without restrictions. Instead CORS is mainly intended for the case of web resources running inside an intranet or otherwise behind a firewall and that are only otherwise protected by IP-based authentication that assumes anybody inside the intranet/firewall is trusted.
The problem CORS is meant to address is, when a user accesses an arbitrary outside web app inside such an intranet/firewall, that web app runs with the same authority as the user. So if web resources inside the intranet/firewall trust the user just based on IP address, then without cross-origin restrictions, any arbitrary outside web app also runs with the same authority/trust as the user.
What about if I am not a browser and I don’t implement any security checkings? … If I make a java app to make requests to http://mywebapp.com/customers I could get the answer despite the fact I am not asking this from origin http://mywebapp.com:80, right?
Yes. The CORS protocol does nothing to restrict that.