The personal finance app for everyone (by everyone)
Ruby
Encrypt SnapTrade account_number (#3832)
GFR2ea59b48ed57
diff --git a/app/models/snaptrade_account.rb b/app/models/snaptrade_account.rbindex dd3cad25da..fc75de9e1a 100644--- a/app/models/snaptrade_account.rb+++ b/app/models/snaptrade_account.rb@@ -9,6 +9,9 @@ class SnaptradeAccount < ApplicationRecordencrypts :raw_holdings_payloadencrypts :raw_activities_payloadencrypts :raw_balances_payload+ # Not deterministic: never looked up by value, and a brokerage account+ # number is exactly the kind of field worth keeping opaque at rest.+ encrypts :account_numberendbelongs_to :snaptrade_itemdiff --git a/lib/tasks/security_backfill.rake b/lib/tasks/security_backfill.rakeindex 357a232fe3..acf41abdf5 100644--- a/lib/tasks/security_backfill.rake+++ b/lib/tasks/security_backfill.rake@@ -65,7 +65,7 @@ namespace :security doresults[:simplefin_accounts] = backfill_model(SimplefinAccount, %i[raw_payload raw_transactions_payload raw_holdings_payload], batch_size, dry_run)results[:lunchflow_accounts] = backfill_model(LunchflowAccount, %i[raw_payload raw_transactions_payload], batch_size, dry_run)results[:enable_banking_accounts] = backfill_model(EnableBankingAccount, %i[raw_payload raw_transactions_payload], batch_size, dry_run)- results[:snaptrade_accounts] = backfill_model(SnaptradeAccount, %i[raw_payload raw_transactions_payload raw_holdings_payload raw_activities_payload], batch_size, dry_run)+ results[:snaptrade_accounts] = backfill_model(SnaptradeAccount, %i[raw_payload raw_transactions_payload raw_holdings_payload raw_activities_payload account_number], batch_size, dry_run)results[:coinbase_accounts] = backfill_model(CoinbaseAccount, %i[raw_payload raw_transactions_payload], batch_size, dry_run)results[:coinstats_accounts] = backfill_model(CoinstatsAccount, %i[raw_payload raw_transactions_payload], batch_size, dry_run)results[:mercury_accounts] = backfill_model(MercuryAccount, %i[raw_payload raw_transactions_payload], batch_size, dry_run)diff --git a/test/encryption_verification_test.rb b/test/encryption_verification_test.rbindex e712aeeea8..39723a260d 100644--- a/test/encryption_verification_test.rb+++ b/test/encryption_verification_test.rb@@ -353,6 +353,19 @@ class EncryptionVerificationTest < ActiveSupport::TestCaseassert_column_not_plaintext(MonobankAccount, account.id, :raw_transactions_payload, "tx_probe")end+ test "snaptrade account_number is encrypted" do+ skip "No snaptrade accounts in fixtures" unless SnaptradeAccount.any?++ account = SnaptradeAccount.first+ account.update!(account_number: "9988776655")+ account.reload++ assert_equal "9988776655", account.account_number++ # A brokerage account number is exactly the kind of field worth proving opaque.+ assert_column_not_plaintext(SnaptradeAccount, account.id, :account_number, "9988776655")+ end+test "plaid account holdings stored before encryption was enabled can be read and backfilled" doaccount = PlaidAccount.create!(plaid_item: plaid_items(:one),diff --git a/test/lib/tasks/security_backfill_test.rb b/test/lib/tasks/security_backfill_test.rbindex 57258b6414..16f72c111f 100644--- a/test/lib/tasks/security_backfill_test.rb+++ b/test/lib/tasks/security_backfill_test.rb@@ -49,6 +49,23 @@ class SecurityBackfillTest < ActiveSupport::TestCaserefute_includes at_rest, "tx-1"end+ # Guards the task's field list: dropping :account_number from the+ # SnaptradeAccount entry would leave the model-level encryption test green+ # while pre-encryption rows stayed plaintext.+ test "backfill encrypts a plaintext SnapTrade account number" do+ account = snaptrade_accounts(:fidelity_401k)+ ActiveRecord::Base.connection.execute(ActiveRecord::Base.sanitize_sql([+ "UPDATE snaptrade_accounts SET account_number = ? WHERE id = ?", "5544332211", account.id ]))++ capture_io { Rake::Task["security:backfill_encryption"].invoke("500", "false") }++ assert_equal "5544332211", account.reload.account_number+ at_rest = ActiveRecord::Base.connection.select_value(+ SnaptradeAccount.where(id: account.id).select(:account_number).to_sql)+ refute_includes at_rest.to_s, "5544332211",+ "account_number must be stored as ciphertext after the backfill"+ end+# Several payload columns default to {} — Rails presence checks treat empty# Hash/Array as absent, so the backfill must gate on nil-ness or empty# payloads stay plaintext and raise on every read once keys are live.