The personal finance app for everyone (by everyone)
Ruby
fix(wise): refuse an SCA private key when encryption is unavailable (#3415)
Anthony9ec28abacc52
diff --git a/app/models/wise_item.rb b/app/models/wise_item.rbindex f6d4bed51d..5c5e23d791 100644--- a/app/models/wise_item.rb+++ b/app/models/wise_item.rb@@ -3,6 +3,11 @@class WiseItem < ApplicationRecordinclude Syncable, Provided, Unlinking, Encryptable+ # Raised rather than returned so no caller can mistake "not stored" for+ # "stored"; the controller turns it into the same panel error as any other+ # keypair failure.+ class SCAEncryptionUnavailable < StandardError; end+enum :status, { good: "good", requires_update: "requires_update" }, default: :goodenum :profile_type, { personal: "personal", business: "business" }@@ -16,6 +21,13 @@ class WiseItem < ApplicationRecordvalidates :token, presence: true, on: :createvalidates :profile_id, uniqueness: { scope: :family_id }+ # An SCA private key signs balance-statement requests to Wise, so plaintext+ # at rest is not an acceptable degraded mode the way an unencrypted display+ # name would be. Without ActiveRecord encryption configured, `encrypts` above+ # never runs and the PEM would land in the column as-is, so the key is simply+ # refused instead.+ validate :sca_private_key_requires_encryption+before_validation :normalize_tokenbelongs_to :family@@ -136,11 +148,17 @@ class WiseItem < ApplicationRecord# endpoint. The private key stays here (encrypted at rest); the public key# must be registered with Wise by the user (Settings > API tokens > Public keys).def generate_sca_keypair!+ raise SCAEncryptionUnavailable, "Active Record encryption is not configured" unless sca_encryption_available?+key = OpenSSL::PKey::RSA.generate(2048)update!(sca_private_key: key.to_pem)sca_public_keyend+ def sca_encryption_available?+ self.class.encryption_ready?+ end+def sca_public_keyreturn nil unless sca_private_key.present?@@ -190,4 +208,18 @@ class WiseItem < ApplicationRecorddef normalize_tokenself.token = token&.stripend++ # Scoped to writes of the key itself. An install that generated a key+ # before this validation existed still has that value in the column, so+ # validating on every save would reject every later write to the record:+ # renaming the connection, and worse, WiseItemsController#destroy, which+ # unlinks the accounts BEFORE destroy_later's update! and would leave the+ # provider half unlinked and still active. Refusing a NEW key is the point;+ # refusing to let go of an old one is not.+ def sca_private_key_requires_encryption+ return unless will_save_change_to_sca_private_key?+ return if sca_private_key.blank? || sca_encryption_available?++ errors.add(:sca_private_key, :encryption_unavailable)+ endenddiff --git a/config/locales/models/wise_item/en.yml b/config/locales/models/wise_item/en.ymlnew file mode 100644index 0000000000..a2f7e6db04--- /dev/null+++ b/config/locales/models/wise_item/en.yml@@ -0,0 +1,12 @@+---+en:+ activerecord:+ attributes:+ wise_item:+ sca_private_key: SCA private key+ errors:+ models:+ wise_item:+ attributes:+ sca_private_key:+ encryption_unavailable: cannot be stored because Active Record encryption is not configured on this installdiff --git a/config/locales/models/wise_item/fr.yml b/config/locales/models/wise_item/fr.ymlnew file mode 100644index 0000000000..495ba02da0--- /dev/null+++ b/config/locales/models/wise_item/fr.yml@@ -0,0 +1,12 @@+---+fr:+ activerecord:+ attributes:+ wise_item:+ sca_private_key: Clé privée SCA+ errors:+ models:+ wise_item:+ attributes:+ sca_private_key:+ encryption_unavailable: ne peut pas être enregistrée car le chiffrement Active Record n'est pas configuré sur cette installationdiff --git a/test/controllers/wise_items_controller_test.rb b/test/controllers/wise_items_controller_test.rbindex 0698f95227..d9f190fcc1 100644--- a/test/controllers/wise_items_controller_test.rb+++ b/test/controllers/wise_items_controller_test.rb@@ -121,7 +121,10 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTestassert_redirected_to settings_providers_pathend+ # The key is only ever stored encrypted, and the test environment configures+ # no encryption keys, so the flow has to be exercised as an install that does.test "generate_sca_keypair stores a keypair on the item" do+ WiseItem.stubs(:encryption_ready?).returns(true)assert_nil @wise_item.sca_private_keypost generate_sca_keypair_wise_item_url(@wise_item)@@ -131,6 +134,7 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTestendtest "generate_sca_keypair replaces a previously generated keypair" do+ WiseItem.stubs(:encryption_ready?).returns(true)@wise_item.generate_sca_keypair!previous_key = @wise_item.sca_private_key@@ -139,6 +143,14 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTestassert_not_equal previous_key, @wise_item.reload.sca_private_keyend+ test "generate_sca_keypair reports an error rather than storing a key in the clear" do+ WiseItem.stubs(:encryption_ready?).returns(false)++ post generate_sca_keypair_wise_item_url(@wise_item)++ assert_nil @wise_item.reload.sca_private_key+ end+test "link_profiles redirects to providers when the session token cannot be decrypted" doProvider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)post wise_items_url, params: { wise_item: { token: "live_token_abc" } }diff --git a/test/models/provider/wise_adapter_test.rb b/test/models/provider/wise_adapter_test.rbindex b4b217d84a..de902f6a04 100644--- a/test/models/provider/wise_adapter_test.rb+++ b/test/models/provider/wise_adapter_test.rb@@ -6,7 +6,10 @@ class Provider::WiseAdapterTest < ActiveSupport::TestCase@wise_item = wise_items(:one)end+ # The key is only ever stored on an install with Active Record encryption+ # configured, which the test environment is not.test "build_provider threads sca_private_key through to Provider::Wise" do+ WiseItem.stubs(:encryption_ready?).returns(true)key = OpenSSL::PKey::RSA.new(2048).to_pem@wise_item.update!(sca_private_key: key)diff --git a/test/models/wise_item_test.rb b/test/models/wise_item_test.rbindex 6a9ad4af36..580636755a 100644--- a/test/models/wise_item_test.rb+++ b/test/models/wise_item_test.rb@@ -59,6 +59,7 @@ class WiseItemTest < ActiveSupport::TestCaseendtest "generate_sca_keypair! stores a private key and returns a matching public key" do+ WiseItem.stubs(:encryption_ready?).returns(true)public_pem = @wise_item.generate_sca_keypair!assert @wise_item.sca_configured?@@ -69,7 +70,47 @@ class WiseItemTest < ActiveSupport::TestCaseassert_equal private_key.public_key.to_pem, public_pemend+ # An SCA private key signs requests to Wise. Storing it unencrypted is not a+ # degraded mode worth having, so an install without Active Record encryption+ # is refused rather than silently writing the PEM in the clear.+ test "refuses to store an SCA private key when encryption is not configured" do+ WiseItem.stubs(:encryption_ready?).returns(false)++ assert_raises(WiseItem::SCAEncryptionUnavailable) { @wise_item.generate_sca_keypair! }+ assert_nil @wise_item.reload.sca_private_key++ @wise_item.sca_private_key = "not a real PEM"++ assert_not @wise_item.valid?+ assert_includes @wise_item.errors.attribute_names, :sca_private_key+ end++ # The validation guards writes of the key, not the record. A value stored+ # before it existed must not make the record permanently unsaveable: the+ # destroy path unlinks the accounts first and only then calls update!, so a+ # refusal there strands the provider half unlinked and still active.+ test "a key stored before encryption was required does not block later saves" do+ WiseItem.stubs(:encryption_ready?).returns(false)+ @wise_item.update_column(:sca_private_key, "legacy plaintext value")++ assert @wise_item.reload.valid?+ assert @wise_item.update(name: "Renamed connection")++ assert_nothing_raised { @wise_item.destroy_later }+ assert @wise_item.reload.scheduled_for_deletion+ end++ # Same shape as the other Encryptable models' tests: the suite deliberately+ # runs without encryption keys (see EncryptionVerificationTest), so this+ # skips rather than asserting a state the default environment cannot reach.+ test "declares the SCA private key as encrypted" do+ skip "Encryption not configured" unless WiseItem.encryption_ready?++ assert_includes WiseItem.encrypted_attributes.map(&:to_s), "sca_private_key"+ end+test "generate_sca_keypair! replaces a previously generated key" do+ WiseItem.stubs(:encryption_ready?).returns(true)first_public_key = @wise_item.generate_sca_keypair!second_public_key = @wise_item.generate_sca_keypair!