How browsers remember you, and the rules that decide which websites may call which servers.
Cookies are small pieces of data that a website asks your browser to keep and send back on later requests. CORS (Cross-Origin Resource Sharing) is the set of rules that decides when a web page from one site may read answers from a server on a different site.
HTTP is stateless: each request stands alone. After you log in, the server needs a way to know the next request is still you. It sends a Set-Cookie header. Your browser stores the cookie and adds a Cookie header to every later request to that site.
HTTP/1.1 200 OK Set-Cookie: sid=a1b2c3; Secure; HttpOnly; SameSite=Lax; Path=/; Max-Age=3600 GET /orders HTTP/1.1 Host: shop.example.com Cookie: sid=a1b2c3
An origin is the trio scheme + host + port. https://app.example.com and https://api.example.com are different origins (the host differs). So are http:// and https://, and so are two different ports.
Without a safety rule, any web page you open could call your bank's website using your logged-in cookies and read your balance. So browsers only let JavaScript read responses from the same origin. When a legitimate site needs to share, the server adds CORS headers that say who may read.
| Attribute | What it does | Why it matters |
|---|---|---|
| Secure | Send the cookie only over HTTPS | Stops it from leaking on plain HTTP |
| HttpOnly | JavaScript cannot read it | Limits damage if a script is injected (XSS) |
| SameSite=Strict | Sent only when the request comes from the same site | Strongest defence against CSRF, but a link from another site arrives logged out |
| SameSite=Lax | Also sent on normal top-level link clicks (GET), but not on cross-site POSTs or background requests | A good default for login cookies |
| SameSite=None | Sent on cross-site requests (needs Secure) | Needed for embedded widgets and some cross-site APIs |
| Domain / Path | Which hosts and paths receive the cookie | Keep the scope as narrow as possible |
| Max-Age / Expires | How long it lives. Without them it is a session cookie, which goes away when the browser closes | Persistent cookies stay for days or months |
| Header | Meaning |
|---|---|
| Access-Control-Allow-Origin | Which origin may read the answer: one exact origin, or * for anyone |
| Access-Control-Allow-Methods | Which methods are allowed (used in the preflight answer) |
| Access-Control-Allow-Headers | Which request headers are allowed (such as Authorization or Content-Type) |
| Access-Control-Allow-Credentials | true means cookies and login data may be included. Then Allow-Origin must be one exact origin, never * |
| Access-Control-Max-Age | How long the browser may remember a preflight answer, so it does not ask every time |
Simplified model of the browser rules. The cookie is assumed to belong to the API's host, and the user is logged in. A "site" is scheme plus registrable domain, so a different port is cross-origin but still same-site. Preflight is needed for methods other than GET, HEAD and POST, for custom headers, and for a JSON content type.
HTTP/1.1 204 No Content Access-Control-Allow-Origin: https://app.example.com Access-Control-Allow-Methods: GET, POST, PUT Access-Control-Allow-Headers: Content-Type, Authorization Access-Control-Allow-Credentials: true Access-Control-Max-Age: 600 Vary: Origin
/api on the same host, behind a reverse proxy), there is no CORS to configure at all.| Term | Meaning in easy words |
|---|---|
| Cookie | A small piece of data the browser stores for a site and sends back with later requests |
| Set-Cookie | The response header a server uses to create a cookie |
| Session cookie | A cookie with no expiry, removed when the browser closes |
| HttpOnly | A cookie flag that hides the cookie from JavaScript |
| Secure | A cookie flag that sends it only over HTTPS |
| SameSite | A cookie flag that limits sending the cookie on cross-site requests (Strict, Lax or None) |
| Origin | Scheme + host + port, for example https://app.example.com |
| Same-origin policy | The browser rule that stops a page from reading data from a different origin |
| CORS | Cross-Origin Resource Sharing: headers that let a server allow other origins to read its answers |
| Preflight | An OPTIONS request the browser sends first, to ask if the real request is allowed |
| CSRF | Cross-Site Request Forgery: tricking a logged-in browser into sending an unwanted request |
| Choice | We gain | We lose |
|---|---|---|
| Cookie for login | Automatic, HttpOnly, works with plain page loads | Needs CSRF protection and a server-side session store |
| Token in a header | No automatic sending, so no CSRF by default | JavaScript must store it, which exposes it to XSS |
| SameSite=Strict | Strongest CSRF defence | Users arriving from other sites look logged out at first |
| SameSite=None | Works in cross-site embeds | Weaker CSRF defence and more privacy limits |
| Allow-Origin: * | Easy for public, read-only data | Cannot be used with cookies, and exposes the data to every site |
| Exact origin list | Safe and cookies can work | More configuration to maintain |
| Same-origin proxy (/api) | No CORS at all | Needs a reverse proxy in front |