skip to content

In a Rails app on the cookie session store, what do reset_session and expire_after actually guarantee about ending a session?

level: seniorimportance: should knowfreq 35%

answer

  1. new session id, empty hash
  2. CSRF token and flash cleared too
  3. no server copy to delete
  4. expiry embedded in the encrypted cookie
  5. sliding window, refreshed per request

basics

~20 s

reset_session empties the session, issues a new session id, resets the CSRF token and clears the flash, but with the cookie store an old copy of the cookie still decrypts. expire_after both sets the cookie's expiry and embeds it, so Rails rejects older cookies after that time.

solid answer

~40 s

`reset_session` calls the store's `delete_session`, clears the hash, loads a new session id, resets the CSRF token and sets the flash to nil; the response carries a fresh cookie. With `:cookie_store` there is no server record to delete, so a copy of the previous cookie, taken from the browser or a log, still decrypts and restores the old session. `expire_after: 2.weeks` limits that window: the cookie's `Expires` is refreshed on each request, and because the session cookie goes through the encrypted jar, the expiry is also embedded in the encrypted message, so Rails reads an expired cookie as an empty session even if the client keeps sending it. Without `expire_after` the session cookie has neither. For real revocation you need server-side state.

code

ruby · 10 lines
ruby
# config/application.rb
config.session_store :cookie_store, key: "_bakery_session", expire_after: 2.weeks

# app/controllers/checkouts_controller.rb
class CheckoutsController < ApplicationController
  def destroy
    reset_session
    redirect_to root_path, notice: "Basket cleared.", status: :see_other
  end
end

go deeper

for a junior

Recall that reset_session clears the session and starts a new one, and that expire_after makes the session cookie expire after a period of inactivity.

for a middle

Explain what reset_session clears (hash, id, CSRF token, flash), why to set the flash afterwards, and how expire_after both sets Expires and embeds an expiry.

for a senior

Show you know a copied cookie-store session survives reset_session until its embedded expiry, and choose server-side session state when revocation matters.

for a principal

Decide whether client-held sessions are acceptable for the product's risk, and pair idle windows with server-side records where sign-out everywhere is required.

## What reset_session does `reset_session` is an Action Controller method that delegates to the request: 1. `session.destroy` clears the hash and asks the **store** to delete the session (`delete_session`), which returns a **new session id**. 2. The session is reloaded empty so the new id is written with the response. 3. The **CSRF token** is reset, so forms rendered before the reset no longer validate. 4. The **flash** is set to `nil`, so messages assigned earlier in the request are gone. Typical uses are on sign-in, to stop a session id chosen before authentication from carrying over, and on sign-out, so nothing from the old session is left in the new cookie. Because of step 4, set the flash **after** the reset: ```ruby reset_session redirect_to root_path, notice: "Signed out.", status: :see_other ``` ## What it cannot do with the cookie store With a server-side store, `delete_session` removes the stored data, so the old id leads nowhere. With **`:cookie_store`** the session data **is** the cookie. The store's `delete_session` only prepares a new hash with a new id; there is nothing on the server to remove. Consequences: - The browser receives a new cookie and the user's own browser is signed out. - A **copy** of the previous cookie still decrypts and verifies, because it was produced with the same key. Replaying it restores the old session hash. - That remains true until the cookie stops verifying: it expires (if it carries an expiry) or `secret_key_base` changes without a rotation for the old key. This is the core trade-off of a client-held session. It is why designs that need server-side revocation keep a record per session, as Rails' authentication generator does with its own table, or use a server-side store. ## What expire_after does with the cookie store `config.session_store :cookie_store, expire_after: 2.weeks` has two effects: | Effect | Mechanism | |---|---| | The browser drops the cookie after the period | `Expires` is set on the `Set-Cookie` header | | Rails rejects an older copy even if it is replayed | the expiry is embedded in the encrypted message, which fails verification afterwards | | The window slides | the cookie is re-issued with a fresh expiry on each request, even one that does not touch the session | Rails' own tests check both halves: a request three hours in gets a new expiry five hours ahead, and a request after the expiry reads an **empty** session. The embedded expiry comes from the encrypted cookie jar, which adds `expires_at` metadata whenever a cookie is written with `expires:`. Without `expire_after`, the session cookie is a browser-session cookie: no `Expires` attribute and **no embedded expiry**, so a copied cookie verifies for as long as the key is unchanged. ## Guarantees side by side | Situation | `reset_session` | `expire_after` set | |---|---|---| | The user's browser after the call | new, empty session | unchanged until idle too long | | A copied pre-reset cookie, replayed | still valid | valid until its embedded expiry | | An idle user | n/a | session gone after the window | | A user active every day | n/a | never expires (sliding) | `expire_after` is an **idle** timeout, not an absolute one. An absolute lifetime, re-authentication rules and rotation policy are session-lifecycle design questions beyond these two options. ## Practical setup for a bakery pre-order app - Keep the basket id in the session with a sensible idle window: `expire_after: 2.weeks`. - Call `reset_session` at sign-in and sign-out, then set any flash. - If the business needs to sign a customer out everywhere, for example after a password change, use server-side session records instead of relying on the cookie.

  • Why is a notice set before reset_session never shown?
    `reset_session` sets the request's flash to `nil` as well as clearing the session. Anything assigned to `flash` earlier in the request is dropped, so the redirected page has nothing to show. Call `reset_session` first, then pass `notice:` to `redirect_to` or assign the flash.
  • Does expire_after make an active user's session end after the period?
    No. With the cookie store the cookie is re-issued with a fresh expiry on every request, so the period counts from the last request, not from sign-in. A user active every day keeps the session indefinitely; an absolute limit has to be enforced separately.

  • reset_session on the cookie store makes every copy of the old cookie invalid
  • expire_after only sets the browser's Expires, so a replayed cookie still works
  • expire_after ends the session a fixed time after sign-in, however active the user is
  • a flash set before reset_session is still shown after the redirect
  • without expire_after the session cookie expires after 20 years like a permanent cookie