In a Rails 8.1 app, where does secret_key_base come from, what is derived from it, and what breaks if you replace it after a leak?
answer
- ENV first, then credentials
- tmp/local_secret.txt only in development and test
- key_generator feeds every message verifier
- signed and encrypted cookies, the cookie session
- old value goes to message_verifiers.rotate
basics
~20 ssecret_key_base comes from ENV['SECRET_KEY_BASE'], then credentials, then in development and test tmp/local_secret.txt. It seeds the key generator behind signed and encrypted cookies, cookie sessions and message verifiers, so replacing it invalidates them all unless old values are rotated in.
solid answer
~40 s`Rails.application.secret_key_base` is resolved from `ENV["SECRET_KEY_BASE"]`, then `credentials.secret_key_base`; in development and test only, Rails falls back to a random value saved in `tmp/local_secret.txt`, and since 8.1 it uses the ENV or credentials value locally when one is present. Production raises if none is found. The value seeds `Rails.application.key_generator`, which derives the keys for signed and encrypted cookies (and so the default cookie session), `message_verifiers` such as Active Storage's signed blob ids and Active Record's `signed_id` and `generates_token_for` tokens. Someone holding it can forge those cookies and tokens. Replacing it signs everyone out and breaks outstanding links unless you rotate: verifiers accept `message_verifiers.rotate(secret_key_base: old)` and cookies have their own rotations. Active Record Encryption keys are separate, so encrypted columns are unaffected.
code
bash · 6 lines# Generate a new value
bin/rails secret
# Put it in the production credentials file
bin/rails credentials:edit -e production
# secret_key_base: <paste the new value>go deeper
Recall that secret_key_base lives in credentials, signs cookies, and must never be committed in plain text.
Explain the lookup order, the development-only local file and its 8.1 change, and which features derive keys from it.
Show the response to a leak, what it invalidates, when to rotate instead of cutting over, and what stays untouched.
Weigh rotation cadence against user disruption, and how a team detects and responds to a root-secret exposure.
## What `secret_key_base` is `secret_key_base` is the **root secret** of a Rails application: a long random string from which Rails derives the keys that sign and encrypt data it hands to clients. A new app's `credentials.yml.enc` contains one, generated by `rails new`, and per-environment files for production or custom environments get their own. `bin/rails secret` prints a fresh random value suitable for it. ## Where Rails looks for it `config.secret_key_base` is resolved the first time something asks for it: | Order | Source | Where it applies | |---|---|---| | 1 | `ENV["SECRET_KEY_BASE_DUMMY"]` set: a generated secret in `tmp/local_secret.txt` | build steps such as asset precompilation | | 2 | `ENV["SECRET_KEY_BASE"]` | every environment | | 3 | `Rails.application.credentials.secret_key_base` | every environment | | 4 | a random value saved in `tmp/local_secret.txt` | development and test only | If nothing is found in production, Rails raises `ArgumentError` naming the environment. The Rails 8.1 change is in row 4's place: before 8.1, development and test always used the local file; since 8.1 an ENV or credentials value is used there too when present, so two developers can share signed fixtures or cookies deliberately. ## What is derived from it `Rails.application.key_generator` wraps `secret_key_base` in a PBKDF2 key generator (1000 iterations, cached). Each feature asks it for a key under its own salt, so one root secret yields many independent keys: - **Signed and encrypted cookies**, `cookies.signed` and `cookies.encrypted`, including the authentication generator's `session_id` cookie. - **The cookie session store**, the default, which keeps `session` in an encrypted cookie. - **`Rails.application.message_verifiers`**, used by Active Storage's signed blob ids and redirect URLs, and by Active Record's `signed_id` and `generates_token_for` tokens such as password-reset links. - Your own `Rails.application.message_verifier(:name)` calls. Not derived from it: **Active Record Encryption**, which uses the three keys under `active_record_encryption` in credentials, and the credentials file itself, which uses the master key. ## If it leaks An attacker with `secret_key_base` can compute the same derived keys. In practice that means they can: 1. forge a signed or encrypted cookie, including a session cookie that names any user; 2. read the contents of encrypted cookies and the cookie session; 3. mint valid signed ids, password-reset tokens and signed blob URLs. So a leak is equivalent to losing every session and every signed link at once. Replace it, and treat anything it protected as compromised. ## What replacing it breaks, and how to soften it Changing the value changes every derived key: - every user is signed out, because session cookies no longer verify; - outstanding reset links, signed ids and Active Storage URLs stop working; - signed or encrypted cookies set by the app read as nil. After a **leak**, that is the point: you want old cookies and tokens dead. For a **planned** change, Rails lets old values keep verifying while new ones are issued with the new key: ```ruby # config/initializers/secret_rotation.rb Rails.application.message_verifiers.rotate(secret_key_base: ENV["OLD_SECRET_KEY_BASE"]) ``` Cookie jars have their own rotation settings. Remove the old value once the longest-lived cookie or token has expired. ## Operational notes - Keep it in credentials or a secret store; never commit it in plain text or print it in logs. - Give each environment its own value; a staging value that equals production's makes a staging leak a production leak. - Build steps that only need some value to boot, such as asset precompilation, use `SECRET_KEY_BASE_DUMMY=1` instead of the real secret. ## What interviewers check - That you can name the lookup order and the development-only fallback. - That you know what derives from it and what does not. - That you can explain the trade-off between invalidating everything after a leak and rotating for a planned change.
- Why does rotating secret_key_base not affect columns encrypted with Active Record's encrypts?Active Record Encryption takes its primary key, deterministic key and key derivation salt from the `active_record_encryption` section of credentials, not from `secret_key_base`. The two are separate secrets with separate rotation procedures, so a new `secret_key_base` signs users out but leaves encrypted columns readable.
- A developer sets SECRET_KEY_BASE in their shell and Rails 8.1 stops writing tmp/local_secret.txt; is that a bug?No. Since 8.1, development and test use `ENV["SECRET_KEY_BASE"]` or `credentials.secret_key_base` when present and only generate `tmp/local_secret.txt` when neither is set. Before 8.1 the local file was always used in those environments.
- secret_key_base only matters for the cookie session store.
- Rotating secret_key_base makes encrypted Active Record columns unreadable.
- Production generates tmp/local_secret.txt when secret_key_base is missing.
- A leaked secret_key_base only exposes data already in cookies, not new forgeries.
- Replacing secret_key_base after a leak should keep the old one in rotation for convenience.
Assumes Rails 8.1; since 8.1 an ENV or credentials secret_key_base is used in development and test instead of always generating tmp/local_secret.txt.