Cookies, Sessions, and the Browser Same-Origin Policy
🎯 Introduction
By the end of this article, you will be able to explain how browsers send login credentials and why pages cannot read some cross-origin responses.
🍪 Browsers store cookies and send them according to rules
A server can use a response header to ask the browser to store a cookie. For example, on an HTTPS site:
Set-Cookie: sid=random-session-credential; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=1800
sid is the name, and the random value identifies a session. Path=/ allows use across paths on that host; Max-Age=1800 stores it for 1,800 seconds. Subsequent requests that meet the conditions include:
Cookie: sid=random-session-credential
This means the browser supplied a credential; it does not mean the server has accepted the login as valid. The server must still check whether the session exists, has expired, or has been revoked.
🗃 Sessions store login state on the server
A session is a login record stored on the server. For example, a random credential can identify user 1, an expiration time, and a login device. The cookie usually holds only the credential used to find this record, rather than the entire user profile.
After a successful login, the server creates the record and sets the cookie. On subsequent requests, it uses the cookie's value to find the record and identify the current user. On logout, the server deletes or invalidates the record and asks the browser to clear the cookie.
Set-Cookie: sid=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0
Deletion must match the original cookie's name, Path, and Domain scope. Clearing user information from the page alone does not invalidate the server session.
🛡 What each cookie attribute protects
| Attribute | Purpose | Checks it cannot replace |
|---|---|---|
| HttpOnly | Prevents page scripts from reading the cookie directly | Does not stop the browser from sending requests on the user's behalf |
| Secure | Sends the cookie only over a secure connection | Does not establish that the user has permission |
| SameSite=Lax | Restricts cookies on requests initiated from other sites; following a link from another site will generally still include them | Does not replace complete request forgery protection |
| Max-Age | Controls browser storage duration | The server must still check session expiration |
Omitting Domain normally creates a host-only cookie. Do not expose login cookies to all subdomains for convenience. Cookies are not isolated by port, so their rules differ from the same-origin rules for different ports on one host.
🌐 The same origin requires the same scheme, host, and port
The browser's same-origin policy restricts data access between pages from different origins. For a page at https://app.example.com:
| Request URL | Same origin? | Reason |
|---|---|---|
| https://app.example.com/api/users | Yes | Only the path changes |
| https://api.example.com/users | No | Different host |
| http://app.example.com/users | No | Different scheme |
| https://app.example.com:8443/users | No | Different port |
A cross-origin request is not necessarily blocked from being sent. Some requests reach the server, but the browser prevents the initiating page from reading the response. Others first send a preflight request to decide whether to send the actual request. The server grants read access through CORS response headers; configuration is covered in the cross-origin handling article.
For example, https://app.example.com and https://api.example.com have different origins, so reading the API response requires cross-origin permission. They generally belong to the same site, however, and whether a cookie is sent also depends on its domain and attributes.
❓ Questions and Answers
Q1: Can cookies store only login information?
No. They can also store preferences. Login cookies are sensitive credentials, though, so restrict their scope and configure appropriate attributes.
Q2: Is it normal to see an HttpOnly cookie in the Network panel?
Yes. HttpOnly restricts access by page scripts; it does not hide data from the person using the browser.
Q3: Do different frontend and backend ports mean different sites?
They mean different origins, but cookie scope and SameSite rules depend on more than the port. Check cross-origin read access and cookie transmission separately.
🧪 Practice
Compare http://localhost:5173 and http://localhost:3000. State whether they share an origin and explain why that alone cannot determine whether cookies are sent.
// Write your code or record your verification results here
