Same-Origin Policy and CORS
Why your fetch fails cross-origin and how CORS allows it
Skills taught: web.same-origin-policy, web.cors, web.preflight, web.cross-origin
What is an 'origin'?
An origin is: protocol + hostname + port
https://kernal.dhurta.org/lessons → origin: https://kernal.dhurta.org
https://kernal.dhurta.org/roadmap → origin: https://kernal.dhurta.org (same)
http://kernal.dhurta.org/ → DIFFERENT (http ≠ https)
https://api.dhurta.org/ → DIFFERENT (different subdomain)
https://kernal.dhurta.org:8080/ → DIFFERENT (different port)
https://otherdomain.com/ → DIFFERENT (different domain)
Two origins are the same only if ALL THREE parts match exactly.
The Same-Origin Policy rule
JavaScript running on one origin can send requests to other origins, but it can only read the response if the server explicitly permits it with CORS headers.
// Running on: https://kernal.dhurta.org
// ✓ ALLOWED — same origin
const data = await fetch('/api/lessons').then(r => r.json());
// ✗ BLOCKED by SOP (unless the API adds CORS headers)
const weather = await fetch('https://api.weather.com/forecast').then(r => r.json());
// → TypeError: Failed to fetch
// → Console: 'Access to fetch at https://api.weather.com/ from origin
// https://kernal.dhurta.org has been blocked by CORS policy:
// No 'Access-Control-Allow-Origin' header is present'
// ✓ ALLOWED — server added CORS header
// Response from api.weather.com includes:
// Access-Control-Allow-Origin: https://kernal.dhurta.org
const weather = await fetch('https://api.weather.com/forecast').then(r => r.json());
CORS: the server's opt-in
Server response to a cross-origin request:
Access-Control-Allow-Origin: https://kernal.dhurta.org
↑ Only kernal.dhurta.org can read this response
Access-Control-Allow-Origin: *
↑ Any origin can read (OK for public APIs, NOT for auth)
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
↑ These HTTP methods are allowed cross-origin
Access-Control-Allow-Headers: Content-Type, Authorization
↑ These request headers are allowed
Access-Control-Allow-Credentials: true
↑ Cookies will be included (requires specific origin, not *)
Preflight requests
For non-simple requests (DELETE, PUT, or any request with JSON body or custom headers), the browser sends a preflight OPTIONS request first to ask the server if the real request is allowed.
// You write:
fetch('https://api.example.com/users/5', {
method: 'DELETE',
headers: { Authorization: 'Bearer token' }
})
// Browser first sends (automatically):
OPTIONS /users/5 HTTP/1.1
Origin: https://yoursite.com
Access-Control-Request-Method: DELETE
Access-Control-Request-Headers: Authorization
// Server must respond:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://yoursite.com
Access-Control-Allow-Methods: DELETE
Access-Control-Allow-Headers: Authorization
// Only then does the browser send the actual DELETE request
Tip: CORS is a server-side configuration. If you get a CORS error, the fix is on the server — add the correct headers. Nothing you do in your frontend JavaScript can bypass SOP (and you should not want to — it protects your users).
Frequently Asked Questions
Why does my server-to-server request work but browser fetch fails?
Same-Origin Policy is a browser security mechanism — it only applies to JavaScript running in a browser. When your Node.js server, curl, or Postman makes a request, there is no browser enforcement. Only browsers enforce SOP. This is why a proxy pattern works: your frontend fetches your own API (/api/proxy), and your server makes the cross-origin request on behalf of the browser — no CORS needed.
Is CORS enough to secure my API?
No. CORS controls which browser origins can read your responses. It does not authenticate users, authorize operations, rate-limit requests, or prevent server-to-server abuse. Think of CORS as browser-level access control only. You still need authentication (tokens, sessions), authorization (who can do what), input validation, and rate limiting.