Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(wise): require encrypted SCA key registration (#3439)

Sure Admin (bot)f5d9a15c99ad
diff --git a/app/models/wise_item.rb b/app/models/wise_item.rb
index 5c5e23d791..d66af3e084 100644
--- a/app/models/wise_item.rb
+++ b/app/models/wise_item.rb
@@ -3,6 +3,8 @@
class WiseItem < ApplicationRecord
include Syncable, Provided, Unlinking, Encryptable
+ SCA_PRIVATE_KEY_ATTRIBUTE = "sca_private_key"
+
# 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.
@@ -156,7 +158,8 @@ class WiseItem < ApplicationRecord
end
def sca_encryption_available?
- self.class.encryption_ready?
+ self.class.encryption_ready? &&
+ Array(self.class.encrypted_attributes).map(&:to_s).include?(SCA_PRIVATE_KEY_ATTRIBUTE)
end
def sca_public_key
diff --git a/test/controllers/wise_items_controller_test.rb b/test/controllers/wise_items_controller_test.rb
index d9f190fcc1..2a3d9a16ac 100644
--- a/test/controllers/wise_items_controller_test.rb
+++ b/test/controllers/wise_items_controller_test.rb
@@ -121,10 +121,9 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to settings_providers_path
end
- # 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)
+ WiseItem.any_instance.stubs(:sca_encryption_available?).returns(true)
+
assert_nil @wise_item.sca_private_key
post generate_sca_keypair_wise_item_url(@wise_item)
@@ -134,7 +133,8 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTest
end
test "generate_sca_keypair replaces a previously generated keypair" do
- WiseItem.stubs(:encryption_ready?).returns(true)
+ WiseItem.any_instance.stubs(:sca_encryption_available?).returns(true)
+
@wise_item.generate_sca_keypair!
previous_key = @wise_item.sca_private_key
diff --git a/test/controllers/wise_sca_localization_test.rb b/test/controllers/wise_sca_localization_test.rb
index 482f889ed2..6c8d97d978 100644
--- a/test/controllers/wise_sca_localization_test.rb
+++ b/test/controllers/wise_sca_localization_test.rb
@@ -68,8 +68,6 @@ class WiseScaPanelLocalizationTest < ActionDispatch::IntegrationTest
assert_sca_copy "regenerate_confirm"
end
- # 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 "German Wise SCA keypair generation returns the localized success message" do
WiseItem.stubs(:encryption_ready?).returns(true)
WiseItem.any_instance.expects(:generate_sca_keypair!).once.returns("SYNTHETIC PUBLIC KEY")
diff --git a/test/models/provider/wise_adapter_test.rb b/test/models/provider/wise_adapter_test.rb
index de902f6a04..f63dda6dab 100644
--- a/test/models/provider/wise_adapter_test.rb
+++ b/test/models/provider/wise_adapter_test.rb
@@ -6,12 +6,9 @@ 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)
+ @wise_item.update_column(:sca_private_key, key)
provider = Provider::WiseAdapter.build_provider(family: @family, wise_item_id: @wise_item.id)
diff --git a/test/models/wise_item_test.rb b/test/models/wise_item_test.rb
index 580636755a..fdbd44d68b 100644
--- a/test/models/wise_item_test.rb
+++ b/test/models/wise_item_test.rb
@@ -59,7 +59,8 @@ class WiseItemTest < ActiveSupport::TestCase
end
test "generate_sca_keypair! stores a private key and returns a matching public key" do
- WiseItem.stubs(:encryption_ready?).returns(true)
+ stub_sca_encryption_available
+
public_pem = @wise_item.generate_sca_keypair!
assert @wise_item.sca_configured?
@@ -70,6 +71,24 @@ class WiseItemTest < ActiveSupport::TestCase
assert_equal private_key.public_key.to_pem, public_pem
end
+ test "refuses to store an SCA private key unless the attribute is registered encrypted" do
+ WiseItem.stubs(:encryption_ready?).returns(true)
+ WiseItem.stubs(:encrypted_attributes).returns([])
+
+ assert_not @wise_item.sca_encryption_available?
+ assert_raises(WiseItem::SCAEncryptionUnavailable) { @wise_item.generate_sca_keypair! }
+ assert_nil @wise_item.reload.sca_private_key
+ end
+
+ test "refuses to store an SCA private key when encrypted attributes are unset" do
+ WiseItem.stubs(:encryption_ready?).returns(true)
+ WiseItem.stubs(:encrypted_attributes).returns(nil)
+
+ assert_not @wise_item.sca_encryption_available?
+ assert_raises(WiseItem::SCAEncryptionUnavailable) { @wise_item.generate_sca_keypair! }
+ assert_nil @wise_item.reload.sca_private_key
+ end
+
# 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.
@@ -110,7 +129,8 @@ class WiseItemTest < ActiveSupport::TestCase
end
test "generate_sca_keypair! replaces a previously generated key" do
- WiseItem.stubs(:encryption_ready?).returns(true)
+ stub_sca_encryption_available
+
first_public_key = @wise_item.generate_sca_keypair!
second_public_key = @wise_item.generate_sca_keypair!
@@ -171,6 +191,11 @@ class WiseItemTest < ActiveSupport::TestCase
private
+ def stub_sca_encryption_available
+ WiseItem.stubs(:encryption_ready?).returns(true)
+ WiseItem.stubs(:encrypted_attributes).returns([ "sca_private_key" ])
+ end
+
def create_interbalance_entry(account, resource_id, side:, amount:)
external_id = "wise_interbalance_#{resource_id}_#{side}"
transaction = Transaction.create!(kind: "funds_movement")