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

Small Scale Industries in India (SSI)

small-scale-industries-in-india-ssi
Small Scale Industries hold an important position in India, in terms of contributing to the country’s economy. As per the Ministry of Statistics and Programme Implementation, Government of India, the…

Faking Min Width on a Table Column

Faking Min Width on a Table Column
The good ol’ <table> tag is the most semantic HTML for showing tabular data. But I find it very hard to control how the table is presented, particularly column widths…

4 Qualities Your Business Website Must Have

4-qualities-your-business-website-must-have
As most business owners know, having a strong digital presence is vital in today’s digital era. There are various ways in which you can strengthen your digital presence and impact,…

Customer Engagement Tips For Retail Businesses

customer-engagement-tips-for-retail-businesses
Customer-Engagement-Tips-For-Retail-Businesses In today’s time, customers’ only want is to meet their requirements. It is the anticipation of the customers that you would anticipate and meet their demands within, or before…