Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index dd3cad25da..fc75de9e1a 100644
--- a/app/models/snaptrade_account.rb
+++ b/app/models/snaptrade_account.rb
@@ -9,6 +9,9 @@ class SnaptradeAccount < ApplicationRecord
encrypts :raw_holdings_payload
encrypts :raw_activities_payload
encrypts :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_number
end
belongs_to :snaptrade_item
diff --git a/lib/tasks/security_backfill.rake b/lib/tasks/security_backfill.rake
index 357a232fe3..acf41abdf5 100644
--- a/lib/tasks/security_backfill.rake
+++ b/lib/tasks/security_backfill.rake
@@ -65,7 +65,7 @@ namespace :security do
results[: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.rb
index e712aeeea8..39723a260d 100644
--- a/test/encryption_verification_test.rb
+++ b/test/encryption_verification_test.rb
@@ -353,6 +353,19 @@ class EncryptionVerificationTest < ActiveSupport::TestCase
assert_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" do
account = PlaidAccount.create!(
plaid_item: plaid_items(:one),
diff --git a/test/lib/tasks/security_backfill_test.rb b/test/lib/tasks/security_backfill_test.rb
index 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::TestCase
refute_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.