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

Add allow_other_host option to redirect_back method #30850

Merged
merged 1 commit into from
Oct 10, 2017

Conversation

timfjord
Copy link
Contributor

This PR adds allow_other_host option to redirect_back method
When allow_other_host: false redirection back to URLs from different host is not allowed
allow_other_host is true by default to make it backward compatible

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

def redirect_back(fallback_location:, **args)
if referer = request.headers["Referer"]
def redirect_back(fallback_location:, allow_other_host: true, **args)
if (referer = request.headers["Referer"]) && (allow_other_host || URI(referer).host == request.host)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URI(referer) can raise exceptions. Should we make sure that returns false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it raises an error on non-string attribute.
So what if I use URI(referer.to_s).host == request.host?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are more cases where it could raise. I'd prefer to call to_s but also rescue the possible exceptions and return false in those cases.

@@ -82,8 +82,8 @@ def redirect_to(options = {}, response_status = {})
#
# All options that can be passed to <tt>redirect_to</tt> are accepted as
# options and the behavior is identical.
def redirect_back(fallback_location:, **args)
if referer = request.headers["Referer"]
def redirect_back(fallback_location:, allow_other_host: true, **args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document this new option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will do

@timfjord
Copy link
Contributor Author

@rafaelfranca Requested changes are in place. Please take a look

referer = request.headers["Referer"]
redirect_to_referer = begin
referer && (allow_other_host || URI(referer.to_s).host == request.host)
rescue ArgumentError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also rescue URI:: Error and better to move URI(referer.to_s).host == request.host to a private method.

@timfjord
Copy link
Contributor Author

Done. I think we also need to cover bad URL case with tests

@rafaelfranca rafaelfranca merged commit b205ea2 into rails:master Oct 10, 2017
@timfjord timfjord deleted the safe-redirect-back branch October 11, 2017 04:36
@bogdanvlviv
Copy link
Contributor

@Timsly @rafaelfranca I think we need to add changelog entry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants