Answer#
A cookie that carries a session ID is a password for as long as it lives, so each attribute closes a way to steal or abuse it:
| Attribute | What it does |
|---|---|
HttpOnly | JavaScript cannot read the cookie, so a script-injection flaw cannot copy it |
Secure | The browser sends it only over HTTPS |
SameSite=Lax (or Strict) | The browser does not send it on cross-site sub-requests and most cross-site POSTs |
Path=/ and no Domain | Limits the cookie to your own host, not its sub-domains |
Max-Age | Bounds the lifetime in the browser (the server must also expire the session) |
__Host- prefix | Browsers reject the cookie unless it is Secure, has Path=/ and no Domain |
[dependencies]
cookie = "0.18"use cookie::{time::Duration, Cookie, SameSite};
fn main() {
let session = Cookie::build(("__Host-sid", "k3j2h1g0f9e8d7c6"))
.http_only(true) // not readable from JavaScript
.secure(true) // sent over HTTPS only
.same_site(SameSite::Lax) // not sent on cross-site POSTs and sub-requests
.path("/") // required by the __Host- prefix
.max_age(Duration::hours(8)) // bounded lifetime; the server must also expire it
.build();
println!("{session}");
// Logging out: overwrite with an expired cookie with the same name and path.
let mut gone = Cookie::build(("__Host-sid", "")).path("/").secure(true).http_only(true).build();
gone.make_removal();
println!("{gone}");
}__Host-sid=k3j2h1g0f9e8d7c6; HttpOnly; SameSite=Lax; Secure; Path=/; Max-Age=28800
__Host-sid=; HttpOnly; Secure; Path=/; Max-Age=0; Expires=Fri, 05 Dec 2025 09:02:55 GMTThe second line is the logout cookie: the same name and path with an empty value and a date in the past, which tells the browser to delete it. The date shown depends on the day the program runs.
How to do it#
- Build the cookie in one function so every response uses the same attributes.
- Serve the whole site over HTTPS (see the thread on rustls) before setting
Secure. - On logout, expire the cookie in the browser and delete the session on the server.
- Keep the cookie value an opaque random ID; do not put user data in it (see the thread on session IDs).
Common mistakes#
- Setting
Securewhile testing over plain HTTP, then wondering why the browser drops the cookie. Use HTTPS locally or a separate development setting. - A very long lifetime for sensitive applications. Use an idle timeout and an absolute timeout on the server.
- Using
SameSite=Nonewithout a clear need; it re-enables cross-site sending.
Related questions#
Is SameSite enough against CSRF?#
It is a useful layer but not the only one. See the thread on CSRF tokens.
Can I use a cookie for something other than a session?#
Yes, for preferences. Apply the same attributes unless you have a reason not to, and never store secrets or personal data in a cookie.
Sources#
- OWASP Session Management Cheat Sheet, "Cookies": https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#cookies
- MDN, "Set-Cookie": https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie
cookiedocumentation: https://docs.rs/cookie/0.18.2/cookie/