Skip to content

Commit

Permalink
Merge pull request #45485 from santib/fix-open-redirects
Browse files Browse the repository at this point in the history
Fix vulnerability in open redirects
  • Loading branch information
rafaelfranca committed Jun 28, 2022
2 parents 7ebda0a + 708bb9d commit c3c7475
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/metal/redirecting.rb
Expand Up @@ -195,7 +195,8 @@ def _enforce_open_redirect_protection(location, allow_other_host:)
end

def _url_host_allowed?(url)
[request.host, nil].include?(URI(url.to_s).host)
host = URI(url.to_s).host
host == request.host || host.nil? && url.to_s.start_with?("/")
rescue ArgumentError, URI::Error
false
end
Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/controller/redirect_test.rb
Expand Up @@ -88,6 +88,10 @@ def unsafe_redirect_back
redirect_back_or_to "http://www.rubyonrails.org/"
end

def unsafe_redirect_malformed
redirect_to "http:///www.rubyonrails.org/"
end

def only_path_redirect
redirect_to action: "other_host", only_path: true
end
Expand Down Expand Up @@ -504,6 +508,16 @@ def test_unsafe_redirect_back
end
end

def test_unsafe_redirect_with_malformed_url
with_raise_on_open_redirects do
error = assert_raise(ActionController::Redirecting::UnsafeRedirectError) do
get :unsafe_redirect_malformed
end

assert_equal "Unsafe redirect to \"http:///www.rubyonrails.org/\", pass allow_other_host: true to redirect anyway.", error.message
end
end

def test_only_path_redirect
with_raise_on_open_redirects do
get :only_path_redirect
Expand Down

0 comments on commit c3c7475

Please sign in to comment.