skip to content

In Rails 8.1, what stops a signed remember-me cookie from being replayed after its expiry or pasted into a different cookie name?

level: seniorimportance: should knowfreq 35%

answer

  1. metadata inside the signed message
  2. expires: is embedded, not only sent
  3. purpose is cookie dot name
  4. use_cookies_with_metadata since 6.0 defaults
  5. permanent means twenty years

basics

~20 s

Metadata inside the signed value. With load_defaults 6.0 or later, including 8.1, the signed and encrypted jars embed the expires: time and a purpose of cookie.<name>, so Rails returns nil for an expired value or one copied into another cookie, whatever the browser sends.

solid answer

~40 s

Under `config.action_dispatch.use_cookies_with_metadata`, on since the 6.0 defaults, the signed and encrypted jars wrap the value in an envelope with `exp` (from your `expires:`) and `pur`, set to `cookie.<name>`. On read the verifier checks both: past the expiry, or under a different name, `cookies.signed[...]` returns `nil`. So `cookies.signed[:remember_token] = { value: token, expires: 2.weeks }` is enforced by the server, not only by the browser's `Expires`. `cookies.permanent` sets a 20-year `Expires`, which is why a remember-me token should carry an explicit, shorter `expires:`. The metadata limits a copied cookie's life and where it can be used; it cannot revoke one early, which still needs a server-side token you can delete.

go deeper

for a junior

Recall that signed cookies can carry an expires: the server enforces, and that cookies.permanent means a 20-year expiry.

for a middle

Explain the _rails envelope with exp and pur, how reads check them and return nil, and why expires: belongs on the signed jar for a remember-me token.

for a senior

Show you pair embedded expiry and purpose with a server-side token digest for revocation, and know apps on pre-6.0 defaults lack the metadata.

for a principal

Set the app's remember-me policy: maximum lifetime, revocation path and cookie attributes, and make sure upgrades through load_defaults pick up metadata.

## Two replay tricks A news-reader app signs readers in with `cookies.signed[:remember_token]`. A signed value cannot be **edited**, but two tricks reuse a **valid** one: 1. **Replay after expiry.** The browser deletes the cookie when its `Expires` passes, but someone who copied the value can keep sending it. 2. **Name swapping.** If the app signs other values with the same key, say `cookies.signed[:reader_id]` for analytics, a value lifted from one cookie could be pasted into another whose meaning is more powerful. Rails answers both with **metadata inside the signed message**. ## The envelope With `config.action_dispatch.use_cookies_with_metadata` set, which `load_defaults` 6.0 and every later default set do, the signed and encrypted jars wrap the serialized value as: ```json { "_rails": { "message": "<the serialized value>", "exp": "2026-10-20T09:00:00.000Z", "pur": "cookie.remember_token" } } ``` - **`pur`** is the **purpose**, always `"cookie.<name>"` for the cookie being written. - **`exp`** is filled when you pass `expires:`. A Duration such as `2.weeks` is turned into an absolute time from now, a `Time` is used as given; without `expires:` it is empty and nothing expires. The envelope is then signed (or encrypted), so neither field can be changed without the key. ## What happens on read `cookies.signed[:remember_token]` asks the verifier for purpose `cookie.remember_token`, and the verifier checks: | Check | Fails when | Result | |---|---|---| | Signature | any byte changed | `nil` | | Purpose | the value was written for another cookie name | `nil` | | Expiry | the current time is past `exp` | `nil` | Rails' tests cover each: a signed cookie written with `expires: 2.hours` reads back after one hour and is `nil` after three; a value copied from `user_id` into `discount_percentage` reads as `nil` once metadata is on. ## Choosing the expiry for a remember-me cookie ```ruby cookies.signed[:remember_token] = { value: token, expires: 2.weeks, httponly: true, secure: Rails.env.production? } ``` - **Pass `expires:` on the signed jar itself.** The value you give is what the jar embeds and the browser receives. - **`cookies.permanent`** sets `Expires` to 20 years from now. It suits long-lived preferences, and chaining it with the signed jar (`cookies.permanent.signed[...]`) is common. For a sign-in token, though, twenty years leaves the cookie's whole life to server-side checks. - The permanent jar is **meant only for writing**: read the value back through `cookies.signed[...]` or `cookies[...]`. ## What metadata does not give you - **Early revocation.** An unexpired, correctly named value still verifies. If a reader signs out on every device or the token leaks, only server state helps: store a digest of the token in the database and delete it. - **Protection for plain cookies.** `cookies[...]` values have no envelope. - **Older apps.** On defaults before 6.0, values are signed without `exp` or `pur`, so expired copies and swapped names verify. Turning on `use_cookies_with_metadata` applies to newly written cookies; old ones are still read. ## Seeing the metadata yourself A signed cookie is readable, which makes the envelope easy to inspect in a console. After URL-decoding the cookie value, the part before `--` is Base64: ```ruby value = CGI.unescape(raw_cookie_value) Base64.decode64(value.split("--").first) # => "{"_rails":{"message":...,"exp":"2026-10-20T09:00:00.000Z","pur":"cookie.remember_token"}}" ``` This is the same trick Active Support's `MessageVerifier` documentation shows. It is also a reminder that a signed value hides nothing. ## Putting it together A sound remember-me design in Rails 8.1 uses a random token, a digest of it in a table, the signed jar with an explicit `expires:` and `httponly: true`, and deletion of the table row on sign-out. The metadata then bounds what a copied cookie can do, and the table makes early revocation possible.

  • What does cookies.permanent.signed[:remember_token] = token set?
    A signed cookie with `Expires` 20 years ahead: the permanent jar sets that expiry and the signed jar signs the value. It is read back with `cookies.signed[:remember_token]`, since the permanent jar is meant for writing. For a sign-in token an explicit `expires:` such as `2.weeks` on the signed jar is the safer choice.
  • Why can one signed cookie's value not be pasted into another signed cookie in Rails 8.1?
    Each value is signed together with the purpose `cookie.<name>`. Reading `cookies.signed[:reader_id]` expects purpose `cookie.reader_id`, so a value signed for `remember_token` fails the check and returns `nil`, even though the signature itself is valid.

  • the browser's Expires attribute is the only thing that ends a signed cookie
  • a signed value from one cookie works in any other signed cookie
  • cookies.permanent gives a cookie no expiry at all
  • embedded expiry lets the server revoke a token before it expires
  • use_cookies_with_metadata is off unless an app enables it by hand

Assumes Rails 8.1 with load_defaults 8.1, which includes use_cookies_with_metadata from the 6.0 defaults; apps on older defaults sign values without expiry or purpose.