News for hackers. Stuff that matters.
Re:CORS is not meant to secure an API endpoint by JimDabell on Sunday February 13, 2022 @04:52AM (#30320613)
> CORS is an implementation in the browser and is designed to protect the user from malicious applications by ensuring that the resource in the browser is only allowed to access specific endpoints.

> This browser implementation can be bypassed at any time. First, it is up to the browser itself: if CORS is not integrated, or not integrated cleanly, then it will not work.

> An attacker can access the API key via the source code of the web app and use it, for example, via a cURL request to directly access the API resources of the backend. With cURL, no CORS takes effect, so the attacker has direct access with the full rights of the user.

All of this is completely wrong.

CORS does not protect anything from anyone. The same-origin policy stops code from one site reading resources from another site. CORS selectively removes that protection – it decreases security.

If CORS is not integrated, the same-origin policy is in full effect and code from one site cannot read resources from your site. It starts off as secure, and stays secure.

If you use cURL, then the same-origin policy doesn’t apply, and you can of course read any resource.

The same-origin policy is not a generic access barrier, and shouldn’t be used as such. It stops one site from abusing the user’s authenticated state with other sites, and that’s it.

Re:CORS is not meant to secure an API endpoint by fivea on Sunday February 13, 2022 @07:40AM (#30321338)
> CORS does not protect anything from anyone. The same-origin policy stops code from one site reading resources from another site. CORS selectively removes that protection – it decreases security.

This comment comes off as either disingenuous or needlessly contrarian, and in the process tries to make points that fall somewhere between completely wrong and miopic.

Your personal assertion that CORS somehow decreases security is based on the patently false assertion that at any point in time requests only came from the same origin. Not only was this never the case, this ignores the fact that with the popularity of REST and SPAs and microservices and API-as-a-service, the norm was since switched to having browsers make requests to anything other than the same origin.

So, without CORS, you have a all-or-nothing security model, where the needle would always pend to the "nothing" side. With CORS, that needle can point "all that matters", which is pretty close to the optimal same-site solution. What CORS does is allow developers to abstract away the definition of "same origin" to mean "the origins that I explicitly allow".

You simply cannot claim that allowing only requests to the origins you explicitly allow is an erosion of a security model. That makes no sense. It made no sense when implicitly that list was only comprised of your own domain, it makes no sense now when that list includes reputable APIs that you pay to use.

Re:CORS is not meant to secure an API endpoint by JimDabell on Sunday February 13, 2022 @10:13AM (#30322173)
The set of all requests possible with CORS is a superset of all requests possible without CORS. The entire purpose of CORS is to allow more requests. By definition, it removes security barriers and opens things up.

> So, without CORS, you have a all-or-nothing security model, where the needle would always pend to the "nothing" side. With CORS, that needle can point "all that matters"

Yes, and a policy that permits nothing is more secure than a policy that permits some things. Although “nothing” is not accurate, some requests are still permitted in either case.

> You simply cannot claim that allowing only requests to the origins you explicitly allow is an erosion of a security model.

I didn't claim that. “Allowing only requests to the origins you explicitly allow” implies that you are enforcing a restriction, when in fact the opposite is happening – you are permitting more by removing restrictions. Tightly scoping what you open up with CORS is still removing restrictions, not adding them.

Re:CORS is not meant to secure an API endpoint by Dylan16807 on Sunday February 13, 2022 @03:18PM (#30325203)
> Yes, and a policy that permits nothing is

You're reading that backwards. "nothing" is "no security".

Re:CORS is not meant to secure an API endpoint by hn_throwaway_99 on Sunday February 13, 2022 @09:59AM (#30322088)
> This comment comes off as either disingenuous or needlessly contrarian, and in the process tries to make points that fall somewhere between completely wrong and miopic.

Bluntly, your response is the one coming off as needlessly argumentative, and the parent comment made plenty of sense to me. I don't think it's disingenuous to clearly explain the difference between the Same-Origin Policy and CORS, and to emphasize that CORS relaxes the Same-Origin Policy.

Re:CORS is not meant to secure an API endpoint by pshc on Sunday February 13, 2022 @11:09AM (#30322688)
Regardless of their tone, fivea is correct to point this out as myopic.

The grandparent comment argues about semantics and provides information that is technically correct, but not in a useful sense. To say that "CORS decreases security" because it opens up cross origin communication is perhaps not disingenuous (who can say?) but certainly misleading. All browser users would be worse off without CORS.

I think we are arriving at the word 'security' with different starting points and lenses, and a different chunking of what constitutes CORS. So I too am guilty of making a semantic argument.

Re:CORS is not meant to secure an API endpoint by unclebucknasty on Sunday February 13, 2022 @12:59PM (#30323837)
>All browser users would be worse off without CORS.

You and fivea are conflating functionality with security.

Yes, it may be functionally desirable for an app to allow cross-origin requests, as enabled by CORS. But, you're granting permissions, and that is opening things up, possibly making them less secure.

I think great-grandparent's point highlighting this is worth making, given that there is so much confusion around CORS.

Re:CORS is not meant to secure an API endpoint by TheCoelacanth on Sunday February 13, 2022 @02:27PM (#30324745)
The three pillars of security are confidentiality, integrity and availability.

Letting people have access to things that they are supposed to have access to is part of security. You don't get perfect security just by denying everyone access to everything.

Re:CORS is not meant to secure an API endpoint by unclebucknasty on Sunday February 13, 2022 @06:59PM (#30326915)
Agreed.
Re:CORS is not meant to secure an API endpoint by Dylan16807 on Sunday February 13, 2022 @03:22PM (#30325251)
> You and fivea are conflating functionality with security.

fivea wasn't conflating functionality and security. They were saying that a world without CORS would be a world where those rights are always granted.

Re:CORS is not meant to secure an API endpoint by unclebucknasty on Monday February 14, 2022 @11:26AM (#30333921)
>a world without CORS would be a world where those rights are always granted.

Or a world where those rights are never granted.

Which of those you conclude is a matter of semantics, and this interpretability contributes to the confusion around what CORS actually does. It's that confusion which another commenter was addressing [0] and that my comments sought to support/clarify. You may be unaware that some people believe that CORS is intended to "lock things down" vs. "open them up". They don't understand that it's opt-in or the default behavior without it.

But, it's a matter of fact that usage of CORS presents risks vs the default policy, and those risks must be considered. That's really the point vs the idea that CORS has no utility.

It's worth noting too that, strictly speaking, there are frequently workarounds to the default policy that don't require CORS, some of which are arguably more secure by way of being less prone to configuration errors.

[0] https://news.ycombinator.com/item?id=30320613

Re:CORS is not meant to secure an API endpoint by Dylan16807 on Monday February 14, 2022 @11:43AM (#30334141)
> Which of those you conclude is a matter of semantics, and this interpretability contributes to the confusion around what CORS actually does. It's that confusion which another commenter was addressing [0] and that my comments sought to support/clarify. You may be unaware that some people believe that CORS is intended to "lock things down" vs. "open them up". They don't understand that it's opt-in or the default behavior without it.

Okay, sure, there's a semantics question and also some people are just confused.

So here is where I took issue: Yes, many confused people are conflating functionality and security. But while fivea/pshc's words might accidentally encourage that confusion, they were not confused, and were not conflating the two.

Re:CORS is not meant to secure an API endpoint by unclebucknasty on Monday February 14, 2022 @03:06PM (#30337529)
>while fivea/pshc's words might accidentally encourage that confusion, they were not confused, and were not conflating the two.

Probably more semantics. I don't claim to know what's in fivea/pshc's heads or whether they themselves are confused. I was speaking to my observation that their comments merged the two issues.

You may prefer phrasing like, "their words might accidentally encourage confusion" versus "conflation". I'd say conflation is the mechanism there, but OK.

Or you might prefer I specifically clarify "their comments conflated the two" vs "they conflated the two".

OK.

Re:CORS is not meant to secure an API endpoint by Dylan16807 on Monday February 14, 2022 @04:05PM (#30338346)
Here's how I see the difference: They made a definite distinction between the two, but if someone read too fast they might miss the distinction.

So their comments did not conflate, but might accidentally cause future conflation, so it's reasonable to reply to make the difference extra clear, but I don't think it's reasonable to accuse them of conflation.

Re:CORS is not meant to secure an API endpoint by unclebucknasty on Monday February 14, 2022 @05:05PM (#30339037)
Then it's not merely semantics. We legitimately disagree.

It happens. Thanks for clarifying.

Re:CORS is not meant to secure an API endpoint by JimDabell on Monday February 14, 2022 @10:16PM (#30342294)
> a world without CORS would be a world where those rights are always granted.

We’ve already seen a world without CORS, and that didn’t happen. Cross-origin restrictions were brought in almost immediately after the introduction of JavaScript and existed for over a decade before CORS was introduced.

Re:CORS is not meant to secure an API endpoint by JanSt on Sunday February 13, 2022 @01:03PM (#30323894)
I disagree. Just read the article „cors protects us“ - things like these come from misunderstandings like this one.
Re:CORS is not meant to secure an API endpoint by blurker on Sunday February 13, 2022 @12:54PM (#30323773)
Actually, I agree with OP. Grandparent's phrasing:

> All of this is completely wrong.

Is not only needlessly argumentative but also wrong itself. The 3rd point they include as "completely wrong" is not wrong, and they end up just rephrasing it later in their correction.

Re:CORS is not meant to secure an API endpoint by JimDabell on Monday February 14, 2022 @10:21PM (#30342328)
> The 3rd point they include as "completely wrong" is not wrong

It is:

> With cURL, no CORS takes effect, so the attacker has direct access with the full rights of the user.

The attacker has direct access with the full rights of the user because this is not a situation where one origin is making a request for a resource from another origin, so there’s nothing that says this shouldn’t happen. It’s got absolutely nothing to do with CORS at all.

Re:CORS is not meant to secure an API endpoint by vinnymac on Sunday February 13, 2022 @10:33AM (#30322309)
You and OP make a good point. In that most people I’ve come across don’t understand how the Same-Origin Policy and Cross-Origin Resource Sharing differ and relate.

I believe it’s important developers learn the nuances, otherwise we will likely see more faulty implementations.

Re:CORS is not meant to secure an API endpoint by patrickthebold on Sunday February 13, 2022 @12:25PM (#30323458)
I think the confusing is in "adding CORS". I think you mean "Adding CORS to the browser standards", and gp means "Adding some kind of CORS interceptor thing in my backend".

It's great that CORS is a thing in browsers, and you can use it if you need. But each person who uses it is allowing more origins, and so allows more requests.

Personally I always avoid CORS and just server every thing I need from the same origin, but that's just because of the kinds of things I've worked on.

Re:CORS is not meant to secure an API endpoint by iqanq on Sunday February 13, 2022 @07:51AM (#30321390)
If you have a lock with no keys, it is secure, since it can't be opened. As soon as you have one key, the lock is less secure, because now the lock can be opened.
Re:CORS is not meant to secure an API endpoint by fivea on Sunday February 13, 2022 @08:15AM (#30321553)
> If you have a lock with no keys, it is secure, since it can't be opened. As soon as you have one key, the lock is less secure, because now the lock can be opened.

No, not really. Your simplistic example fails to acknowledge that your same-site "lock with no keys" model rendered same-origin-policy completely unusable in projects that fall beyond the scope of a personal hobby project, which left the whole world with no alternative to turn it off. CORS recognizes the absurdity of implicitly assuming that the same origin is the only possible and conceivable safe origin, or even that a site has a single origin.

Therefore, your comparison would actually be between an old lock which was no longer usable and thus forced everyone to go around with no locks, or a lock designed around one of the world's most basic requirements and thus made it possible for the old lock concept to be applicable.

Re:CORS is not meant to secure an API endpoint by JanSt on Sunday February 13, 2022 @01:18PM (#30324061)
We are not arguing about the usefulness of CORS, of course it is useful. It does not protect you though.
Re:CORS is not meant to secure an API endpoint by pshc on Sunday February 13, 2022 @08:18AM (#30321568)
If you have a lock with no keys, it's a non-starter; the door has no purpose. You'd be better off sealing the entrance with bricks.

If you offer a website with an API then CORS is an enhancement in that you're helping protect users from getting owned or hijacked. Alternatively if you can't stomach the risk, you could take down your API, which is about as useful as solving a math equation by multiplying both sides by zero.

Re:CORS is not meant to secure an API endpoint by casion on Sunday February 13, 2022 @10:08AM (#30322139)
"Lock with no keys" isn't particularly uncommon. They exist in the real world as one-way devices.
Re:CORS is not meant to secure an API endpoint by iakh on Sunday February 13, 2022 @10:44AM (#30322433)
There's a while saying dedicated to it, "lock them up and throw away the key".
Re:CORS is not meant to secure an API endpoint by pshc on Sunday February 13, 2022 @10:50AM (#30322521)
Hahah, alright. I'll grant you that. The lock:CORS metaphor was tenuous at inception, and now it is truly vaporized.
Re:CORS is not meant to secure an API endpoint by Dylan16807 on Monday February 14, 2022 @11:50AM (#30334235)
Notice how nobody ever does that with an actual lock. It's a metaphor for never letting them go free.
Re:CORS is not meant to secure an API endpoint by eurasiantiger on Sunday February 13, 2022 @08:16AM (#30321560)
A lock with no keys can still be opened with a lockpick or just brute force.
Re:CORS is not meant to secure an API endpoint by djm_ on Sunday February 13, 2022 @06:29AM (#30320993)
From experience, I can tell you that many people simply refer to this entire domain as CORS despite that S standing for Sharing. The Same Origin Policy is treated verbally more like the default state of CORS in some circles.

It is very confusing and I’m not entirely sure how it ended up like that.

Re:CORS is not meant to secure an API endpoint by politelemon on Sunday February 13, 2022 @07:00AM (#30321146)
It's a common for protocols, mechanisms and policies to be confused in terms of intention, and often to be misunderstood. SameSite was another example recently, a lot of people don't understand that Site and Origin have specific meanings in the browser world.

From my own time observing the process of how these things get drafted up, it's because the creators of these mechanisms work in a committee and in a circle in which everyone is highly familiar with their specific terminology. There is no thought given to accessibility of general understanding for 'the masses' and that eventually manifests itself in this way. I'm not saying they should or shouldn't be giving thought to naming, just pointing out what I observe.

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.
Re:CORS is not meant to secure an API endpoint by winwiz on Sunday February 13, 2022 @06:13AM (#30320928)
I agree, the author doesn't seem to grasp the fact that CORS always waters down security. That is, CORS is the way of security weakening. Although the author correctly notes that only browsers respect CORS, there is no mention that this happens because CORS relaxes security built into browsers.
Re:CORS is not meant to secure an API endpoint by glintik on Sunday February 13, 2022 @10:48AM (#30322500)
Of course. Author just missed how CORS works :).