News for hackers. Stuff that matters.
Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @05:30AM (#32909563)
If you serve your static files from a CDN it's simply not possible to do so.

It's a very common case.

Re:Cache your CORS by zmxz on Tuesday September 20, 2022 @07:10AM (#32910189)
Can you provide an example, just so we can be on the same page? You do an XmlHttpRequest or fetch() to a static asset and it's a non-trivial request to CDN, I just wonder how it looks like and why it exists in the first place.
Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @07:59AM (#32910510)
Say that you want to serve your API from the same origin as your static files (HTML, CSS, JS, etc.) but you also want said static files to be served by a CDN.

Basically that won't work besides making your CDN somehow proxy requests for which not static file exists.

Re:Cache your CORS by chrisweekly on Tuesday September 20, 2022 @09:26AM (#32911351)
It's an extremely common scenario. The obvious and straightforward solution is to configure path-based cache rules exactly like you describe, to proxy requests which don't correspond to static assets. Cloudfront allows you to do this out of the box, as do other major CDN providers. If your CDN doesn't support this, consider switching - or introduce an edge proxy to facilitate it.
Re:Cache your CORS by snowwrestler on Tuesday September 20, 2022 @08:22AM (#32910698)
We use Fastly (the Hosts feature) to do exactly this. Basically it routes requests to different backends based on the URL path. If it starts with /assets, the backend is S3. If it starts with /api, the backend is our application. If it starts with /blog, the backend is Wordpress. All on different hosting platforms.

(In case it’s not clear, Fastly is also caching and serving these requests as a CDN.)

Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @08:59AM (#32911035)
So that would indicate that you don't do end-to-end TLS on your infrastructure for the API which means that Fastly man-in-the-middles your API.

In a lot of sensitive businesses that wouldn't be allowed.

Re:Cache your CORS by snowwrestler on Tuesday September 20, 2022 @10:44AM (#32912331)
Are you sure about that? Have you talked to a good lawyer about it?
Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @03:56PM (#32916944)
This is not necessarily only a legal issue it's also an information security issue.

You can't guarantee the integrity and privacy of the full request and response chain.

This goes against various security standards and ISOs.

You might be able to get away with it and trust the CDN but that's an awful lot of trust.

Re:Cache your CORS by snowwrestler on Friday September 23, 2022 @09:21AM (#32951565)
Unless you are terminating TLS entirely on owned hardware, you are paying a 3rd party to manage TLS for you.

A lot of people seem to think that there is a big difference between paying a lessor (e.g. Hetzner) for a server on which you terminate TLS, paying a cloud host (e.g. Amazon) to terminate TLS, and paying a CDN (e.g. Fastly) to terminate TLS. Legally there is no difference aside from the specific language of the contracts, which you can review and negotiate in advance.

The difference security-wise is entirely down to the operations of each company, which again you can review and discuss in advance. Strictly speaking a CDN should have lower risk than a host since they are not persisting sensitive data (if you set your cache headers correctly). And as discussed above, using one domain helps avoids cross-domain security concerns.

Re:Cache your CORS by icedchai on Tuesday September 20, 2022 @05:18PM (#32917930)
You're putting a lot of trust in your CDN, anyway. If your CDN gets hacked, what's stopping your frontend code from being updated to send your API requests somewhere else? Maybe they get rerouted to a proxy, then back to you...
Re:Cache your CORS by zmxz on Tuesday September 20, 2022 @09:07AM (#32911127)
Serving said JS on CDN implies that the JS performs xhr/fetch, which means I have control over said CDN

Given the fact I have control over domain(s), I'd have http://domain.tld/api for API and serve static js from http://domain.tld/static/*.js

Given the fact I have the need for CDN, it means I've got enough traffic that justifies the bill incurred

Re:Cache your CORS by tomjakubowski on Tuesday September 20, 2022 @05:38PM (#32918141)
> you want to serve your API from the same origin as your static files (HTML, CSS, JS, etc.)

What is the benefit or purpose of doing this?

Re:Cache your CORS by icedchai on Tuesday September 20, 2022 @06:27PM (#32918633)
You don't have CORS configuration to worry about. It simplifies development and deployment.
Re:Cache your CORS by tomjakubowski on Tuesday September 20, 2022 @09:18PM (#32919871)
You don't need to set up CORS to load assets on a page from a CDN's origin. Cross-origin taint only matters when trying to read asset data from a script.

I think we may be talking past each other and one of us is misunderstanding OP.

Re:Cache your CORS by icedchai on Tuesday September 20, 2022 @10:16PM (#32920164)
Yes, but you need it if your API isn't on the same origin as your static assets.
Re:Cache your CORS by tomjakubowski on Tuesday September 20, 2022 @05:36PM (#32918125)
Only if you need to read those assets from Javascript with a cross-domain request… right? (nb: you can always insert cross-origin assets to web page, with e.g. a video or img tag, you just can't read the data from JS)

Say you stand up a website with an API at https://example.com. You host your static assets on the CDN, at https://example.myfastcdn.com/. So example.com's home page looks like:

    <head>
      <script src="https://example.myfastcdn.com/app.js"></script>
    </head>
    <body>
      <div id="site-root"></div><!-- app.js renders some application here -->
    </body>
The origin when you load your site is https://example.com, so the scripts hosted on the CDN can still make API requests to https://example.com.

What am I missing here?

Re:Cache your CORS by tylerhou on Tuesday September 20, 2022 @05:44AM (#32909670)
IIRC Cloudflare has page rules, which makes this possible? In fact, with page rules, Cloudflare can proxy a subpath (like /api) to a completely different domain.
Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @09:03AM (#32911076)
As I mentioned above this doesn't work for all CDNs and also involves trusting your CDN to MitM your API
Re:Cache your CORS by zmxz on Tuesday September 20, 2022 @10:24AM (#32912057)
This kind of comment falls under what I posted originally - people making up reasons why they can't go with proxy solution.
Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @03:54PM (#32916933)
I don't think that security requirements are a "made up" restriction.

It's like saying that a house built without a lock is a made up issue and that no lock pr door is needed.

Re:Cache your CORS by zmxz on Tuesday September 20, 2022 @04:24PM (#32917290)
They're not, but you're blatantly refusing to read what's being written.

Public CDN should never be trusted. If you use a CDN in the first place and have strict security requirements, then you create your own private CDN. And if you can control that private CDN, you have all the ingredients to avoid CORS.

It's really that simple. No one is saying you are wrong, but you're refusing to look at the entire picture and you focus only on a subset, in which - of course - your argument works.

Re:Cache your CORS by LunaSea on Tuesday September 20, 2022 @05:17PM (#32917909)
So your point is that there is no reason to not serve everything behind the same origin, it only requires setting up a full fledged CDN to do so.

I'm sorry but that's simply not an acceptable constraint.

Re:Cache your CORS by zmxz on Thursday September 22, 2022 @05:34AM (#32936631)
I'm sorry that we ended up discussing this because all you did was invent situations and argued with people who didn't even state any of what you managed to read.

No one is telling you not to deal with CORS your way. Fact of the matter is that you can avoid it, but you're making up reasons why you can't. The only reason you can't is because you won't. You're free to use whatever approach you like, there's no police here, just don't state that I or anyone else wrote what we didn't. It'd be grown up thing to do. Thanks and best of success with your projects.