>From e380a4482efaa0cd5df7fac9bf0834254d4b6ca7 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Wed, 8 Apr 2015 17:23:13 +0200 Subject: [PATCH] Prevent open redirect to arbitrary hosts --- app/controllers/application_controller.rb | 34 +++++++++++++++++++++++------- test/functional/account_controller_test.rb | 10 ++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6c16f99..2388fc9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -393,8 +393,8 @@ class ApplicationController < ActionController::Base def redirect_back_or_default(default, options={}) back_url = params[:back_url].to_s - if back_url.present? && valid_back_url?(back_url) - redirect_to(back_url) + if back_url.present? && valid_url = validate_back_url(back_url) + redirect_to(valid_url) return elsif options[:referer] redirect_to_referer_or default @@ -404,8 +404,9 @@ class ApplicationController < ActionController::Base false end - # Returns true if back_url is a valid url for redirection, otherwise false - def valid_back_url?(back_url) + # Returns a validated URL string if back_url is a valid url for redirection, + # otherwise false + def validate_back_url(back_url) if CGI.unescape(back_url).include?('..') return false end @@ -416,19 +417,36 @@ class ApplicationController < ActionController::Base return false end - if uri.host.present? && uri.host != request.host + [:scheme, :host, :port].each do |component| + if uri.send(component).present? && uri.send(component) != request.send(component) + return false + end + uri.send(:"#{component}=", nil) + end + # Always ignore basic user:password in the URL + uri.userinfo = nil + + path = uri.to_s + # Ensure that the remaining URL starts with a slash, followed by a + # non-slash character or the end + if path !~ %r{\A/([^/]|\z)} return false end - if uri.path.match(%r{/(login|account/register)}) + if path.match(%r{/(login|account/register)}) return false end - if relative_url_root.present? && !uri.path.starts_with?(relative_url_root) + if relative_url_root.present? && !path.starts_with?(relative_url_root) return false end - return true + return path + end + private :validate_back_url + + def valid_back_url?(back_url) + !!validate_back_url(back_url) end private :valid_back_url? diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index e7b4c4f..eea9237 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -63,6 +63,7 @@ class AccountControllerTest < ActionController::TestCase # request.uri is "test.host" in test environment back_urls = [ 'http://test.host/issues/show/1', + 'http://test.host/', '/' ] back_urls.each do |back_url| @@ -90,7 +91,14 @@ class AccountControllerTest < ActionController::TestCase def test_login_should_not_redirect_to_another_host back_urls = [ 'http://test.foo/fake', - '//test.foo/fake' + '//test.foo/fake', + 'http://test.host//fake', + 'http://test.host/\n//fake', + '//bar@test.foo', + '//test.foo', + '////test.foo', + '@test.foo', + 'fake@test.foo' ] back_urls.each do |back_url| post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url -- 2.2.2