Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Reuse the pending Enable Banking connection instead of leaving empty ones (#5105)

David Gil079a8aa1697f
diff --git a/app/controllers/enable_banking_items_controller.rb b/app/controllers/enable_banking_items_controller.rb
index e97675f32e..26f4ce6909 100644
--- a/app/controllers/enable_banking_items_controller.rb
+++ b/app/controllers/enable_banking_items_controller.rb
@@ -101,9 +101,11 @@ class EnableBankingItemsController < ApplicationController
end
begin
+ pending_connection = pending_connection_for(aspsp_name) if params[:new_connection] == "true"
target_item = if params[:new_connection] == "true"
- Current.family.enable_banking_items.create!(
+ pending_connection || Current.family.enable_banking_items.create!(
name: "Enable Banking Connection",
+ aspsp_name: aspsp_name,
country_code: @enable_banking_item.country_code,
application_id: @enable_banking_item.application_id,
client_certificate: @enable_banking_item.client_certificate
@@ -120,13 +122,15 @@ class EnableBankingItemsController < ApplicationController
# begin_authorization! re-fetches ASPSP metadata and auto-selects the best
# auth method (REDIRECT > DECOUPLED > EMBEDDED). Decoupled/MFA banks proceed
# through Enable Banking's hosted SCA page rather than being blocked.
- redirect_url = target_item.begin_authorization!(
- aspsp_name: aspsp_name,
- redirect_url: enable_banking_callback_url,
- state: issue_oauth_state_for(target_item),
- psu_type: psu_type,
- language: language
- )
+ redirect_url = with_oauth_state_for(target_item, supersede: pending_connection.present?) do |state|
+ target_item.begin_authorization!(
+ aspsp_name: aspsp_name,
+ redirect_url: enable_banking_callback_url,
+ state: state,
+ psu_type: psu_type,
+ language: language
+ )
+ end
safe_redirect_to_provider(
redirect_url,
@@ -212,11 +216,13 @@ class EnableBankingItemsController < ApplicationController
# Route through the shared path so reauthorization re-selects the same auth
# method (decoupled banks included) instead of falling back to a default.
- redirect_url = @enable_banking_item.begin_authorization!(
- redirect_url: enable_banking_callback_url,
- state: issue_oauth_state_for(@enable_banking_item),
- language: language
- )
+ redirect_url = with_oauth_state_for(@enable_banking_item, supersede: false) do |state|
+ @enable_banking_item.begin_authorization!(
+ redirect_url: enable_banking_callback_url,
+ state: state,
+ language: language
+ )
+ end
safe_redirect_to_provider(
redirect_url,
@@ -534,13 +540,56 @@ class EnableBankingItemsController < ApplicationController
end
private
+ # The connection is created before the bank's page opens, so a later click
+ # on the same bank, a back-and-retry or a failed start would leave an empty
+ # connection behind. An earlier attempt at this bank with the same
+ # credentials that never came back from it (no session, no accounts) is
+ # reused instead. Other banks keep their own connection. Only attempts
+ # started in this browser session qualify: two browsers of one family
+ # sharing a pending connection could both complete on it.
+ def pending_connection_for(aspsp_name)
+ Current.family.enable_banking_items.active
+ .where(id: pending_oauth_states.values)
+ .where(
+ aspsp_name: aspsp_name,
+ session_id: nil,
+ application_id: @enable_banking_item.application_id,
+ country_code: @enable_banking_item.country_code,
+ client_certificate: @enable_banking_item.client_certificate
+ )
+ .where.missing(:enable_banking_accounts)
+ .ordered
+ .first
+ end
+
# Pending states map state => item id, so parallel flows (two tabs) each
# keep their own entry. Only the newest few are kept.
- def issue_oauth_state_for(item)
+ #
+ # Yields a new state to the block that starts the flow, and records it only
+ # once the flow got a trusted provider URL. A reused pending connection
+ # (supersede) then keeps only that newest flow: two logins finishing on one
+ # connection would leave the first one's accounts attached to the second
+ # one's session. Its older states go before the cap applies, so they never
+ # push out another login still open. A start that fails, or returns an
+ # untrusted URL, leaves the map as it was, so the login still open can
+ # complete; only a connection with no state yet gets this one, so a retry
+ # finds it for reuse.
+ def with_oauth_state_for(item, supersede:)
state = SecureRandom.urlsafe_base64(32)
- pending = pending_oauth_states.merge(state => item.id)
+ pending = pending_oauth_states
+ started = false
+ redirect_url = yield(state)
+ started = trusted_provider_url?(redirect_url, TRUSTED_ENABLE_BANKING_HOSTS)
+ redirect_url
+ ensure
+ if started || !pending.value?(item.id)
+ pending = pending.reject { |_, item_id| item_id == item.id } if started && supersede
+ write_pending_oauth_states(pending.merge(state => item.id))
+ end
+ end
+
+ def write_pending_oauth_states(pending)
session[OAUTH_STATE_SESSION_KEY] = pending.to_a.last(MAX_PENDING_OAUTH_STATES).to_h
- state
end
# An entry is removed only when it matches, so a stray callback with a
diff --git a/test/controllers/enable_banking_items_controller_test.rb b/test/controllers/enable_banking_items_controller_test.rb
index 8fe30e937f..94db7311d8 100644
--- a/test/controllers/enable_banking_items_controller_test.rb
+++ b/test/controllers/enable_banking_items_controller_test.rb
@@ -265,6 +265,186 @@ class EnableBankingItemsControllerTest < ActionDispatch::IntegrationTest
assert @sent_state.length >= 32
end
+ # One N26 login used to leave five connections behind, four of them empty.
+ test "starting a new connection twice for the same bank reuses the pending one" do
+ stub_authorization_start
+
+ assert_difference -> { @family.enable_banking_items.count }, 1 do
+ 2.times do
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true" }
+ end
+ end
+
+ assert_redirected_to "https://api.enablebanking.com/auth/redirect/abc"
+ end
+
+ test "a new connection to another bank still gets its own connection" do
+ stub_authorization_start(banks: [ "Test Bank", "Other Bank" ])
+
+ assert_difference -> { @family.enable_banking_items.count }, 2 do
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true" }
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Other Bank", psu_type: "personal", new_connection: "true" }
+ end
+ end
+
+ test "a connection that already has a session is never reused for a new one" do
+ stub_authorization_start
+ start_new_connection
+ @family.enable_banking_items.order(:created_at).last.update!(session_id: "live-session")
+
+ assert_difference -> { @family.enable_banking_items.count }, 1 do
+ start_new_connection
+ end
+ end
+
+ test "a connection that already has accounts is never reused for a new one" do
+ stub_authorization_start
+ start_new_connection
+ @family.enable_banking_items.order(:created_at).last
+ .enable_banking_accounts.create!(name: "Checking", uid: "eb_existing", currency: "EUR")
+
+ assert_difference -> { @family.enable_banking_items.count }, 1 do
+ start_new_connection
+ end
+ end
+
+ # Reuse is limited to connections this browser started: two browsers of one
+ # family sharing a pending connection could both complete on it.
+ test "a pending connection started in another browser is not reused" do
+ stub_authorization_start
+
+ assert_difference -> { @family.enable_banking_items.count }, 2 do
+ start_new_connection
+ reset!
+ sign_in users(:family_admin)
+ start_new_connection
+ end
+ end
+
+ test "a start that failed at the provider is reused on the next attempt" do
+ Provider::EnableBanking.any_instance.stubs(:get_aspsps).returns(
+ aspsps: [ { name: "Test Bank", country: "DE", psu_types: [ "personal" ], auth_methods: [ { approach: "REDIRECT" } ] } ]
+ )
+ Provider::EnableBanking.any_instance.stubs(:start_authorization)
+ .raises(Provider::EnableBanking::EnableBankingError.new("REDIRECT_URI_NOT_ALLOWED"))
+
+ assert_difference -> { @family.enable_banking_items.count }, 1 do
+ 3.times do
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true" }
+ end
+ end
+ end
+
+ test "a connection scheduled for deletion is not reused" do
+ stub_authorization_start
+ start_new_connection
+ @family.enable_banking_items.order(:created_at).last.update!(scheduled_for_deletion: true)
+
+ assert_difference -> { @family.enable_banking_items.count }, 1 do
+ start_new_connection
+ end
+ end
+
+ # Two new logins to the same bank now share the pending connection, so only
+ # the newest may finish: otherwise the first login's accounts would end up on
+ # the second login's session.
+ test "of two new logins to the same bank, only the newest can complete" do
+ stub_authorization_start
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true" }
+ first_state = @sent_state
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true" }
+ second_state = @sent_state
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).once
+ get callback_enable_banking_items_url, params: { code: "first", state: first_state }
+ assert_redirected_to settings_providers_path
+ get callback_enable_banking_items_url, params: { code: "second", state: second_state }
+ assert_redirected_to accounts_path
+ end
+
+ test "a retry that fails to start leaves the earlier login able to complete" do
+ stub_authorization_start
+ start_new_connection
+ first_state = @sent_state
+ Provider::EnableBanking.any_instance.stubs(:start_authorization)
+ .raises(Provider::EnableBanking::EnableBankingError.new("ASPSP unavailable"))
+ start_new_connection
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "first")
+ get callback_enable_banking_items_url, params: { code: "first", state: first_state }
+ assert_redirected_to accounts_path
+ end
+
+ # Each failed retry used to add a dead state, so enough of them pushed the
+ # earlier login's state out of the capped map.
+ test "repeated retries that fail to start leave the earlier login able to complete" do
+ stub_authorization_start
+ start_new_connection
+ first_state = @sent_state
+ Provider::EnableBanking.any_instance.stubs(:start_authorization)
+ .raises(Provider::EnableBanking::EnableBankingError.new("ASPSP unavailable"))
+ EnableBankingItemsController::MAX_PENDING_OAUTH_STATES.times { start_new_connection }
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "first")
+ get callback_enable_banking_items_url, params: { code: "first", state: first_state }
+ assert_redirected_to accounts_path
+ end
+
+ test "a retry the provider sends to an untrusted host leaves the earlier login able to complete" do
+ stub_authorization_start
+ start_new_connection
+ first_state = @sent_state
+ Provider::EnableBanking.any_instance.stubs(:start_authorization)
+ .returns(url: "https://api.enablebanking.com.evil.test/auth", authorization_id: "auth_2")
+ start_new_connection
+ assert_redirected_to settings_providers_path
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "first")
+ get callback_enable_banking_items_url, params: { code: "first", state: first_state }
+ assert_redirected_to accounts_path
+ end
+
+ # The retry's older state is dropped before the cap applies, so it never
+ # evicts another bank's login that is still open.
+ test "retrying one login keeps the other logins open when the limit is reached" do
+ banks = [ "Bank B", "Bank A", "Bank C", "Bank D", "Bank E" ]
+ assert_equal EnableBankingItemsController::MAX_PENDING_OAUTH_STATES, banks.size
+ stub_authorization_start(banks: banks)
+ start_new_connection(bank: "Bank B")
+ bank_b_state = @sent_state
+ banks.drop(1).each { |bank| start_new_connection(bank: bank) }
+ start_new_connection(bank: "Bank A")
+ assert_redirected_to "https://api.enablebanking.com/auth/redirect/abc"
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "bank b")
+ get callback_enable_banking_items_url, params: { code: "bank b", state: bank_b_state }
+ assert_redirected_to accounts_path
+ end
+
+ test "reauthorize issues a state the callback accepts" do
+ stub_authorization_start
+ @item.update!(aspsp_name: "Test Bank")
+
+ post reauthorize_enable_banking_item_url(@item)
+ assert_redirected_to "https://api.enablebanking.com/auth/redirect/abc"
+ EnableBankingItem.any_instance.stubs(:sync_later)
+
+ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "reauth")
+ get callback_enable_banking_items_url, params: { code: "reauth", state: @sent_state }
+ assert_redirected_to accounts_path
+ end
+
test "callback completes authorization when the state matches this session" do
stub_authorization_start
post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }
@@ -446,9 +626,14 @@ class EnableBankingItemsControllerTest < ActionDispatch::IntegrationTest
private
- def stub_authorization_start
+ def start_new_connection(bank: "Test Bank")
+ post authorize_enable_banking_item_url(@item),
+ params: { aspsp_name: bank, psu_type: "personal", new_connection: "true" }
+ end
+
+ def stub_authorization_start(banks: [ "Test Bank" ])
Provider::EnableBanking.any_instance.stubs(:get_aspsps).returns(
- aspsps: [ { name: "Test Bank", country: "DE", psu_types: [ "personal" ], auth_methods: [ { approach: "REDIRECT" } ] } ]
+ aspsps: banks.map { |name| { name: name, country: "DE", psu_types: [ "personal" ], auth_methods: [ { approach: "REDIRECT" } ] } }
)
test_case = self
Provider::EnableBanking.any_instance.stubs(:start_authorization).with do |**kwargs|