security: close remaining plaintext-storage gaps found while auditing #3142
Bottom of a stack: #3190 → #3833 → #4945. This merge with main also sets the rotation's previous schemes on ActiveRecord::Encryption.config directly: the backfill-completion check from #3221 snapshots the engine config before that line runs, so setting only the Rails config left a key rotation without any previous scheme.
Why a separate PR
#3189 fixes the encryption_ready? gating bug itself. While auditing every place that bug could touch (per the issue thread, security deserves a thorough pass, not just the two spots originally reported), I found several related but distinct gaps that fixing the gating check alone does not close. Splitting these out so the critical fix (#3189) can be reviewed and merged fast on its own.
What's in this PR
- Three models had no encryption wired up at all (not a gating bug — no
encryptscall existed in the first place):IndexaCapitalAccount,SophtronAccount,OnchainWalletAccountnow encrypt their raw provider payloads, matching every structurally identical sibling*Accountmodel. IndexaCapitalItem#username/#document(a login-credential pair used by one of its two auth modes —documentis very likely a national ID/tax document number for KYC) had noencryptscall. Now encrypted.- Eight
Itemmodels encrypted their credentials but notraw_payload/raw_institution_payload, unlike every sibling provider that does: Brex, Coinbase, Binance, CoinStats, Mercury, IndexaCapital, Sophtron, SnapTrade. lib/tasks/security_backfill.rake(the task operators run to re-encrypt data written before encryption was correctly configured) only covered 16 of the now-50 affected models. Extended to cover every provider item/account, plusApiKey,SsoProvider,SsoIdentityBlock. Without this, upgrading past #3189 would leave previously-plaintext data for roughly half the affected models with no remediation path at all.compose.example.yml/compose.example.ai.ymlshipped an identical, hardcoded, working defaultSECRET_KEY_BASE. Since Active Record's encryption keys are deterministically derived fromSECRET_KEY_BASEwhen not set explicitly, any default install that never overrode it in.envends up with encryption keys — and session/cookie-signing secrets — that are publicly computable straight from this repo. Removed the default: compose now fails fast with a clear message ifSECRET_KEY_BASEisn't set. Also added theACTIVE_RECORD_ENCRYPTION_*passthrough these files were missing (same gap as #2388 — folded in here rather than duplicating it, feel free to close that one in favor of this if maintainers agree).docs/hosting/docker.mdupdated to match: settingSECRET_KEY_BASEis no longer presented as an optional/skippable step, and a new section documents running the backfill task after an encryption-related update.
A pre-existing bug found along the way
Adding a read/update test for the newly-encrypted IndexaCapitalAccount surfaced a latent bug: test/fixtures/indexa_capital_accounts.yml seeds raw_payload/raw_holdings_payload/raw_activities_payload as literal {}/[]. Once those columns are encrypted, config.active_record.encryption.encrypt_fixtures double-encodes empty Hash/Array fixture values for jsonb columns, so reading them back raises ActiveRecord::Encryption::Errors::Decryption.
This isn't new — SnaptradeAccount's fixture has the identical {}/[] pattern and is silently broken the same way (SnaptradeAccount.first.raw_payload raises identically on main today); its test suite just never happens to read that field on a fixture-loaded record. Fixed indexa_capital_accounts.yml to use nil instead, which resolves it. Left SnaptradeAccount's pre-existing instance of this out of scope here since it's unrelated to this PR's changes — happy to file a follow-up issue if useful.
Testing
bin/rails teston all changed models/tasks: 153 runs, 368 assertions, 0 failures.bin/rubocop: 24 files, no offenses.bin/brakeman --no-pager: 0 errors, 0 security warnings.
Summary by CodeRabbit
-
Security
- Expanded encryption coverage for sensitive provider credentials and financial data.
- Added warnings for compromised default secret keys.
- Encryption warnings now recognize automatically generated keys.
-
Configuration
- Self-hosted deployments must provide
SECRET_KEY_BASE. - Added optional encryption key settings.
- Self-hosted deployments must provide
-
Maintenance
- Added tools and guidance for re-encrypting existing data.
- Legacy unencrypted data remains readable during migration.
- Improved authentication during encryption migrations.
-
Documentation
- Updated Docker hosting and encryption migration instructions.
-
Tests
- Added coverage for encryption, warnings, authentication, and backfills.