FAQ - Help and questions

Which attributes should a session cookie have, and how do I set them in Rust?

#security#cookies#sessions#webpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

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.

Checked with rustc 1.93.0, cookie 0.18.2, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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:

AttributeWhat it does
HttpOnlyJavaScript cannot read the cookie, so a script-injection flaw cannot copy it
SecureThe 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 DomainLimits the cookie to your own host, not its sub-domains
Max-AgeBounds the lifetime in the browser (the server must also expire the session)
__Host- prefixBrowsers reject the cookie unless it is Secure, has Path=/ and no Domain
Cargo.tomltoml
[dependencies]
cookie = "0.18"
src/main.rsrust
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.

Is SameSite enough against CSRF?#

It is a useful layer but not the only one. See the thread on CSRF tokens.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet