The personal finance app for everyone (by everyone)
Ruby
Share one return_to check across eight provider controllers (#5343)
jaysbeekay905ec68c6f62
diff --git a/app/controllers/akahu_items_controller.rb b/app/controllers/akahu_items_controller.rbindex dfbe969255..38612407ed 100644--- a/app/controllers/akahu_items_controller.rb+++ b/app/controllers/akahu_items_controller.rb@@ -1,4 +1,6 @@class AkahuItemsController < ApplicationController+ include SafeReturnPath+before_action :set_akahu_item, only: [ :show, :edit, :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]before_action :require_admin!, only: [:new, :create, :preload_accounts, :select_accounts, :link_accounts,@@ -305,33 +307,4 @@ class AkahuItemsController < ApplicationControllerskip_initial_sync: true)end-- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/app/controllers/coinspot_items_controller.rb b/app/controllers/coinspot_items_controller.rbindex 5c8b3cbf43..10207a01be 100644--- a/app/controllers/coinspot_items_controller.rb+++ b/app/controllers/coinspot_items_controller.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass CoinspotItemsController < ApplicationController+ include SafeReturnPath+before_action :set_coinspot_item, only: %i[update destroy sync setup_accounts complete_account_setup]before_action :require_admin!, only: %i[create select_accounts link_accounts select_existing_account link_existing_account@@ -187,7 +189,7 @@ class CoinspotItemsController < ApplicationController# instead of left half-set-up.def complete_account_setupselected_accounts = Array(params[:selected_accounts]).reject(&:blank?)- return_to = safe_return_to_path(params[:return_to])+ return_to = safe_return_to_pathcreated_accounts = []failed_accounts = 0@@ -302,32 +304,4 @@ class CoinspotItemsController < ApplicationControllerredirect_to fallback_path, alert: messageendend-- # Validates params[:return_to] is a same-origin relative path before- # using it as a redirect target, rejecting anything with a scheme, host,- # backslashes, control characters, or encoded separators (open-redirect protection).- def safe_return_to_path(value = nil)- return_value = value || params[:return_to]- return nil if return_value.blank?-- return_to = return_value.to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- %w[%2F %5C].include?(encoded_second_character)- endenddiff --git a/app/controllers/concerns/safe_return_path.rb b/app/controllers/concerns/safe_return_path.rbnew file mode 100644index 0000000000..849d9cbe64--- /dev/null+++ b/app/controllers/concerns/safe_return_path.rb@@ -0,0 +1,39 @@+# The `return_to` param as a path on this site, or nil. Rejects absolute and+# protocol-relative URLs (`//host`, `/\host`, and their percent-encoded forms),+# backslashes, control characters and paths that do not route, so a crafted+# link can't send the user off the site after a provider flow. A bare `/` is+# rejected too, so the caller's own default applies instead of the dashboard.+module SafeReturnPath+ extend ActiveSupport::Concern++ private+ def safe_return_to_path+ return nil if params[:return_to].blank?++ return_to = params[:return_to].to_s.strip+ return nil unless return_to.start_with?("/")+ return nil if return_to == "/"+ return nil if return_to[1] == "/" || return_to[1] == "\\"+ return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)+ return nil if encoded_path_separator?(return_to)++ uri = URI.parse(return_to)+ return nil unless uri.relative?++ Rails.application.routes.recognize_path(uri.path, method: :get)++ return_to+ rescue URI::InvalidURIError, ActionController::RoutingError+ nil+ end++ def encoded_path_separator?(return_to)+ encoded_second_character = return_to[1, 3]+ return false unless encoded_second_character&.start_with?("%")++ decoded = URI.decode_www_form_component(encoded_second_character)+ decoded == "/" || decoded == "\\"+ rescue ArgumentError+ true+ end+enddiff --git a/app/controllers/fio_items_controller.rb b/app/controllers/fio_items_controller.rbindex 0afff29a5c..392e6ecb1b 100644--- a/app/controllers/fio_items_controller.rb+++ b/app/controllers/fio_items_controller.rb@@ -9,6 +9,8 @@# every screen here works on `fio_item.fio_account` — discovered by the first sync — and# never offers a picker.class FioItemsController < ApplicationController+ include SafeReturnPath+before_action :set_fio_item, only: [ :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]before_action :require_admin!, only: [:create, :select_accounts, :link_accounts,@@ -356,36 +358,4 @@ class FioItemsController < ApplicationControllerskip_initial_sync: true)end-- # Validate the return_to param as a safe in-app relative path, or nil.- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- # True if the path's second char is a percent-encoded slash/backslash- # (used to block protocol-relative redirect bypasses).- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/app/controllers/kraken_items_controller.rb b/app/controllers/kraken_items_controller.rbindex 3b0f05633e..4118561a09 100644--- a/app/controllers/kraken_items_controller.rb+++ b/app/controllers/kraken_items_controller.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass KrakenItemsController < ApplicationController+ include SafeReturnPath+before_action :set_kraken_item, only: %i[update destroy sync setup_accounts complete_account_setup]before_action :require_admin!, only: %i[create select_accounts link_accounts select_existing_account link_existing_account update destroy sync setup_accounts complete_account_setup]@@ -203,33 +205,4 @@ class KrakenItemsController < ApplicationControllerredirect_to fallback_path, alert: messageendend-- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/app/controllers/monobank_items_controller.rb b/app/controllers/monobank_items_controller.rbindex a1cc10f6f2..e14748a41c 100644--- a/app/controllers/monobank_items_controller.rb+++ b/app/controllers/monobank_items_controller.rb@@ -3,6 +3,8 @@# through the dialogs in app/views/monobank_items. There is deliberately no# index/show/new/edit action: nothing links to them and they would have no template.class MonobankItemsController < ApplicationController+ include SafeReturnPath+before_action :set_monobank_item, only: [ :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]before_action :require_admin!, only: [:create, :preload_accounts, :select_accounts, :link_accounts,@@ -361,36 +363,4 @@ class MonobankItemsController < ApplicationControllerskip_initial_sync: true)end-- # Validate the return_to param as a safe in-app relative path, or nil.- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- # True if the path's second char is a percent-encoded slash/backslash- # (used to block protocol-relative redirect bypasses).- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/app/controllers/questrade_items_controller.rb b/app/controllers/questrade_items_controller.rbindex 0fb4677774..a0702a6cba 100644--- a/app/controllers/questrade_items_controller.rb+++ b/app/controllers/questrade_items_controller.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass QuestradeItemsController < ApplicationController+ include SafeReturnPath+ALLOWED_ACCOUNTABLE_TYPES = %w[Depository CreditCard Investment Loan OtherAsset OtherLiability Crypto Property Vehicle].freezebefore_action :set_questrade_item, only: [ :show, :edit, :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]@@ -342,33 +344,4 @@ class QuestradeItemsController < ApplicationControlleraccountable_type.constantizeend-- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/app/controllers/redbark_items_controller.rb b/app/controllers/redbark_items_controller.rbindex 2862c3eb7a..f9a86d1dad 100644--- a/app/controllers/redbark_items_controller.rb+++ b/app/controllers/redbark_items_controller.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass RedbarkItemsController < ApplicationController+ include SafeReturnPath+ALLOWED_ACCOUNTABLE_TYPES = %w[Depository CreditCard Investment Loan OtherAsset OtherLiability Crypto Property Vehicle].freezebefore_action :set_redbark_item, only: [ :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]@@ -220,36 +222,6 @@ class RedbarkItemsController < ApplicationControllert("redbark_items.setup_accounts.api_error", message: e.message)end- # Only allow internal relative paths in return_to- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- end-def redbark_item_paramsparams.require(:redbark_item).permit(:name,diff --git a/app/controllers/up_items_controller.rb b/app/controllers/up_items_controller.rbindex 48b3a7bad7..cdb58d0692 100644--- a/app/controllers/up_items_controller.rb+++ b/app/controllers/up_items_controller.rb@@ -1,4 +1,6 @@class UpItemsController < ApplicationController+ include SafeReturnPath+before_action :set_up_item, only: [ :show, :edit, :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]before_action :require_admin!, only: [:new, :create, :preload_accounts, :select_accounts, :link_accounts,@@ -373,36 +375,4 @@ class UpItemsController < ApplicationControllerskip_initial_sync: true)end-- # Validate the return_to param as a safe in-app relative path, or nil.- def safe_return_to_path- return nil if params[:return_to].blank?-- return_to = params[:return_to].to_s.strip- return nil unless return_to.start_with?("/")- return nil if return_to[1] == "/" || return_to[1] == "\\"- return nil if return_to.include?("\\") || return_to.match?(/[[:cntrl:]]/)- return nil if encoded_path_separator?(return_to)-- uri = URI.parse(return_to)- return nil unless uri.relative?-- Rails.application.routes.recognize_path(uri.path, method: :get)-- return_to- rescue URI::InvalidURIError, ActionController::RoutingError- nil- end-- # True if the path's second char is a percent-encoded slash/backslash- # (used to block protocol-relative redirect bypasses).- def encoded_path_separator?(return_to)- encoded_second_character = return_to[1, 3]- return false unless encoded_second_character&.start_with?("%")-- decoded = URI.decode_www_form_component(encoded_second_character)- decoded == "/" || decoded == "\\"- rescue ArgumentError- true- endenddiff --git a/test/controllers/concerns/safe_return_path_test.rb b/test/controllers/concerns/safe_return_path_test.rbnew file mode 100644index 0000000000..dffcb199f1--- /dev/null+++ b/test/controllers/concerns/safe_return_path_test.rb@@ -0,0 +1,55 @@+require "test_helper"++class SafeReturnPathTest < ActiveSupport::TestCase+ class Host+ include SafeReturnPath++ attr_reader :params++ def initialize(return_to)+ @params = ActionController::Parameters.new(return_to: return_to)+ end++ def call+ safe_return_to_path+ end+ end++ test "keeps a path on this site, with its query string and without surrounding whitespace" do+ assert_equal "/accounts", Host.new("/accounts").call+ assert_equal "/accounts?tab=manual", Host.new("/accounts?tab=manual").call+ assert_equal "/accounts", Host.new(" /accounts ").call+ end++ test "rejects paths that leave the site or cannot be trusted" do+ [+ "https://evil.example/accounts",+ "//evil.example",+ "///evil.example",+ "/\\evil.example",+ "/accounts\\evil.example",+ "/\t/evil.example",+ "/acc\tounts",+ "/%2Fevil.example",+ "/%2fevil.example",+ "/%5Cevil.example",+ "/%zzevil.example",+ "accounts"+ ].each do |return_to|+ assert_nil Host.new(return_to).call, "#{return_to.inspect} should be rejected"+ end+ end++ test "rejects a path that does not route" do+ assert_nil Host.new("/no-such-route-here").call+ end++ test "rejects a bare slash so the caller's own default applies" do+ assert_nil Host.new("/").call+ end++ test "returns nil when no return path is given" do+ assert_nil Host.new(nil).call+ assert_nil Host.new("").call+ end+end