Cookies, sessions and the same-origin policy
Unit 2 of 5 in Web fundamentals.
You will be able to explain how cookies make sessions, which cookie settings protect them, and what the same-origin policy limits.
Lesson
Cookies add memory to HTTP
RFC 6265 defines the Cookie and Set-Cookie header fields, which servers use to store state at the user agent, called cookies, so that servers can maintain a stateful session over the mostly stateless HTTP protocol. The standard adds that cookies have many historical weaknesses that harm security and privacy, yet are widely used.
When you log in, the server typically creates a session and sends a cookie holding a session identifier. Your browser sends the cookie back on each request, and that identifier is all that links you to your logged-in state. Anyone who gets it can act as you, which is why it needs protection.
Settings that protect a session cookie
The Secure attribute limits the cookie to secure channels, so the browser includes it in a request only if the request goes over something like HTTP over TLS. The standard notes it protects only the cookie's confidentiality. The HttpOnly attribute tells the user agent to omit the cookie when scripts ask for cookies through browser APIs, so a script that runs on your page cannot read it.
OWASP adds the SameSite attribute, which controls whether browsers send a cookie with cross-site requests. It advises that session cookies explicitly set SameSite to Strict, which it prefers, or Lax, and to treat it as defense in depth against cross-site request forgery, not as a replacement for a CSRF token.
The browser's main rule: same origin
The same-origin policy is a critical browser security mechanism that restricts how a document or script loaded from one origin can interact with a resource from another. MDN gives the reason: it stops a malicious site from running script in your browser that reads data from a webmail service or company intranet you are signed in to.
Two addresses have the same origin if their protocol, port and host are all the same. Only the path may differ. This is why a page on one site cannot simply read another site's responses, and why features that relax the rule, such as cross-origin resource sharing, must be configured with care.
A session lifecycle to aim for
OWASP's session guidance covers the properties of session identifiers such as entropy and length, built-in session management implementations, exchanging identifiers over TLS in cookies with the attributes above, renewing the identifier after a privilege level change such as logging in, automatic expiry through idle and absolute timeouts, and manual expiry through a logout. The practical rule for a builder: use your framework's session management and turn those features on.
Key points
- Cookies carry a session identifier; whoever has it is treated as the user.
- Secure, HttpOnly and SameSite each close a different gap.
- Same origin means the same protocol, host and port.
- Use the framework's session management and expire sessions.
Sources
- RFC 6265: HTTP State Management Mechanism
- OWASP Session Management Cheat Sheet
- MDN: Same-origin policy
Practice in the labs
Harden a session cookie on a practice app and fix a cross-site request weakness in a lab you run yourself.
- Harden session cookies and stop session fixation: You set the cookie attributes and fix how sessions are issued, in a practice app on your own machine.
- Exploit and fix CSRF (Cross-Site Request Forgery) in your own lab: You see a cross-site request weakness in your own lab app and add the fix, which shows what SameSite and tokens are for.
Knowledge check
- What does the HttpOnly cookie attribute do? Answer: Stops browser scripts from reading the cookie. RFC 6265: HttpOnly instructs the user agent to omit the cookie when providing access to cookies through non-HTTP APIs such as those exposed to scripts.
- Which two URLs have the same origin? Answer: http://shop.example/a and http://shop.example/b. Same origin means the same protocol, port and host; only the path may differ (MDN).
- Is SameSite a replacement for a CSRF token? Answer: No, OWASP treats it as defense in depth in addition to a token. OWASP advises treating SameSite as defense in depth against CSRF, not as a replacement for a CSRF token.
Back to the syllabus or see all classes.