News for hackers. Stuff that matters.
Re:CORS is not meant to secure an API endpoint by goto11 on Sunday February 13, 2022 @04:59AM (#30320646)
It is more complex than that, because certain kinds of cross-site requests has always been allowed. GET and POST requests are allowed, but PUT and DELETE is not. For POST requests you can send the request but not access the result. So CORS can be used to increase protection, by disabling cross-site POST requests, but it can also be used to decrease protection for other requests.
Re:CORS is not meant to secure an API endpoint by iancarroll on Sunday February 13, 2022 @05:14AM (#30320696)
This isn't fully accurate -- you can never prevent cross-site POST requests from being initiated, with or without CORS. For example, CORS is never involved in requests from <form> submissions (which can be triggered via JavaScript).

Really CORS cannot be used to lock anything down. The behavior of a server not implementing CORS has the same end result as a server trying to be as restrictive as possible. Both would not send any CORS headers in responses and would simply ignore pre-flight requests.

Re:CORS is not meant to secure an API endpoint by goto11 on Sunday February 13, 2022 @08:43AM (#30321718)
> Really CORS cannot be used to lock anything down.

Lets say you have a legitimate website with uses form POST's or GET's to implement cross-site requests to some service on a different domain, authenticated through a cookie. This is vulnerable to cross-site request forging. Changing this to use fetch with CORS can protect against this kind of attack, since the service can now ensure the request is initiated from a legitimate origin.

It is just important to understand what CORS protect against and what it doesn't protect against. You can always forge a request to look exactly like a CORS-compliant request from a legitimate origin - but you still wouldn't have the necessary authentication cookie. If the attacker have somehow gotten access to the users cookie, then there is no need for request-forging in the first place, the attacker can just directly log into the service. If the users browser is compromised it is the same. So CORS protect against the specific scenario where a malicious site visited by a legitimate user forges a request to a legitimate site, misusing the authentication cookie associated with the legitimate domain. In this scenario, the CORS header will indicate the origin of the malicious site, and the server will be able to reject the request.

Re:CORS is not meant to secure an API endpoint by Xylakant on Sunday February 13, 2022 @09:24AM (#30321902)
AFAIR it’s never the server that rejects the request in a CORS scenario: The server indicates what origins are allowed to call the API and the browser cancels the request on behalf of the user if called on a website that’s not on the allowed list. The server canceling the request based on a header would be insecure since the client can send arbitrary headers.
Re:CORS is not meant to secure an API endpoint by goto11 on Sunday February 13, 2022 @09:30AM (#30321936)
JavaScript in an uncompromized browser cannot send arbitrary headers. See: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_...

Otherwise a malicious site could just forge the Origin header in the preflight request.

Re:CORS is not meant to secure an API endpoint by iancarroll on Sunday February 13, 2022 @09:44AM (#30322007)
This is true, but there have been a lot of browser bugs in the past with Origin/Referer headers. Relying on those is not as foolproof as a CSRF token, which would require a more severe UXSS-type issue to leak. I wouldn’t advise it.
Re:CORS is not meant to secure an API endpoint by hoppla on Sunday February 13, 2022 @06:26AM (#30320972)
Form Posts are limited to few content types, and application/json is not one of them. I have however found some CSRFs against POST endpoints that did not care that the request was text/plain
Re:CORS is not meant to secure an API endpoint by mmis1000 on Sunday February 13, 2022 @11:26AM (#30322843)
> Really CORS cannot be used to lock anything down.

yep, that's what Cross-Origin Resource Policy (CORP) for. It is designed to seal the last loop hole in same-origin policy. (img and scripts don't require cors at all) But there is a big gotcha, the old browser that don't know about it simply don't care. So it isn't strictly useful for now.

Re:CORS is not meant to secure an API endpoint by jrockway on Sunday February 13, 2022 @05:48AM (#30320831)
Yup. CORS has so many loopholes that you should pretend like it doesn't exist.

My favorite is... imagine I routinely port-forward an app's debug port to my workstation for debugging. It has a useful /[email protected] endpoint. You can then call that over the Internet by serving me a web page that contains <img src="http://localhost:1234/email-debug-info?address=example@examp...">. CORS doesn't care. My browser doesn't care. It will just silently leak information. (Also great for other things on your network. Log into your router at 192.168.1.1 recently? Someone can make a web page with a form that submits to http://192.168.1.1/enable-port-forwarding or whatever, and you can forward whatever ports you want.)

What's hilarious to me is that CORS seems burdensome in the opposite direction too. It breaks peoples applications somehow! While writing this comment, I did a search to see if I could remember the standards-track proposal for fixing the two "bugs" I mention above... but all the search results are people asking how to disable CORS rules because their app is broken. Sigh! The web is a mess.

Re:CORS is not meant to secure an API endpoint by dividuum on Sunday February 13, 2022 @05:55AM (#30320849)
> My browser doesn't care.

It soon will https://web.dev/cors-rfc1918-feedback/

Re:CORS is not meant to secure an API endpoint by JanSt on Sunday February 13, 2022 @06:28AM (#30320983)
Apart from the misunderstanding that CORS protects you, you are describing two scenarios.

I don't really understand the first one or why you think SOP should protect you in that case. Could you restate that problem?

The second one is a CSRF issue, as noted by another user.

The third thing you describe is not people who disable CORS but actually use CORS to the max (usally things like Access-Control-Allow-Origin: *), because they don't understand what it does. It's a huge problem...

Re:CORS is not meant to secure an API endpoint by Mavvie on Sunday February 13, 2022 @10:52AM (#30322542)
As mentioned, CORS isn't designed to protect against requests where the response isn't read by another origin's JS. Even with fetch I think you could "bypass" CORS to the same extent as this example by passing the "no-cors" mode.

This is just a CSRF issue, which is well understood and easily fixed with a CSRF/authenticity token.

Also, GET requests should not have side effects like sending an email. A reasonable default seen in some frameworks is GET requests have no side effects and don't require CSRF tokens, while all other verbs do.

Re:CORS is not meant to secure an API endpoint by accept-apply on Sunday February 13, 2022 @06:04AM (#30320888)
Isn't this a CSRF issue?
Re:CORS is not meant to secure an API endpoint by masklinn on Sunday February 13, 2022 @11:25AM (#30322828)
> It is more complex than that, because certain kinds of cross-site requests has always been allowed. GET and POST requests are allowed

Request headers and body content-type are also a factor for POST, anything which couldn't be set through a FORM is forbidden.

The most common issue is that the request is only simple if its content-type is `application/x-www-form-urlencoded`, `multipart/form-data`, or `text/plain`. JSON POST requests usually run afoul that. The second big issue is setting bespoke headers as only Accept, Accept-Language, Content-Language, and Content-Type (restricted to the above list) are simple. There common sticking points are headers like Authorization.

Re:CORS is not meant to secure an API endpoint by JanSt on Sunday February 13, 2022 @05:09AM (#30320684)
The request does not send resource/cookie data if CORS does not explicitly relax the SOP. CORS does not protect!
Re:CORS is not meant to secure an API endpoint by goto11 on Sunday February 13, 2022 @08:26AM (#30321607)
A cross-origin POST request can be initiated using a HTML form and does send cookies. CORS provides a safer alternative.
Re:CORS is not meant to secure an API endpoint by aobdev on Sunday February 13, 2022 @12:01PM (#30323219)
Can you elaborate on how CORS provides an alternative? Responding with a CORS policy to your own client’s preflight requests is not going to do anything for a malicious client’s form POST.