← Back to Pull requests

Encrypt the remaining provider identifiers at rest #4336

Open
unknown opened this pull request with 504 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 20, 2026 09:05 UTC
Author

Problem

Some provider fields still store personal data as plaintext in the database, even though the fields next to them are already encrypted:

  • enable_banking_items.last_psu_ip: the user's IP address. Enable Banking needs it for a while after login.

  • akahu_accounts.formatted_account: the full New Zealand bank account number.

  • akahu_accounts.institution_metadata: holds a second copy of that number and the account holder's name. No code reads either key.

  • lunchflow_accounts.raw_holdings_payload: the brokerage holdings. The other Lunch Flow payloads are already encrypted.
  • What changes

  • Encryption: these three attributes are now declared with encrypts, inside the existing if encryption_ready? blocks. Nothing looks them up by value, so they use non-deterministic encryption.

  • Akahu metadata: Akahu no longer writes account_number and holder into institution_metadata. A migration removes both keys from existing rows. The number stays readable through the encrypted formatted_account, and both values remain in the encrypted raw payload.

  • Rows written before encryption:

  • - Each attribute uses support_unencrypted_data: true. A plaintext value stays readable instead of raising ActiveRecord::Encryption::Errors::Decryption. That covers rows from earlier syncs, rows written by a process still on the old code during a deploy, and instances that only configure keys later.
    - A migration also encrypts the plaintext already stored in these columns, so the data does not stay plaintext at rest until someone runs the backfill. It skips values that are already encrypted (string and jsonb columns), pages by id, and does nothing when encryption is not configured.
    - security:backfill_encryption now lists the new columns.

    Not included

  • enable_banking_accounts.iban: #3510 already encrypts it, deterministically, because the IBAN work looks it up. A second encrypts with a different scheme here would leave the column unreadable once both land.

  • Covered by other open PRs: the raw payloads of the other providers and Indexa's username and document (#3190), and DebugLogEntry metadata (#3833). If #3221 (backfill completion flag) lands first, its backfill manifest needs the three columns from this PR. These PRs also edit the backfill task's model list, so whichever merges last gets a small, additive conflict there.

  • Provider account ids (Trade Republic, Interactive Brokers, Fio, Questrade, Trading 212): syncs look them up by value, and the same number is stored in several columns, so encrypting one column alone would not help.

  • Counterparty data inside transactions.extra: rules and search query that column.
  • Tests

  • New tests cover encryption at rest for the three columns, reading a value stored before encryption without global support_unencrypted_data, the backfill task on plaintext rows, both migrations, and the Akahu importer no longer writing account details into metadata.

  • Like the existing encryption tests, these skip unless the ACTIVE_RECORD_ENCRYPTION_* keys are set, and CI does not set them. I ran them locally with keys: they pass with the change and fail without it.

  • Full suite: bin/rails test passes (12,442 runs). Rubocop and Brakeman are clean.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

  • Security Enhancements

  • - Sensitive provider account identifiers and financial data are now encrypted at rest.
    - Existing unencrypted records remain readable during migration, while new and backfilled data is encrypted.

  • Privacy Improvements

  • - Akahu institution metadata no longer stores account numbers or account-holder names.
    - Protected account details remain available through the appropriate account fields.

  • Data Migration

  • - Existing records are automatically updated to apply encryption and remove sensitive metadata without interrupting access.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 504 lines (+378 / -126) across 6 files Reviewer: jjmata Review duration: 1143m
    All Vouch review confidence checks passed
    Residual risk 0.38 = Change Risk (0.71) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 504 lines diff across 6 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.372
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #4336"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 70.7% (HIGH) · Higher = larger blast radius
    70.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 37.7% · Moderate risk
    37.7%