security: close remaining plaintext-storage gaps found while auditing #3142

Merged
GF
20d ago
+992-232

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

  1. Three models had no encryption wired up at all (not a gating bug — no encrypts call existed in the first place): IndexaCapitalAccount, SophtronAccount, OnchainWalletAccount now encrypt their raw provider payloads, matching every structurally identical sibling *Account model.
  2. IndexaCapitalItem#username/#document (a login-credential pair used by one of its two auth modes — document is very likely a national ID/tax document number for KYC) had no encrypts call. Now encrypted.
  3. Eight Item models encrypted their credentials but not raw_payload/raw_institution_payload, unlike every sibling provider that does: Brex, Coinbase, Binance, CoinStats, Mercury, IndexaCapital, Sophtron, SnapTrade.
  4. 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, plus ApiKey, SsoProvider, SsoIdentityBlock. Without this, upgrading past #3189 would leave previously-plaintext data for roughly half the affected models with no remediation path at all.
  5. compose.example.yml/compose.example.ai.yml shipped an identical, hardcoded, working default SECRET_KEY_BASE. Since Active Record's encryption keys are deterministically derived from SECRET_KEY_BASE when not set explicitly, any default install that never overrode it in .env ends 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 if SECRET_KEY_BASE isn't set. Also added the ACTIVE_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.md updated to match: setting SECRET_KEY_BASE is 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 test on 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.
  • 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.