# Which attributes should a session cookie have, and how do I set them in Rust?
Source: https://ruststudio.org/community/t/92/which-attributes-should-a-session-cookie-have-and-how-do-i-set-them-in-rust
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, cookie 0.18.2, Windows 11
Category: Help and questions | Kind: FAQ | Tags: security, cookies, sessions, web | Views: 0

## Short answer
Set HttpOnly so scripts cannot read it, Secure so it travels only over HTTPS, SameSite=Lax or Strict, an explicit Path and a limited lifetime. The __Host- name prefix makes browsers insist on Secure, Path=/ and no Domain. The cookie crate builds all of this and also writes the logout cookie.

## Answer
## 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` |

```toml title=Cargo.toml
[dependencies]
cookie = "0.18"
```

```rust title=src/main.rs
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}");
}
```

```text
__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 GMT
```

The 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
1. Build the cookie in one function so every response uses the same attributes.
2. Serve the whole site over HTTPS (see the thread on rustls) before setting `Secure`.
3. On logout, expire the cookie in the browser **and** delete the session on the server.
4. Keep the cookie value an opaque random ID; do not put user data in it (see the thread on session IDs).

## Common mistakes
- Setting `Secure` while 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=None` without 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
- `cookie` documentation: https://docs.rs/cookie/0.18.2/cookie/
