Keep SSO identity blocks across a SECRET_KEY_BASE change and finish the rotation guide
Closes #3233
Most of #3233 already landed with #3190, #3221 and #4936 (docs/hosting/encryption.md, security:backfill_encryption with the ACTIVE_RECORD_ENCRYPTION_PREVIOUS_* variables, security:rotate_setting_secrets). This PR closes the remaining gaps.
Problem
- A key rotation silently lifts SSO identity blocks. When a super admin permanently removes a user,
SsoIdentityBlockstores an HMAC of the IdP subject id, keyed withRails.application.key_generator, which is derived fromSECRET_KEY_BASE. The rotation guide tells operators to changeSECRET_KEY_BASE. After that, no stored digest matches, so every block is gone and a removed user can sign in or be re-created via SSO again (web callback, account linking/JIT, mobile SSO). The subject id itself is not stored, so the digests cannot be recomputed. Reproduced onmainwith a test that stubs a new key generator. - Only one public
SECRET_KEY_BASEis detected.KNOWN_COMPROMISED_SECRET_KEY_BASESonly knew the old compose default. These are just as public and trigger no warning:secret-valuefrom.env.example, the literal$(openssl rand -hex 64)that the Hetzner guide writes into.env(Compose does not run it), and the placeholders in the Docker, PetHost and Helm guides. - The rotation guide was missing how to find the old value once
compose.ymlhas been replaced, what a newSECRET_KEY_BASEinvalidates, and how to roll back.
Changes
SsoIdentityBlock: new blocks store the subject id with Active Record encryption (encrypts :uid, deterministic: true). The rotation backfill re-encrypts it like every other column, so blocks no longer depend onSECRET_KEY_BASE. Without Active Record encryption the model keeps writing digests as before. For one release new blocks also keep writinguid_digest, so the previous release (an image rollback, or old processes during a rolling deploy) still sees them; lookups prefer the encrypted uid. The digest column can be dropped in a later release.- Migration
MoveSsoIdentityBlocksToActiveRecordEncryptionaddsuid(unique per provider) andlegacy_digest_key. Every existing digest row gets the HMAC key it was made with, stored encrypted, so legacy blocks keep matching after aSECRET_KEY_BASEchange.downrestores digests for new rows. It raisesIrreversibleMigrationinstead of lifting blocks whenSECRET_KEY_BASEchanged sinceupor the encryption keys are missing. - Lookup: the encrypted uid first, then legacy digests, with one decryption of the shared stored key. An unreadable stored key is logged and skipped, so it cannot turn every SSO login into a 500.
BACKFILL_MANIFEST:sso_identity_blocksnow also coversuidandlegacy_digest_key.CURRENT_BACKFILL_VERSIONgoes from 10 to 11 with its digest.KNOWN_COMPROMISED_SECRET_KEY_BASES: five more public values. The boot warnings and the settings warning (en, id, zh-CN) now say "publicly known value" instead of "compose default".docs/hosting/encryption.md: finding the old value withdocker inspect(works without a Compose file and on a stopped container, unlikedocker compose exec ... printenv), what a newSECRET_KEY_BASEinvalidates (sessions, reset and confirmation links, bill calendar links, connection flows in progress), and a rollback section.
Design note
The subject id is now stored encrypted (recoverable with the Active Record keys, like emails and tokens) instead of as a one-way digest. A keyed digest would need a key of its own, and any key derived from SECRET_KEY_BASE breaks on rotation. Keeping everything on Active Record encryption means one key set, one rotation path and one backfill.
Notes for reviewers
- Bumping
CURRENT_BACKFILL_VERSIONturns the plaintext fallback back on for installs that already completed a backfill, until they rerunbin/rails security:backfill_encryption. The manifest-digest test requires the bump. #3833 also bumps the version, so whichever PR lands second takes the next number. - Known limitations: digest rows written while Active Record encryption is not configured still depend on
SECRET_KEY_BASE, as before. WhileACTIVE_RECORD_ENCRYPTION_PREVIOUS_*are set, Rails writes and queries deterministic attributes with the oldest scheme, so encrypted lookups (emails, and now blocks) miss rows the backfill already moved. The guide and the boot log already say to unset them before starting the app. - #3787 also edits
docs/hosting/docker.md. This PR only changes one sentence there.
Validation
- New tests: model (encrypted path, legacy rows with and without a stored key, survival of a
SECRET_KEY_BASEchange, unreadable stored key, path without encryption), migration (upkeeps legacy blocks across a key change,downkeeps new blocks,downrefuses after a key change,down/uprestores the schema), detection of the public values. The rotation tests fail onmain. - Related suites green: SSO blocks, migration, encryption config, backfill, admin, sessions, OIDC accounts, API auth, OIDC identity, security settings.
- Full suite green apart from environment-only AI/proxy tests. Rubocop clean, Brakeman without warnings.
- Every place that checks a block goes through
SsoIdentityBlock.blocked?/find_block: web SSO callback, account linking/JIT, mobile SSO, identity creation, removal, demo refresh. Admin unblock and the backfill were checked as well.
🤖 Generated with Claude Code
Summary by cubic
Keeps SSO identity blocks of permanently removed users intact across a SECRET_KEY_BASE rotation, so removed users can no longer silently regain access when operators follow the rotation guide. Also warns about more publicly known SECRET_KEY_BASE values and finishes the rotation guide.
- New blocks store the IdP subject id encrypted with Active Record encryption and still write the legacy digest, so the previous release (after an image rollback or during a rolling deploy) keeps seeing them; legacy digest rows get the HMAC key they were made with, stored encrypted, so they keep matching after a key change.
- Each stored legacy key is read on its own: if no block matches while some key is unreadable,
blocked?fails closed, so one corrupt row cannot hide blocks under readable keys or let a removed user log in. - Rolling back after a
SECRET_KEY_BASEchange, or when a stored subject id or key does not decrypt, raisesIrreversibleMigrationinstead of lifting blocks;downrecomputes digests for encrypted rows. - Adds five more publicly known
SECRET_KEY_BASEvalues (.env.example, hosting guide placeholders) to the boot and settings warnings. - Extends
docs/hosting/encryption.mdwith how to recover the old value from a stopped container or Rails credentials, what a new key invalidates (sessions, reset links, bill calendar links, in-progress connection flows), a restart before rollback, rollback steps that restore the keys matching the chosen backup, and rotating provider API keys and tokens when the old key was publicly known or leaked. - Bumps
CURRENT_BACKFILL_VERSIONto 13 (main shipped 11 for the Enable Banking IBAN); installs that already backfilled must rerunbin/rails security:backfill_encryptionto cover the new fields.
Migration
- Requires a
SECRET_KEY_BASEchange to happen together withsecurity:backfill_encryption; installs without Active Record encryption keys keep writing digests as before.
Summary by CodeRabbit
- Security
- SSO identity blocks now store encrypted subject identifiers when encryption is available, while retaining support for legacy records and environments without encryption. Blocks remain matchable after secret-key rotation, and duplicate blocks are avoided.
- Security warnings now cover more publicly known secrets and placeholders.
- Documentation
- Expanded encryption key rotation guidance with Docker troubleshooting, details about affected data, backup precautions, and rollback steps.