Encrypt DebugLogEntry metadata, redact sensitive keys, fix leaky call sites

Merged
GF
20d ago
+439-66

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 existing security:backfill_encryption rake 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 both Hash and Array). This is defense-in-depth for the many capture(...) 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 response body.
    • EnableBankingItem::Importer — dropped uid, api_account_id, previous_current_balance across 3 call sites (the audit had only flagged one; the other two had the same pattern).
    • PdfImport — dropped the raw parsed amount (kept the date, which isn't sensitive on its own).
    • OnchainWalletItem::Importer — dropped the wallet address across 2 call sites.
    • MonobankEntry::Processor — dropped external_id and the raw operation_amount (kept the currency code).
    • CoinspotItem::Importer — dropped the account balance.
    • WiseItem::Importer — dropped balance_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_metadata arrays 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.rb plus the model tests for every touched importer (Questrade, Enable Banking, PdfImport, OnchainWallet, Monobank, CoinSpot, Wise) — green, including with real ActiveRecord::Encryption keys 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 wallet address) 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_id aren'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.