skip to content

A Rails invoice page shows the signed-in user's name and uses fresh_when @invoice; how can a stale or wrong page be served, and how does a class-level etag fix it?

level: seniorimportance: should knowfreq 28%

answer

  1. the ETag only knows the invoice
  2. same browser, different user
  3. etag { Current.user&.id }
  4. template digest and flash come free
  5. public: true widens the blast radius

basics

~20 s

fresh_when @invoice builds its ETag from the invoice, template digest and flash only, so a browser that cached one user's page gets a 304 after another user signs in; a class-level etag { Current.user&.id } adds the viewer to every ETag.

solid answer

~40 s

`fresh_when @invoice` digests the invoice's `cache_key_with_version`, plus whatever the controller's `etag` blocks return: in `ActionController::Base` that is the template digest and the flash when present. Nothing about the viewer is in it. If one user views the invoice, signs out, and another user signs in on the same browser, the browser revalidates with the old ETag, gets `304`, and shows a page rendered for the first user. With `public: true`, a shared cache could hand that page to anyone. The fix is a controller-level `etag` block, `etag { Current.user&.id }`, which `ConditionalGet` stores in the `etaggers` class attribute and runs for every `fresh_when` and `stale?` in that controller and its subclasses, so each user gets a distinct ETag. Personal pages should also stay `private`, the default.

code

ruby · 5 lines
ruby
class ApplicationController < ActionController::Base
  # Every fresh_when / stale? in the app varies by viewer and locale
  etag { Current.user&.id }
  etag { I18n.locale }
end

go deeper

for a junior

Remember that a Rails ETag only covers what you give it, and that etag { Current.user&.id } at the top of a controller adds the user to it.

for a middle

Explain the inputs: the record's cache key and version, the template digest and flash added by ActionController::Base, and any class-level etag blocks run through instance_exec.

for a senior

Spot validators that miss viewer, locale or permissions, keep personal pages private, and choose no_store over conditional GET where storing the page is itself the risk.

for a principal

Set app-wide ETag inputs in the base controller and a policy for which pages may ever be public, so per-user correctness does not depend on each action remembering it.

## What the ETag is built from When an action calls `fresh_when @invoice`, Rails builds the ETag from a list of validators: 1. the argument, `@invoice`, expanded to its `cache_key_with_version`; 2. the result of each block registered with the class method **`etag`**, run in the controller instance. `ActionController::Base` registers two blocks for you: - **`EtagWithTemplateDigest`** adds the digest of the action's template, so a deploy that changes `show.html.erb` changes the ETag; `self.etag_with_template_digest = false` turns it off. - **`EtagWithFlash`** adds the flash when it is not empty, so a page carrying a one-time notice is not answered with a 304 that would show the old notice or none. The list is digested into one weak ETag. Anything not in that list can change without changing the ETag. ## How the wrong page gets served Take an invoice page whose header says "Signed in as Ana". The ETag knows the invoice version, the template and the flash. It does not know who is looking. 1. Ana opens `/invoices/7`; the browser stores the page and its ETag. 2. Ana signs out; Ben signs in on the same browser and opens `/invoices/7` (he may share access to the account). 3. The browser sends `If-None-Match` with Ana's ETag. 4. The invoice, template and flash are unchanged, so `fresh_when` answers `304`. 5. The browser shows its stored copy: Ben sees "Signed in as Ana". With the default `private` Cache-Control, the damage is limited to one browser. With **`fresh_when @invoice, public: true`**, a shared proxy or CDN may store the page and give it to other users, which turns a display glitch into a data leak. ## The fix: a class-level `etag` ```ruby class InvoicesController < ApplicationController etag { Current.user&.id } def show @invoice = Invoice.find(params[:id]) fresh_when @invoice end end ``` - `etag` is a class method of `ActionController::ConditionalGet`; each call appends a block to the **`etaggers`** class attribute. - Every `fresh_when` and `stale?` in this controller, and in subclasses, runs all blocks with `instance_exec`, so they can call anything the controller can, such as `Current.user` from the authentication generator or Devise's `current_user`. - Blocks returning `nil` are dropped, so `Current.user&.id` is safe for signed-out visitors. - Declared in `ApplicationController`, the block applies to the whole app. Now Ben's request computes a different ETag and gets a fresh 200. ## What else belongs in an etag block Anything that changes the page but not the record: - the viewer, or their role when pages differ by permission; - the locale, when the page is translated; - a feature flag or layout version that changes markup; - for API controllers, which have no template digest, a representation version you bump when the JSON shape changes. Keep the blocks cheap: they run on every conditional request. ## Checklist for personal pages | Concern | Rails setting | |---|---| | Viewer-specific content | `etag { Current.user&.id }` | | Shared caches must not store it | keep the default `private`; never `public: true` | | Must not be stored at all | `no_store` instead of `fresh_when` | | Template changes after deploy | built in via the template digest | | One-time notices | built in via the flash | ## What the interviewer listens for That the candidate knows the ETag only reflects what was put into it, can name the built-in inputs, and reaches for the controller-level `etag` block, not a hand-built ETag string in every action.

  • Why does a page with a flash notice not get a 304 even when the record is unchanged?
    `ActionController::Base` includes `EtagWithFlash`, which registers `etag { flash if request.respond_to?(:flash) && !flash.empty? }`. A non-empty flash becomes part of the ETag, so it differs from the stored one and the page is rendered with the notice.
  • When is adding the user to the ETag not enough?
    When the page must not be stored at all, for example it shows bank details on a shared computer. An ETag only controls revalidation; a stored copy can still be shown from history or a cache. Use `no_store` for such pages instead of `fresh_when`.

  • fresh_when @invoice automatically varies the ETag by the signed-in user.
  • public: true is harmless on per-user pages because ETags differ anyway.
  • A class-level etag block runs only in the action that declares it.
  • The flash and template are never part of a Rails ETag.
  • A 304 sends the server's current page, so it cannot show the wrong user.