diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-05 02:21:44 -0800 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-05 02:21:44 -0800 |
commit | e905639af2fe947ff7e1e2331e9216032dff8b3e (patch) | |
tree | 12363a0b618b5cce5228779257036aa0c601e850 /actionpack | |
parent | 4af9be089c345c58b6eae462226ca7190bc6bb73 (diff) | |
parent | 86e3aaab939a67536f007d1633ec19521dba15e9 (diff) | |
download | rails-e905639af2fe947ff7e1e2331e9216032dff8b3e.tar.gz rails-e905639af2fe947ff7e1e2331e9216032dff8b3e.tar.bz2 rails-e905639af2fe947ff7e1e2331e9216032dff8b3e.zip |
Merge pull request #8404 from freegenie/filter_redirects
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/instrumentation.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch.rb | 1 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/filter_redirect.rb | 37 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/response.rb | 1 | ||||
-rw-r--r-- | actionpack/test/controller/log_subscriber_test.rb | 22 |
6 files changed, 68 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 43fd727500..91028e48c8 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,4 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Add filter capability to ActionController logs for redirect locations: + + config.filter_redirect << 'http://please.hide.it/' + + *Fabrizio Regini* + * Fixed a bug that ignores constraints on a glob route. This was caused because the constraint regular expression is overwritten when the `routes.rb` file is processed. Fixes #7924 diff --git a/actionpack/lib/action_controller/metal/instrumentation.rb b/actionpack/lib/action_controller/metal/instrumentation.rb index ca4ae532ca..d3aa8f90c5 100644 --- a/actionpack/lib/action_controller/metal/instrumentation.rb +++ b/actionpack/lib/action_controller/metal/instrumentation.rb @@ -60,7 +60,7 @@ module ActionController ActiveSupport::Notifications.instrument("redirect_to.action_controller") do |payload| result = super payload[:status] = response.status - payload[:location] = response.location + payload[:location] = response.filtered_location result end end diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index 1d716a3248..d002babee3 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -75,6 +75,7 @@ module ActionDispatch autoload :Parameters autoload :ParameterFilter autoload :FilterParameters + autoload :FilterRedirect autoload :Upload autoload :UploadedFile, 'action_dispatch/http/upload' autoload :URL diff --git a/actionpack/lib/action_dispatch/http/filter_redirect.rb b/actionpack/lib/action_dispatch/http/filter_redirect.rb new file mode 100644 index 0000000000..900ce1c646 --- /dev/null +++ b/actionpack/lib/action_dispatch/http/filter_redirect.rb @@ -0,0 +1,37 @@ +module ActionDispatch + module Http + module FilterRedirect + + FILTERED = '[FILTERED]'.freeze # :nodoc: + + def filtered_location + if !location_filter.empty? && location_filter_match? + FILTERED + else + location + end + end + + private + + def location_filter + if request.present? + request.env['action_dispatch.redirect_filter'] || [] + else + [] + end + end + + def location_filter_match? + location_filter.any? do |filter| + if String === filter + location.include?(filter) + elsif Regexp === filter + location.match(filter) + end + end + end + + end + end +end diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 11b7534ea4..0f808ac9cf 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -61,6 +61,7 @@ module ActionDispatch # :nodoc: cattr_accessor(:default_headers) include Rack::Response::Helpers + include ActionDispatch::Http::FilterRedirect include ActionDispatch::Http::Cache::Response include MonitorMixin diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb index 9efb6ab95f..7b4d49be20 100644 --- a/actionpack/test/controller/log_subscriber_test.rb +++ b/actionpack/test/controller/log_subscriber_test.rb @@ -26,6 +26,10 @@ module Another redirect_to "http://foo.bar/" end + def filterable_redirector + redirect_to "http://secret.foo.bar/" + end + def data_sender send_data "cool data", :filename => "file.txt" end @@ -152,6 +156,24 @@ class ACLogSubscriberTest < ActionController::TestCase assert_equal "Redirected to http://foo.bar/", logs[1] end + def test_filter_redirect_url_by_string + @request.env['action_dispatch.redirect_filter'] = ['secret'] + get :filterable_redirector + wait + + assert_equal 3, logs.size + assert_equal "Redirected to [FILTERED]", logs[1] + end + + def test_filter_redirect_url_by_regexp + @request.env['action_dispatch.redirect_filter'] = [/secret\.foo.+/] + get :filterable_redirector + wait + + assert_equal 3, logs.size + assert_equal "Redirected to [FILTERED]", logs[1] + end + def test_send_data get :data_sender wait |