diff options
author | Tim Masliuchenko <insside@gmail.com> | 2017-10-10 14:15:56 +0300 |
---|---|---|
committer | Tim Masliuchenko <insside@gmail.com> | 2017-10-10 18:53:48 +0300 |
commit | 0db6a14ae16b143e078375ff7f3c940cf707290b (patch) | |
tree | 18a71f0e06e9fa206c0988c0921544ee81e3917c /actionpack/lib/action_controller | |
parent | ac1ee519fa513f1c2188180e8830938c71edb48c (diff) | |
download | rails-0db6a14ae16b143e078375ff7f3c940cf707290b.tar.gz rails-0db6a14ae16b143e078375ff7f3c940cf707290b.tar.bz2 rails-0db6a14ae16b143e078375ff7f3c940cf707290b.zip |
Add allow_other_host option to redirect_back method
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/metal/redirecting.rb | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/actionpack/lib/action_controller/metal/redirecting.rb b/actionpack/lib/action_controller/metal/redirecting.rb index 5cd8568d8d..b8a80eef31 100644 --- a/actionpack/lib/action_controller/metal/redirecting.rb +++ b/actionpack/lib/action_controller/metal/redirecting.rb @@ -79,15 +79,18 @@ module ActionController # redirect_back fallback_location: "/images/screenshot.jpg" # redirect_back fallback_location: posts_url # redirect_back fallback_location: proc { edit_post_url(@post) } + # redirect_back fallback_location: '/', allow_other_host: false # - # All options that can be passed to <tt>redirect_to</tt> are accepted as + # ==== Options + # * <tt>:fallback_location</tt> - The default fallback location that will be used on missing `Referer` header. + # * <tt>:allow_other_host</tt> - Allows or dissallow redirection to the host that is different to the current host + # + # All other 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"] - redirect_to referer, **args - else - redirect_to fallback_location, **args - end + def redirect_back(fallback_location:, allow_other_host: true, **args) + referer = request.headers["Referer"] + redirect_to_referer = referer && (allow_other_host || _url_host_allowed?(referer)) + redirect_to redirect_to_referer ? referer : fallback_location, **args end def _compute_redirect_to_location(request, options) #:nodoc: @@ -120,5 +123,11 @@ module ActionController 302 end end + + def _url_host_allowed?(url) + URI(url.to_s).host == request.host + rescue ArgumentError, URI::Error + false + end end end |