security: gate plaintext encryption fallback behind a backfill-completion flag
Closes #3214.
Builds on #3189 (merge that first). Independent of #3190.
Background
config/initializers/active_record_encryption.rb unconditionally sets support_unencrypted_data = true and extend_queries = true so self-hosted installs with pre-existing plaintext data (on the ~50 models #3190 covers) don't crash on boot before running bin/rails security:backfill_encryption. The problem, flagged by CodeRabbit on #3189 and tracked as #3214: these settings stay on forever, even after the backfill has fully completed. A plaintext value landing in an encrypted column later — a bug, a bad migration, a manual DB edit — would be silently accepted instead of raising, defeating the integrity guarantee encryption is supposed to provide.
What this PR does
- Versioned completion flag (
Setting.encryption_backfill_completed_version,app/models/setting.rb) — a DB-backed setting so it persists across processes/deploys and is readable at boot, before the backfill task has necessarily ever run. ActiveRecordEncryptionConfig::CURRENT_BACKFILL_VERSION+.backfill_completed?(lib/active_record_encryption_config.rb) — compares the stored version against the current one, so a future expansion of the backfill task's model coverage (bumping the constant) correctly puts installs that completed an older, narrower backfill back into "not complete" until they re-run the task. Defaults tofalse(fallback stays enabled) if thesettingstable can't be read yet — fresh installs, pre-migration boots, DB errors.lib/tasks/security_backfill.rakenow sets the flag, but only on a clean (zero-failure), non-dry-run completion — a partial failure must not disable the fallback for models that still have unreadable plaintext rows.config/initializers/active_record_encryption.rbnow gates both settings onActiveRecordEncryptionConfig.backfill_completed?instead of leaving them unconditionally on.config/initializers/encryption_warning.rbgets a third boot-time[SECURITY]warning branch (alongside "not configured" and "compromised default key") for "configured but backfill not completed yet," reusing the existing operator-facing warning pattern rather than adding new UI.
Testing
- New coverage in
test/lib/active_record_encryption_config_test.rb(realSettingread/write round-trips forbackfill_completed?, plus the DB-unreachable rescue path) andtest/lib/tasks/security_backfill_test.rb(clean run sets the flag; dry run and a run with failures don't). test/initializers/active_record_encryption_test.rbupdated with a comment explaining why the existing assertions still hold (initializer runs once at boot in the "not completed" state the test suite always starts in; the gating logic itself is covered separately since re-running the initializer per-test isn't possible).
🤖 Generated with Claude Code
Summary by CodeRabbit
-
Security
- Expanded encryption coverage for sensitive credentials and provider data.
- Improved compatibility during encryption migration and strengthened configuration checks.
-
Bug Fixes
- Prevented misleading encryption warnings for supported self-hosted configurations.
- Improved reporting when encryption backfills encounter failures.
-
New Features
- Added encryption backfill tracking and broader coverage.
- Added safer handling for incomplete or compromised encryption settings.
-
Documentation
- Clarified required Docker environment settings and documented encryption backfill procedures.