fix(security): use ready? instead of explicitly_configured? for encryption gating

Merged
GF
21d ago
+1004-106

Fixes #3142.

What

ActiveRecordEncryptionConfig.explicitly_configured? only recognizes explicit ACTIVE_RECORD_ENCRYPTION_* env vars or Rails credentials. It ignores the documented self-hosted fallback where keys are auto-derived from SECRET_KEY_BASE and loaded into Rails.application.config.active_record.encryption at boot (runtime_configured?). ActiveRecordEncryptionConfig.ready? (= explicitly_configured? || runtime_configured?) already covers both cases correctly, but was never called anywhere in the app.

Every encrypts :field if encryption_ready? declaration runs once at class-load time. On self-hosted installs relying on the auto-generation fallback (i.e. never set the three env vars explicitly — the documented default path), encryption_ready? incorrectly returned false, so every one of those encrypts calls was silently skipped. Sensitive fields (provider API tokens/keys, MFA secrets, PII) were stored as plain, unencrypted attributes despite Rails' encryption machinery being genuinely configured and ready to use. This is more than the UI warning the issue title suggests — it's a data-at-rest encryption bug.

Changes

  • Encryptable#encryption_ready? (used by 42 models), Settings::SecuritiesController's warning banner, the boot-time [SECURITY] log warning, and Settings::ProvidersController's warning banner now all check .ready? instead of .explicitly_configured?.
  • CoinstatsItem, IndexaCapitalItem, MercuryItem, SnaptradeItem, and SophtronItem each hand-rolled their own copy of the identical broken check instead of using the Encryptable concern — a fix to the concern alone would not have fixed these 5. Refactored to include Encryptable (same pattern as #1339), which fixes the bug and removes the duplication.
  • Added a regression test at each affected call site for the specific scenario that was previously untested: explicitly_configured? false but runtime_configured? true (the auto-generated-keys case).

No CI/test-env changes were needed to exercise the fix: config/environments/test.rb already hardcodes a working encryption config, so runtime_configured? (and now ready?) is already true in the test suite — the round-trip encrypts tests now actually run instead of being silently skipped.

Operational note for self-hosted operators

Any data written while running under this bug was stored as plaintext. After upgrading, preview with bin/rails security:backfill_encryption (dry run by default), then encrypt previously-plaintext rows with DRY_RUN=false bin/rails security:backfill_encryption. For key rotation — including the deterministic-attribute migration the backfill cannot perform — see docs/hosting/encryption.md. A follow-up PR will extend that task's model coverage (currently 16 of the 47 affected models) and close a few structural gaps (a few models with no encryption wired up at all).

Since review (2026-11-10)

Addressing jjmata's three P2 findings (commits 9a2fb6c4e, 56e06bf04):

  • Boot priority restored: the initializer's partial-credentials rejection now skips when the ENV set is complete (ActiveRecordEncryptionConfig.reject_partial_credentials?), so complete ACTIVE_RECORD_ENCRYPTION_* vars plus a stale/partial credentials block boots again, per the documented ENV-first priority. The combination is covered by new tests in test/lib/active_record_encryption_config_test.rb.
  • Remediation command actually writes: the boot warning prescribes the plain dry-run preview plus the explicit DRY_RUN=false bin/rails security:backfill_encryption write pass (the task defaults to dry run).
  • Rotation procedure handles deterministic attributes: the warning no longer recommends retiring a compromised scheme via the backfill alone — Rails re-serializes deterministic ("fixed") attributes under the oldest configured scheme, so the backfill leaves those columns (User.email, otp_secret, provider tokens, …) under the compromised key. New docs/hosting/encryption.md documents the full four-step procedure (previous-scheme setup → preview + write backfill → per-attribute decrypt-and-rewrite under the current scheme with strict new-key-only verification → retirement); the rake task's scope note carries the same caveat. Automated rotation tooling remains tracked in #3233.

No local test run for these two commits (no Ruby toolchain on the machine they were prepared on) — CI on 56e06bf04 is the check.

Testing

  • bin/rails test on the changed files and their controllers/models: 93 runs, 196 assertions, 0 failures, 0 errors, 0 skips.
  • bin/rubocop: 17 files inspected, no offenses.
  • bin/brakeman --no-pager: 0 errors, 0 security warnings.

Summary by CodeRabbit

  • Bug Fixes

    • Self-hosted installations using runtime-generated encryption keys no longer display incorrect warnings.
    • Legacy unencrypted data remains readable during encryption backfill, including supported deterministic lookups.
    • Encryption readiness is recognized consistently across settings and integrations.
  • Security

    • Sensitive integration credentials are encrypted whenever encryption is available.
    • Known compromised keys now trigger warnings and safe rotation guidance.
    • Incomplete encryption configuration is detected and reported clearly.
  • Tests

    • Added coverage for encryption configuration, warnings, legacy data access, and encrypted credentials.