Understanding CORS: A Friendly Guide to Cross-Origin Resource Sharing
In the vast world of web development, there’s a feature called Cross-Origin Resource Sharing, or CORS for short, that plays a crucial role in keeping our web interactions safe. While CORS can seem a bit technical at first, let's break it down into a story that makes it easier to understand.
The Tale of the Faithful Browser and the Nosy Script
Once upon a time, there was a faithful browser who always tried to do the right thing. This browser was very loyal and preferred not to meddle in other people's business. One day, the browser visited a website hosted by a friendly server, which we'll call server1. Server1 provided the browser with several files, including one particular file named nosy.js.
Now, nosy.js had a habit of being, well, a bit nosy. Shortly after arriving in the browser’s care, nosy.js had a request: “Hey, browser, I need some data from another server called server2. Could you fetch it for me?”
The browser, being the cautious and faithful type, wasn’t too keen on this idea. “I don’t really like fetching data from servers I don’t know,” thought the browser. “I’m loyal to server1, and I certainly don’t want to get into trouble by prying into server2’s business.”
But the browser also didn’t want to outright deny the request. So, the browser decided to ask for permission first. “I’ll send a message to server2,” the browser thought, “to make sure it’s okay before I fetch the data.”
The browser wrote a polite note to server2: “Dear server2, I hope I’m not bothering you, but a script called nosy.js that I received from server1 would like to access one of your JSON files. Is that alright?”
The browser tied this note to a swift and reliable messenger named Preflight, a friendly penguin, and sent Preflight off to server2.
When server2 received the note, it recognized the name server1. “Ah, server1 is a friend of mine,” server2 thought. “I’ve given server1 access to my JSON files before. It’s perfectly fine for the browser to fetch the data for nosy.js.”
So, server2 wrote back to the browser: “Yes, I know server1 well. You have my permission to fetch the JSON file for nosy.js.”
With this permission in hand, the browser felt confident. “Alright, I’ll go ahead and get the data,” the browser decided.
The browser sent the official request to server2, and server2 gladly handed over the JSON file. The browser delivered the data to nosy.js, and everyone was happy. Nosy.js got the data it needed, server2’s boundaries were respected, and the browser stayed true to its cautious and faithful nature.
The Moral of the Story: Understanding CORS
In this story, nosy.js represents a piece of JavaScript on a webpage that wants to request data from a different server (server2) than the one that served the original webpage (server1). The browser, mindful of security, doesn’t want to make such requests unless it’s sure that server2 allows it. The penguin messenger, Preflight, symbolizes the preflight request—a way for the browser to check with server2 if it’s okay to proceed.
CORS is the mechanism that allows server2 to say, “Yes, it’s okay,” and allows the browser to make cross-origin requests safely. It’s not a piece of software or hardware, but a security feature built into web browsers to ensure that requests between different servers are authorized and safe.
So, the next time you hear about CORS, remember the story of the faithful browser, nosy.js, and the penguin messenger, Preflight. It’s all about making sure that data is shared securely across the web, with everyone’s permission.
Subscribe to my newsletter
Read articles from Berthran Daniel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Berthran Daniel
Berthran Daniel
I am full-stack developer niched in the backend of things. My greatest passion is learning and understanding why and how things work. I love and got into tech because it gives me a possibility mentality in approaching challenges I face.