TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to CORS

๐Ÿšง CORS: cheat sheet

๐ŸŽจ The analogyA careful parent who won't pass a stranger's note the reply unless the office sends a permission slip.
๐ŸŽ“ The regular explanationBrowsers stop pages reading responses from other origins (the same-origin policy). CORS is how a server opts in, with Access-Control-Allow-* headers and, for riskier requests, an OPTIONS preflight.
The same-origin ruleStep 3 of 3
๐ŸฆนStranger's noteCORS error๐Ÿง‘โ€๐ŸผCareful parent๐ŸฆYour bank๐Ÿ“„Bank's own note
  1. A page from a stranger asks the browser to fetch your bank details. Your bank would recognise you, because the browser carries your cookies.
  2. The careful parent won't pass the answer to a stranger's note. It goes in the bin.
  3. A note from the bank's own family is fine. Same home, same rules.
The permission slipStep 3 of 3
๐ŸชShop's note๐Ÿง‘โ€๐ŸผCareful parentchecks headersโญReview office
  1. The shop's page wants reviews from a different office. The parent adds a label saying who's asking.
  2. The office sends the reviews with a permission slip, "shop.com may read this". The parent hands them over.
  3. The rule is for the parent, not the office. The office decides who gets a slip, and the parent checks it.
Asking before the big requestStep 3 of 3
๐ŸชShop's note๐Ÿง‘โ€๐ŸผCareful parentcached 1 day๐ŸขShop office
  1. This note wants to change things, not just read. The careful parent rings the office first, "would you accept this?"
  2. The office replies, "yes, from shop.com, for these kinds of changes, and you needn't ask again today".
  3. Only now does the parent send the real request, and the answer goes back to the note.

๐Ÿงฉ Key questions

Why is the browser a careful parent?
Because it carries your cookies, and a stranger's page could ask it to fetch your bank details. It will happily fetch things, but won't hand the answer to a page from somewhere else without permission.
The office replies without a permission slip naming the page. What does the parent do?
Keeps the answer from the page. No slip, no answer. The office may still have received the note.
For notes that want to change things, what does the parent do first?
Rings the office to ask whether it accepts this kind of request from this page. Only after a yes does the real request go.