Technology News

Don’t Snore on CORS

Whatever, I just needed a title. Everyone’s favorite web security feature has crossed my desk a bunch of times lately and I always feel like that is a sign I should write something because that’s what blogging is.

The main problem with CORS is that developers don’t understand CORS. The basic concept of it is supposed to be easy: don’t run code across origins. Meaning if I, at css-tricks.com, try to fetch some JavaScript from an external URL, like any-other-website.com, the browser will just stop it by default. You’ll see an error in the console. Not allowed.

Unless, that is, the other website sends a header that specifically allows this. My domain can be whitelisted or there could be a wildcard that allows it. There is way more detail here (like preflighting and credentials) and, as ever, the MDN article does a good job on that front.

What have traditionally been hair-pulling moments for me are when CORS seems to behave inconsistently. Two requests will go through and a third will fail, which seems inexplicable, but was reproducible. (Perhaps there was a load balancer involved with half-cached headers? Who knows.) Or I’m trying to use a proxy and the proxy stops working. I can’t even remember all the examples, but I bet I’ve been in meetings trying to debug CORS issues over 100 times in my life.

Anyway, those times where CORS have crossed my desk recently:

  • This video, Learn CORS In 6 Minutes, has 10,000 likes and seems to have struck a chord with folks. A non-ironic npm install cors was the solution here.
  • You have to literally tell servers to have the correct headers. So, similar to the video above, I had to do that in a video about Cloudflare Workers, where I used cross-origin (but you don’t have to, which is actually a very cool feature of Cloudflare Workers).
  • Jake’s article “How to win at CORS” which includes a playground.
  • There are browser extensions (like ones for Firefox and Chrome) that yank in CORS headers for you, which feels like a questionable workaround, but I wouldn’t blame anybody for using in development.
  • I wrote about how easy it is to proxy… anything, including a third-party JavaScript file and make it first-party. Plenty of people pointed out in the comments that doing that totally removes the protection you get from CORS, which is danger-danger. Agreed, unless you 100% control that third-party, it’s quite dangerous.
The chip shortage has a silver lining
Search Abandonment Costs Retailers. Here’s How AI Can Help.

Related Articles

War in the time of crypto

war-in-the-time-of-crypto
In times of crisis, there is no good; there’s only a best course of action, given the circumstances. Is crypto good in the context of Russia’s invasion of Ukraine? Is…

Demystifying TypeScript Discriminated Unions

demystifying-typescript-discriminated-unions
TypeScript is a wonderful tool for writing JavaScript that scales. It’s more or less the de facto standard for the web when it comes to large JavaScript projects. As outstanding…

How To Leverage Your CRM For Better Productivity

how-to-leverage-your-crm-for-better-productivity
The use of customer relationship software (CRM) is bringing great changes in the corporate world. Every brand is struggling to gain maximum benefits with its smart use. However, many newly…

Nonprofit Advocacy in 2021: 4 Best Practices

nonprofit-advocacy-in-2021-4-best-practices
Advocacy is a common way for nonprofits to show their stance on relevant public issues in order to advance their mission, increase their fundraising, and deepen relationships with supporters. Nonprofits…