The personal finance app for everyone (by everyone)
Ruby
Bind the Enable Banking OAuth state to the browser session (#3996)
GFR12961a26cfa9
diff --git a/app/controllers/enable_banking_items_controller.rb b/app/controllers/enable_banking_items_controller.rbindex 041af65c27..d4bcb48c92 100644--- a/app/controllers/enable_banking_items_controller.rb+++ b/app/controllers/enable_banking_items_controller.rb@@ -1,9 +1,12 @@class EnableBankingItemsController < ApplicationControllerinclude EnableBankingItems::MapsHelperbefore_action :set_enable_banking_item, only: [ :update, :destroy, :sync, :select_bank, :authorize, :reauthorize, :setup_accounts, :complete_account_setup, :new_connection ]- before_action :require_admin!, only: [ :new, :create, :link_accounts, :select_existing_account, :link_existing_account, :update, :destroy, :sync, :select_bank, :authorize, :reauthorize, :setup_accounts, :complete_account_setup, :new_connection ]+ before_action :require_admin!, only: [ :new, :create, :link_accounts, :select_existing_account, :link_existing_account, :update, :destroy, :sync, :select_bank, :authorize, :callback, :reauthorize, :setup_accounts, :complete_account_setup, :new_connection ]skip_before_action :verify_authenticity_token, only: [ :callback ]+ OAUTH_STATE_SESSION_KEY = :enable_banking_oauth_states+ MAX_PENDING_OAUTH_STATES = 5+def new@enable_banking_item = Current.family.enable_banking_items.buildend@@ -112,7 +115,7 @@ class EnableBankingItemsController < ApplicationControllerredirect_url = target_item.begin_authorization!(aspsp_name: aspsp_name,redirect_url: enable_banking_callback_url,- state: target_item.id,+ state: issue_oauth_state_for(target_item),psu_type: psu_type,language: language)@@ -157,8 +160,10 @@ class EnableBankingItemsController < ApplicationControllerreturnend- # Find the enable_banking_item by ID from state- enable_banking_item = Current.family.enable_banking_items.find_by(id: state)+ # The state must match the one issued to this browser session, so a+ # callback link crafted by someone else cannot attach their bank+ # authorization code to this family's connection.+ enable_banking_item = consume_oauth_state(state)unless enable_banking_item.present?redirect_to settings_providers_path, alert: t(".item_not_found", default: "Connection not found.")@@ -200,7 +205,7 @@ class EnableBankingItemsController < ApplicationController# 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: @enable_banking_item.id,+ state: issue_oauth_state_for(@enable_banking_item),language: language)@@ -506,6 +511,31 @@ class EnableBankingItemsController < ApplicationControllerendprivate+ # 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)+ state = SecureRandom.urlsafe_base64(32)+ pending = pending_oauth_states.merge(state => item.id)+ 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+ # made-up state cannot cancel a real flow in progress.+ def consume_oauth_state(state)+ pending = pending_oauth_states+ matched = pending.keys.find { |candidate| ActiveSupport::SecurityUtils.secure_compare(candidate, state.to_s) }+ return unless matched++ item_id = pending.delete(matched)+ session[OAUTH_STATE_SESSION_KEY] = pending+ Current.family.enable_banking_items.find_by(id: item_id)+ end++ def pending_oauth_states+ pending = session[OAUTH_STATE_SESSION_KEY]+ pending.is_a?(Hash) ? pending.to_h : {}+ enddef set_enable_banking_item@enable_banking_item = Current.family.enable_banking_items.find(params[:id])diff --git a/test/controllers/enable_banking_items_controller_test.rb b/test/controllers/enable_banking_items_controller_test.rbindex 244c9b100b..b57cb29e2f 100644--- a/test/controllers/enable_banking_items_controller_test.rb+++ b/test/controllers/enable_banking_items_controller_test.rb@@ -119,4 +119,89 @@ class EnableBankingItemsControllerTest < ActionDispatch::IntegrationTestassert_nil flash[:alert]assert_equal "DECOUPLED", @item.reload.aspsp_auth_approachend++ test "authorize sends a random state instead of the item id" do+ stub_authorization_start++ post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }++ assert_redirected_to "https://api.enablebanking.com/auth/redirect/abc"+ assert_not_equal @item.id, @sent_state+ assert @sent_state.length >= 32+ 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" }++ EnableBankingItem.any_instance.expects(:complete_authorization).with(code: "good-code")+ EnableBankingItem.any_instance.stubs(:sync_later)++ get callback_enable_banking_items_url, params: { code: "good-code", state: @sent_state }++ assert_redirected_to accounts_path+ end++ test "callback rejects the item id as state" do+ stub_authorization_start+ post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }++ EnableBankingItem.any_instance.expects(:complete_authorization).never++ get callback_enable_banking_items_url, params: { code: "attacker-code", state: @item.id }++ assert_redirected_to settings_providers_path+ end++ test "callback rejects a state that was not issued to this session" do+ EnableBankingItem.any_instance.expects(:complete_authorization).never++ get callback_enable_banking_items_url, params: { code: "attacker-code", state: SecureRandom.urlsafe_base64(32) }++ assert_redirected_to settings_providers_path+ end++ test "callback state can only be used once" do+ stub_authorization_start+ post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }+ EnableBankingItem.any_instance.stubs(:complete_authorization)+ EnableBankingItem.any_instance.stubs(:sync_later)+ get callback_enable_banking_items_url, params: { code: "good-code", state: @sent_state }++ EnableBankingItem.any_instance.expects(:complete_authorization).never+ get callback_enable_banking_items_url, params: { code: "replayed-code", state: @sent_state }++ assert_redirected_to settings_providers_path+ end++ test "parallel authorizations each keep their own state" do+ stub_authorization_start+ post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }+ first_state = @sent_state+ post authorize_enable_banking_item_url(@item), params: { aspsp_name: "Test Bank", psu_type: "personal" }+ second_state = @sent_state+ EnableBankingItem.any_instance.stubs(:sync_later)++ # A stray callback with a made-up state must not cancel pending flows.+ get callback_enable_banking_items_url, params: { code: "stray", state: "made-up" }++ EnableBankingItem.any_instance.expects(:complete_authorization).twice+ get callback_enable_banking_items_url, params: { code: "first", state: first_state }+ assert_redirected_to accounts_path+ get callback_enable_banking_items_url, params: { code: "second", state: second_state }+ assert_redirected_to accounts_path+ end++ private++ def stub_authorization_start+ Provider::EnableBanking.any_instance.stubs(:get_aspsps).returns(+ aspsps: [ { name: "Test Bank", country: "DE", psu_types: [ "personal" ], auth_methods: [ { approach: "REDIRECT" } ] } ]+ )+ test_case = self+ Provider::EnableBanking.any_instance.stubs(:start_authorization).with do |**kwargs|+ test_case.instance_variable_set(:@sent_state, kwargs[:state])+ true+ end.returns(url: "https://api.enablebanking.com/auth/redirect/abc", authorization_id: "auth_1")+ endend