๐ JavaScript Same-Origin Policy and Cross-Origin Communication

Imagine youโre at Yaba Tech library in Lagos. You borrow a locker to keep your books safe. The rule says:
- Only your locker key can open your locker.
- You cannot use your key to open someone elseโs locker.
This rule keeps peopleโs belongings separate and secure.
On the web, browsers use a similar rule called the Same-Origin Policy (SOP). Itโs one of the most important security features in the internet world.
๐ What is the Same-Origin Policy (SOP)?
- The Same-Origin Policy is a security rule in browsers.
- It says: A web page can only access data from the same origin (domain, protocol, and port) that it was loaded from.
๐ In simple terms:
- A page from
https://mysite.comcannot freely access data fromhttps://othersite.com.
๐ What is an โOriginโ?
An origin is defined by 3 parts:
- Protocol โ
http://orhttps:// - Domain (host) โ e.g.,
example.com - Port โ e.g.,
:80,:443
So:
https://example.com:443โ one originhttp://example.com:80โ different origin (because protocol differs)https://sub.example.com:443โ different origin (because domain differs)
โก Why Do We Need SOP?
Think of SOP as a security wall. Without it:
- If youโre logged into your bank at
bank.com, a malicious page atevil.comcould steal your bank balance directly. - SOP prevents
evil.comfrom reaching intobank.comโs locker.
๐ What SOP Restricts
By default, one origin cannot:
- Read cookies or localStorage from another origin.
- Make AJAX requests and read responses from another origin.
- Access the DOM of a page from another origin (e.g., an
<iframe>).
๐ ๏ธ When Cross-Origin Communication is Needed
But sometimes, you actually need to share data across origins. Example:
- Your site
shop.comwants to use an API frompayments.com. - Or your web app loads fonts, videos, or images from a CDN (
cdn.com).
Thatโs where Cross-Origin Communication comes in.
๐ Techniques for Cross-Origin Communication
1. CORS (Cross-Origin Resource Sharing)
-
The server at
api.example.comcan set special headers like:Access-Control-Allow-Origin: https://mysite.com - This tells the browser: โItโs okay, let
mysite.comaccess me.โ - Without CORS, browsers block the request.
2. JSONP (Old Way)
- Before CORS, developers used JSONP: loading cross-domain data by injecting a
<script>tag. -
Example:
<script src="https://api.example.com/data?callback=myFunction"></script> - This was clever but insecure, and today itโs mostly replaced by CORS.
3. PostMessage (for iframes & windows)
-
If your site uses an
<iframe>or a popup from another origin, you can communicate safely with:otherWindow.postMessage("Hello", "https://trusted.com"); -
And the receiving window listens with:
window.addEventListener("message", event => { console.log("Got message:", event.data); }); -
This way, you can exchange messages between different origins securely.
4. Proxy Servers
- Your site can send requests to its own server, and the server forwards them to the external API.
-
Example:
- Browser โ
mysite.com/apiโ Server โothersite.com
- Browser โ
- This way, the browser only talks to the same origin, and the server handles cross-origin logic.
โ ๏ธ Security Considerations
- CORS must be configured carefully โ never allow
Access-Control-Allow-Origin: *for sensitive data. - PostMessage should always check
event.originto ensure the message comes from a trusted site. - JSONP is dangerous and rarely recommended today.
- SOP itself is not a weakness โ itโs a protection. The challenge is: developers must use the right tools when cross-origin communication is actually needed.
โ Summary in Simple Words
- Same-Origin Policy (SOP): A rule that stops one site from messing with anotherโs data.
- An origin = protocol + domain + port.
- By default, SOP blocks cross-origin access for safety.
- When cross-origin communication is needed, developers use CORS, PostMessage, or Proxy servers.
- Security is all about balance: protect users but allow safe sharing when required.
๐ Review โ Fill in the Gaps
- SOP stands for Same ______ Policy.
- An origin is defined by protocol, domain, and ______.
- By default, a page from one origin cannot read another siteโs ______ or localStorage.
- SOP prevents malicious sites from stealing your data, like your ______ balance.
- To allow safe cross-origin requests, servers use ______ headers.
- The modern standard for cross-origin communication is ______ (not JSONP).
- JSONP worked by adding a
<script>tag but is considered ______ today. - To send messages between windows or iframes, developers use the ______ API.
- A server can act as a ______ to forward requests to another origin.
- Developers must carefully check the origin in
postMessageto avoid ______ attacks.