Encrypt DebugLogEntry metadata, redact sensitive keys, fix leaky call sites
Stacked on #3190. The base branch stack/encryption-followup is a copy of #3190's head, so this diff only shows this PR's changes. Please merge #3190 first, then retarget this PR to main. Since the backfill now uses BACKFILL_MANIFEST, this PR adds debug_log_entries (metadata) to it and bumps CURRENT_BACKFILL_VERSION to 11, so installs that completed the version 10 backfill keep the plaintext fallback until they run it again.
Summary
DebugLogEntry#metadata (jsonb) had no encrypts declaration, unlike every raw_*_payload column elsewhere in the app. normalize_metadata only did deep_stringify_keys/.to_s — no sanitization at all. An audit of the ~185 DebugLogEntry.capture(...) call sites across provider importers found 8 confirmed passing raw sensitive data (response bodies, balances, wallet addresses, account identifiers, position amounts) into this unencrypted, unsanitized column.
- Adds
encrypts :metadata, wired into the existingsecurity:backfill_encryptionrake task. - Adds a recursive, key-name-based redaction pass in
normalize_metadata(balance|amount|address|body|qty|uid|api_account_id→"[REDACTED]", at any nesting depth, through bothHashandArray). This is defense-in-depth for the manycapture(...)sites this class has no direct visibility into — not a substitute for fixing the sites below, but a safety net so a missed or future site doesn't silently leak. - Fixes the 8 confirmed sites:
Provider::Questrade— dropped the raw responsebody.EnableBankingItem::Importer— droppeduid,api_account_id,previous_current_balanceacross 3 call sites (the audit had only flagged one; the other two had the same pattern).PdfImport— dropped the raw parsedamount(kept the date, which isn't sensitive on its own).OnchainWalletItem::Importer— dropped the walletaddressacross 2 call sites.MonobankEntry::Processor— droppedexternal_idand the rawoperation_amount(kept the currency code).CoinspotItem::Importer— dropped the accountbalance.WiseItem::Importer— droppedbalance_id(kept the currency code).- Trade Republic's originally-flagged site no longer exists in its old form — the multi-connection refactor (#3754) already rewrote it to carry only IDs/dates/counts, no quantities or amounts.
- The ~6 previously-"unclear" sites (
errors:/extra_metadataarrays and hashes of unknown shape in Binance and Redbark importers) are covered by the recursive redaction pass rather than hand-edited individually — their key names get redacted wherever a nested hash happens to carry a sensitive field, without needing to pin down every possible shape.
Test plan
-
bin/rails test test/models/debug_log_entry_test.rb test/encryption_verification_test.rbplus the model tests for every touched importer (Questrade, Enable Banking, PdfImport, OnchainWallet, Monobank, CoinSpot, Wise) — green, including with realActiveRecord::Encryptionkeys configured (this sandbox has none by default) to actually exercise the encrypted path. - Updated 2 pre-existing tests that asserted on now-removed fields (Monobank
external_id, on-chain walletaddress) to assert the field is absent instead. - Added coverage for the redaction pass (top-level keys, nested hash/array keys, and that unrelated keys like
monobank_account_idaren't false-positived). -
bin/rubocop,bin/brakeman— clean on touched files.
🤖 Generated with Claude Code
Summary by CodeRabbit
- Security
- Sensitive information in diagnostic logs is redacted, including credentials and personal identifiers in nested data.
- Diagnostic logs no longer include sensitive account identifiers, balances, transaction details, wallet addresses, or raw response and import values.
- Existing debug-log metadata is included in the security backfill, with sensitive values redacted before encryption. Backfill supports batch processing and dry runs.