Navigation Menu

Skip to content

Commit

Permalink
Adding filter capability to ActionController logs
Browse files Browse the repository at this point in the history
  • Loading branch information
freegenie committed Dec 5, 2012
1 parent 129eac0 commit 86e3aaa
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 5 deletions.
6 changes: 6 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,4 +1,10 @@
## Rails 4.0.0 (unreleased) ## ## 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 * 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 regular expression is overwritten when the `routes.rb` file is processed. Fixes #7924


Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/instrumentation.rb
Expand Up @@ -60,7 +60,7 @@ def redirect_to(*args)
ActiveSupport::Notifications.instrument("redirect_to.action_controller") do |payload| ActiveSupport::Notifications.instrument("redirect_to.action_controller") do |payload|
result = super result = super
payload[:status] = response.status payload[:status] = response.status
payload[:location] = response.location payload[:location] = response.filtered_location
result result
end end
end end
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch.rb
Expand Up @@ -75,6 +75,7 @@ module Http
autoload :Parameters autoload :Parameters
autoload :ParameterFilter autoload :ParameterFilter
autoload :FilterParameters autoload :FilterParameters
autoload :FilterRedirect
autoload :Upload autoload :Upload
autoload :UploadedFile, 'action_dispatch/http/upload' autoload :UploadedFile, 'action_dispatch/http/upload'
autoload :URL autoload :URL
Expand Down
37 changes: 37 additions & 0 deletions 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
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/http/response.rb
Expand Up @@ -61,6 +61,7 @@ class Response
cattr_accessor(:default_headers) cattr_accessor(:default_headers)


include Rack::Response::Helpers include Rack::Response::Helpers
include ActionDispatch::Http::FilterRedirect
include ActionDispatch::Http::Cache::Response include ActionDispatch::Http::Cache::Response
include MonitorMixin include MonitorMixin


Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/controller/log_subscriber_test.rb
Expand Up @@ -26,6 +26,10 @@ def redirector
redirect_to "http://foo.bar/" redirect_to "http://foo.bar/"
end end


def filterable_redirector
redirect_to "http://secret.foo.bar/"
end

def data_sender def data_sender
send_data "cool data", :filename => "file.txt" send_data "cool data", :filename => "file.txt"
end end
Expand Down Expand Up @@ -152,6 +156,24 @@ def test_redirect_to
assert_equal "Redirected to http://foo.bar/", logs[1] assert_equal "Redirected to http://foo.bar/", logs[1]
end 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 def test_send_data
get :data_sender get :data_sender
wait wait
Expand Down
27 changes: 24 additions & 3 deletions guides/source/action_controller_overview.md
Expand Up @@ -751,15 +751,36 @@ Now the user can request to get a PDF version of a client just by adding ".pdf"
GET /clients/1.pdf GET /clients/1.pdf
``` ```


Parameter Filtering Log Filtering
------------------- -------------

Rails keeps a log file for each environment in the `log` folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file.


Rails keeps a log file for each environment in the `log` folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. You can filter certain request parameters from your log files by appending them to `config.filter_parameters` in the application configuration. These parameters will be marked [FILTERED] in the log. ### Parameters Filtering

You can filter certain request parameters from your log files by appending them to `config.filter_parameters` in the application configuration. These parameters will be marked [FILTERED] in the log.


```ruby ```ruby
config.filter_parameters << :password config.filter_parameters << :password
``` ```


### Redirects Filtering

Sometimes it's desirable to filter out from log files some sensible locations your application is redirecting to.
You can do that by using the `config.filter_redirect` configuration option:

```ruby
config.filter_redirect << 's3.amazonaws.com'
```

You can set it to a String, a Regexp, or an array of both.

```ruby
config.filter_redirect.concat ['s3.amazonaws.com', /private_path/]
```

Matching URLs will be marked as '[FILTERED]'.

Rescue Rescue
------ ------


Expand Down
2 changes: 2 additions & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -123,6 +123,7 @@ def key_generator
# Currently stores: # Currently stores:
# #
# * "action_dispatch.parameter_filter" => config.filter_parameters # * "action_dispatch.parameter_filter" => config.filter_parameters
# * "action_dispatch.redirect_filter" => config.filter_redirect
# * "action_dispatch.secret_token" => config.secret_token, # * "action_dispatch.secret_token" => config.secret_token,
# * "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions # * "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions
# * "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local # * "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local
Expand All @@ -149,6 +150,7 @@ def env_config


super.merge({ super.merge({
"action_dispatch.parameter_filter" => config.filter_parameters, "action_dispatch.parameter_filter" => config.filter_parameters,
"action_dispatch.redirect_filter" => config.filter_redirect,
"action_dispatch.secret_token" => config.secret_token, "action_dispatch.secret_token" => config.secret_token,
"action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
"action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/application/configuration.rb
Expand Up @@ -13,7 +13,7 @@ class Configuration < ::Rails::Engine::Configuration
:railties_order, :relative_url_root, :secret_key_base, :secret_token, :railties_order, :relative_url_root, :secret_key_base, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options, :serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change, :time_zone, :reload_classes_only_on_change,
:queue, :queue_consumer, :beginning_of_week :queue, :queue_consumer, :beginning_of_week, :filter_redirect


attr_writer :log_level attr_writer :log_level
attr_reader :encoding attr_reader :encoding
Expand All @@ -23,6 +23,7 @@ def initialize(*)
self.encoding = "utf-8" self.encoding = "utf-8"
@consider_all_requests_local = false @consider_all_requests_local = false
@filter_parameters = [] @filter_parameters = []
@filter_redirect = []
@helpers_paths = [] @helpers_paths = []
@serve_static_assets = true @serve_static_assets = true
@static_cache_control = nil @static_cache_control = nil
Expand Down

0 comments on commit 86e3aaa

Please sign in to comment.