Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix open redirect vulnerability #945

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/clearance/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ def redirect_back_or(default)
def return_to
if return_to_url
uri = URI.parse(return_to_url)
"#{uri.path}?#{uri.query}".chomp("?") + "##{uri.fragment}".chomp("#")
path = path_without_leading_slashes(uri)
"#{path}?#{uri.query}".chomp("?") + "##{uri.fragment}".chomp("#")
end
end

# @api private
def path_without_leading_slashes(uri)
uri.path.sub(/\A\/+/, "/")
end

# @api private
def return_to_url
session[:return_to]
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@
end

context "with good credentials and a session return url" do
it "redirects to the return URL removing leading slashes" do
user = create(:user)
url = "/url_in_the_session?foo=bar#baz"
return_url = "//////#{url}"
request.session[:return_to] = return_url

post :create, params: {
session: { email: user.email, password: user.password },
}

should redirect_to(url)
end

it "redirects to the return URL maintaining query and fragment" do
user = create(:user)
return_url = "/url_in_the_session?foo=bar#baz"
Expand Down