-
Notifications
You must be signed in to change notification settings - Fork 21.8k
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
Conversation
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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Will do
9d30edf
to
5c45b22
Compare
@rafaelfranca Requested changes are in place. Please take a look |
5c45b22
to
ce38a2b
Compare
referer = request.headers["Referer"] | ||
redirect_to_referer = begin | ||
referer && (allow_other_host || URI(referer.to_s).host == request.host) | ||
rescue ArgumentError |
There was a problem hiding this comment.
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.
ce38a2b
to
0db6a14
Compare
Done. I think we also need to cover bad URL case with tests |
@Timsly @rafaelfranca I think we need to add changelog entry |
…_back` method [ci skip] Related to rails#30850
This PR adds
allow_other_host
option toredirect_back
methodWhen
allow_other_host: false
redirection back to URLs from different host is not allowedallow_other_host
istrue
by default to make it backward compatible