Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Drop invalid `previous:` option on PlaidAccount holdings encryption (#4059)

Megan Holsteinbdc45f0988ef
diff --git a/app/models/plaid_account.rb b/app/models/plaid_account.rb
index fa5922ae18..9ed9a48315 100644
--- a/app/models/plaid_account.rb
+++ b/app/models/plaid_account.rb
@@ -5,8 +5,10 @@ class PlaidAccount < ApplicationRecord
if encryption_ready?
encrypts :raw_payload
encrypts :raw_transactions_payload
- # Support reading data encrypted under the old column name after rename
- encrypts :raw_holdings_payload, previous: { attribute: :raw_investments_payload }
+ # Renamed from raw_investments_payload; rename_column keeps the stored ciphertext,
+ # which decrypts under the new name because Active Record Encryption does not bind
+ # ciphertext to the attribute name, so no `previous:` scheme is needed.
+ encrypts :raw_holdings_payload
encrypts :raw_liabilities_payload
end
diff --git a/test/encryption_verification_test.rb b/test/encryption_verification_test.rb
index b5871c8387..e712aeeea8 100644
--- a/test/encryption_verification_test.rb
+++ b/test/encryption_verification_test.rb
@@ -353,6 +353,31 @@ class EncryptionVerificationTest < ActiveSupport::TestCase
assert_column_not_plaintext(MonobankAccount, account.id, :raw_transactions_payload, "tx_probe")
end
+ test "plaid account holdings stored before encryption was enabled can be read and backfilled" do
+ account = PlaidAccount.create!(
+ plaid_item: plaid_items(:one),
+ plaid_id: "holdings_backfill_probe",
+ name: "Brokerage",
+ plaid_type: "investment",
+ currency: "USD",
+ current_balance: 1000
+ )
+ # Store plaintext, as an instance that enabled encryption after syncing would have.
+ PlaidAccount.where(id: account.id).update_all(
+ "raw_holdings_payload = '{\"holdings\": [{\"security_id\": \"sec_probe\"}]}'::jsonb"
+ )
+
+ with_unencrypted_data_supported do
+ account = PlaidAccount.find(account.id)
+ assert_equal "sec_probe", account.raw_holdings_payload["holdings"].first["security_id"]
+
+ account.encrypt
+ end
+
+ assert_equal "sec_probe", account.reload.raw_holdings_payload["holdings"].first["security_id"]
+ assert_column_not_plaintext(PlaidAccount, account.id, :raw_holdings_payload, "sec_probe")
+ end
+
# ============================================================================
# DATABASE VERIFICATION TESTS
# ============================================================================
@@ -380,6 +405,14 @@ class EncryptionVerificationTest < ActiveSupport::TestCase
private
+ def with_unencrypted_data_supported
+ previous = ActiveRecord::Encryption.config.support_unencrypted_data
+ ActiveRecord::Encryption.config.support_unencrypted_data = true
+ yield
+ ensure
+ ActiveRecord::Encryption.config.support_unencrypted_data = previous
+ end
+
# Reads +column+ straight from the database, bypassing the model, and asserts the
# stored bytes do not contain +value+. An accessor round-trip alone cannot tell
# encryption from plaintext; this can.