I recently had to set Access-Control-Allow-Origin
to *
in order to be able to make cross-subdomain AJAX calls. I feel like this might be a security problem. What risks am I exposing myself to if I keep the setting?
Access-Control-Allow-Origin: *
is totally safe to add to any resource, unless that resource contains private data protected by something other than standard credentials. Standard credentials are cookies, HTTP basic auth, and TLS client certificates.
Eg: Data protected by cookies is safe
Imagine https://example.com/users-private-data
, which may expose private data depending on the user's logged in state. This state uses a session cookie. It's safe to add Access-Control-Allow-Origin: *
to this resource, as this header only allows access to the response if the request is made without cookies, and cookies are required to get the private data. As a result, no private data is leaked.
Eg: Data protected by location / ip / internal network is not safe (unfortunately common with intranets and home appliances):
Imagine https://intranet.example.com/company-private-data
, which exposes private company data, but this can only be accessed if you're on the company's wifi network. It's not safe to add Access-Control-Allow-Origin: *
to this resource, as it's protected using something other than standard credentials. Otherwise, a bad script could use you as a tunnel to the intranet.
Rule of thumb
Imagine what a user would see if they accessed the resource in an incognito window. If you're happy with everyone seeing this content (including the source code the browser received), it's safe to add Access-Control-Allow-Origin: *
.
Access-Control-Allow-Origin: *
only allows requests without cookies. I've edited the answer to clarify a bit. –
Mcchesney fetch('https://intranet.example.com/company-private-data')
, which runs on your machine, and send the results back to my server. By doing this, I've used your access to the intranet to read the intranet. –
Mcchesney ACAO: *
, an attacker could set a up a malicious page that would send a login request with candidate credentials, read the response to determine whether the login attempt was successful and, therefore, whether the credentials are valid. They could enlist any visitor of their malicious page in a coordinated, distributed client-side credential-stuffing attack. Tim Tomes and Kevin Cody described such a browser botnet in their DerbyCon 2019 talk. –
Beetle By responding with Access-Control-Allow-Origin: *
, the requested resource allows sharing with every origin. This basically means that any site can send an XHR request to your site and access the server’s response which would not be the case if you hadn’t implemented this CORS response.
So any site can make a request to your site on behalf of their visitors and process its response. If you have something implemented like an authentication or authorization scheme that is based on something that is automatically provided by the browser (cookies, cookie-based sessions, etc.), the requests triggered by the third party sites will use them too.
This indeed poses a security risk, particularly if you allow resource sharing not just for selected resources but for every resource. In this context you should have a look at When is it safe to enable CORS?.
Update (2020-10-07)
Current Fetch Standard omits the credentials when credentials mode is set to include
, if Access-Control-Allow-Origin
is set to *
.
Therefore, if you are using a cookie-based authentication, your credentials will not be sent on the request.
Access-Control-Allow-Origin: *
on them? There will be no nogin etc, they are public to everyone? –
Safier Access-Control-Allow-Origin: *
myself, that's why I was looking at this Q&A. I.e. I don't know what the correct answer would be, but I would be interested –
Ellary Access-Control-Allow-Origin: *
is totally safe to add to any resource, unless that resource contains private data protected by something other than standard credentials. Standard credentials are cookies, HTTP basic auth, and TLS client certificates.
Eg: Data protected by cookies is safe
Imagine https://example.com/users-private-data
, which may expose private data depending on the user's logged in state. This state uses a session cookie. It's safe to add Access-Control-Allow-Origin: *
to this resource, as this header only allows access to the response if the request is made without cookies, and cookies are required to get the private data. As a result, no private data is leaked.
Eg: Data protected by location / ip / internal network is not safe (unfortunately common with intranets and home appliances):
Imagine https://intranet.example.com/company-private-data
, which exposes private company data, but this can only be accessed if you're on the company's wifi network. It's not safe to add Access-Control-Allow-Origin: *
to this resource, as it's protected using something other than standard credentials. Otherwise, a bad script could use you as a tunnel to the intranet.
Rule of thumb
Imagine what a user would see if they accessed the resource in an incognito window. If you're happy with everyone seeing this content (including the source code the browser received), it's safe to add Access-Control-Allow-Origin: *
.
Access-Control-Allow-Origin: *
only allows requests without cookies. I've edited the answer to clarify a bit. –
Mcchesney fetch('https://intranet.example.com/company-private-data')
, which runs on your machine, and send the results back to my server. By doing this, I've used your access to the intranet to read the intranet. –
Mcchesney ACAO: *
, an attacker could set a up a malicious page that would send a login request with candidate credentials, read the response to determine whether the login attempt was successful and, therefore, whether the credentials are valid. They could enlist any visitor of their malicious page in a coordinated, distributed client-side credential-stuffing attack. Tim Tomes and Kevin Cody described such a browser botnet in their DerbyCon 2019 talk. –
Beetle AFAIK, Access-Control-Allow-Origin is just a http header sent from the server to the browser. Limiting it to a specific address (or disabling it) does not make your site safer for, for example, robots. If robots want to, they can just ignore the header. The regular browsers out there (Explorer, Chrome, etc.) by default honor the header. But an application like Postman simply ignores it.
The server end doesn't actually check what the 'origin' is of the request when it returns the response. It just adds the http header. It's the browser (the client end) which sent the request that decides to read the access-control header and act upon it. Note that in the case of XHR it may use a special 'OPTIONS' request to ask for the headers first.
So, anyone with creative scripting abilities can easily ignore the whole header, whatever is set in it.
See also Possible security issues of setting Access-Control-Allow-Origin.
Now to actually answer the question
I can't help but feel that I'm putting my environment to security risks.
If anyone wants to attack you, they can easily bypass the Access-Control-Allow-Origin. But by enabling '*' you do give the attacker a few more 'attack vectors' to play with, like, using regular webbrowsers that honor that HTTP header.
Access-Control-Allow-Origin *
on a malicious website that hosts scripts to steal passwords is strongly discouraged :-) –
Farci Someone can set up a malicious webpage
- yes, and that someone will add the malicious directive, too. And again, any client - like an infected webbrowser - may choose to ignore http headers whatever is set in them. –
Farci 192.168.1.1
) and reconfigure your router to allow attacks. It can even use your router directly as a DDoS node. (Most routers have test pages which allow for pings or simple HTTP server checks. These can be abused en masse.) –
Khudari Here are 2 examples posted as comments, when a wildcard is really problematic:
Suppose I log into my bank's website. If I go to another page and then go back to my bank, I'm still logged in because of a cookie. Other users on the internet can hit the same URLs at my bank as I do, yet they won't be able to access my account without the cookie. If cross-origin requests are allowed, a malicious website can effectively impersonate the user.
– Brad
Suppose you have a common home router, such as a Linksys WRT54g or something. Suppose that router allows cross-origin requests. A script on my web page could make HTTP requests to common router IP addresses (like 192.168.1.1) and reconfigure your router to allow attacks. It can even use your router directly as a DDoS node. (Most routers have test pages which allow for pings or simple HTTP server checks. These can be abused en masse.)
– Brad
I feel that these comments should have been answers, because they explain the problem with a real life example.
In scenario where server attempts to disable the CORS completely by setting below headers.
Access-Control-Allow-Origin: * (tells the browser that server accepts cross site requests from any ORIGIN)
Access-Control-Allow-Credentials: true (tells the browser that cross site requests can send cookies)
There is a fail safe implemented in browsers that will result in below error
"Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’"
So in most scenarios setting ‘Access-Control-Allow-Origin’ to *
will not be a problem. However to secure against attacks, the server can maintain a list of allowed origins and whenever server gets a cross origin request, it can validate the ORIGIN header against the list of allowed origins and then echo back the same in Access-Control-Allow-Origin header.
Since ORIGIN header can't be changed by javascript running on the browser, the malicious site will not be able to spoof it.
© 2022 - 2024 — McMap. All rights reserved.
Access-Control-Allow-Origin: *
is safe. And if you do include credentials, browsers won’t allow you to doAccess-Control-Allow-Origin: *
. Safe. – EndorsementACAO: *
policy at the reverse-proxy level, which would also cover sensitive endpoints like login. – Beetle