A Rails 8.1 app deployed without RAILS_MASTER_KEY reads every credential as nil instead of failing; why, and what does config.require_master_key change?
answer
- missing key reads as empty YAML
- require_master_key defaults to false
- EncryptedFile::MissingKeyError when true
- Missing secret_key_base for 'production'
- wrong key: MessageEncryptor::InvalidMessage
basics
~10 sWith no key, Rails treats the credentials as an empty file, so every lookup returns nil until something like secret_key_base raises. config.require_master_key = true makes the first credentials read raise ActiveSupport::EncryptedFile::MissingKeyError instead.
solid answer
~40 s`EncryptedFile#key` returns nil when neither `RAILS_MASTER_KEY` nor the key file exists, and `read` then raises `MissingContentError`, which `EncryptedConfiguration#read` rescues and turns into an empty string. So `credentials.dig(:maps, :api_key)` is nil and the app keeps going until something needs a value: in production, `secret_key_base` finds no credential and raises `ArgumentError` "Missing `secret_key_base` for 'production' environment". With `config.require_master_key = true` (default false; the generated `production.rb` does not mention it), the missing key raises `ActiveSupport::EncryptedFile::MissingKeyError` on the first credentials read, with a message telling you to set `RAILS_MASTER_KEY`. A **wrong** key is different: decryption raises `ActiveSupport::MessageEncryptor::InvalidMessage` whatever the setting. Pair it with bang accessors so each required secret also fails loudly.
code
ruby · 8 lines# config/environments/production.rb
Rails.application.configure do
config.require_master_key = true
end
# config/initializers/maps.rb
# Runs at boot: no key -> MissingKeyError, blank value -> KeyError
MapsClient.api_key = Rails.application.credentials.maps.api_key!go deeper
Recall that a server needs RAILS_MASTER_KEY or the key file, and that missing credentials read as nil.
Explain the empty-configuration fallback, the Missing secret_key_base error, and what require_master_key raises instead.
Distinguish missing key, wrong key and missing value from the error alone, and design boot-time checks that stop a broken deploy.
Decide which secrets justify refusing to boot versus degrading a feature, and how that policy is enforced across services.
## The silent path `Rails.application.credentials` is built with `raise_if_missing_key: config.require_master_key`. When Rails reads it: 1. `EncryptedFile#key` tries `ENV["RAILS_MASTER_KEY"]`, then the file at `config.credentials.key_path`. 2. If both are missing and `raise_if_missing_key` is false, `key` returns nil. 3. `EncryptedFile#read` sees no key and raises `MissingContentError`. 4. `EncryptedConfiguration#read` rescues that error and returns `""`, so the configuration is an empty hash. Nothing is logged. For a food-delivery app, `Rails.application.credentials.dig(:maps, :api_key)` returns nil, the mapping client is configured with nil, and the first sign of trouble is a 401 from the mapping provider at runtime. ## Where it eventually breaks `config.secret_key_base` is resolved lazily, the first time something asks for it, from: 1. `ENV["SECRET_KEY_BASE"]`; 2. `Rails.application.credentials.secret_key_base`; 3. in development and test only, a generated secret. In production with no key, steps 1 and 2 give nil and step 3 does not apply, so the setter raises `ArgumentError`: ``Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit` ``. Because cookies, sessions and message verifiers all need that value, this surfaces early, but it names the wrong problem: the secret exists, the key to read it does not. And if `SECRET_KEY_BASE` is supplied separately, the app runs with no credentials at all. ## What `require_master_key` changes `config.require_master_key` defaults to **false**, and the `production.rb` that Rails 8.1 generates does not set it. With it set to true: - a missing key makes `EncryptedFile#key` raise `ActiveSupport::EncryptedFile::MissingKeyError`; - the message reads "Missing encryption key to decrypt file with. Ask your team for your master key and write it to config/master.key or put it in the ENV['RAILS_MASTER_KEY']." (the path is whichever key path applies); - the error surfaces on the first read of credentials, so put a required read in an initializer if you want the process to refuse to boot. ```ruby # config/environments/production.rb config.require_master_key = true ``` ## Three different failures | Situation | `require_master_key = false` | `require_master_key = true` | |---|---|---| | no key at all | every value nil; later `Missing secret_key_base` | `MissingKeyError` on first read | | wrong key | `ActiveSupport::MessageEncryptor::InvalidMessage` | the same | | key present, value missing | nil from `dig`, `KeyError` from a bang accessor | the same | | invalid YAML | `EncryptedConfiguration::InvalidContentError` | the same | A key of the wrong length raises `ActiveSupport::EncryptedFile::InvalidKeyLengthError` when encrypting, which you meet in `credentials:edit` rather than at runtime. ## Failing fast per secret `require_master_key` only proves a key exists. A required value can still be missing from the file you are reading, for instance because it went into the global file while production reads `production.yml.enc`. Read required secrets with the bang accessor, so a missing value stops the process with a `KeyError` that names it: - `Rails.application.credentials.maps.api_key!` - `Rails.application.credentials.fetch(:maps)` for a whole section ## Checking a deploy - `bin/rails credentials:show -e production` on a machine with the key confirms the file decrypts; it exits non-zero and says the key is missing when it is. - A smoke test that boots the app with production settings and reads one required credential catches all three failures before traffic arrives. ## What interviewers check - That you know a missing key is silent by default and why. - That you can name `require_master_key` and the error it raises. - That you distinguish a missing key from a wrong key and from a missing value.
- The deploy log shows ActiveSupport::MessageEncryptor::InvalidMessage instead of MissingKeyError; what does that tell you?A key was found, so the variable or file exists, but it does not decrypt this file. Usually `RAILS_MASTER_KEY` holds `master.key` while the server reads `config/credentials/production.yml.enc`, or the key belongs to another app. `require_master_key` cannot help; supply the key for the file this environment reads.
- Why can an app with SECRET_KEY_BASE set in the environment still run with every credential nil?`secret_key_base` checks `ENV["SECRET_KEY_BASE"]` before credentials, so it never needs the encrypted file. With no master key and `require_master_key` false, credentials read as empty and nothing else complains until code uses a value. Bang accessors or `require_master_key = true` expose it.
- Rails refuses to boot in production whenever the master key is missing.
- require_master_key = true also catches a wrong master key.
- A missing key raises MissingContentError in application code.
- Setting SECRET_KEY_BASE removes the need for the master key entirely.
- credentials.dig raises when the value is absent, so no extra checks are needed.