diff options
author | Prem Sichanugrist <s@sikachu.com> | 2010-01-21 04:37:10 +0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-21 10:08:26 +0100 |
commit | bd4f21fbac495fb28b6be993be808509e567239e (patch) | |
tree | facad12a926cd08f40ebf97da6d336cb8e03e6d9 /actionpack/lib/action_dispatch/http | |
parent | fa9f000246c2f6010f18bf40237d105b782873e2 (diff) | |
download | rails-bd4f21fbac495fb28b6be993be808509e567239e.tar.gz rails-bd4f21fbac495fb28b6be993be808509e567239e.tar.bz2 rails-bd4f21fbac495fb28b6be993be808509e567239e.zip |
Move filter_parameter_logging logic out of the controller and create ActionDispatch::ParametersFilter to handle parameter filteration instead. This will make filteration not depending on controller anymore.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack/lib/action_dispatch/http')
-rw-r--r-- | actionpack/lib/action_dispatch/http/parameters.rb | 23 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/parameters_filter.rb | 88 | ||||
-rwxr-xr-x | actionpack/lib/action_dispatch/http/request.rb | 1 |
3 files changed, 89 insertions, 23 deletions
diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb index 68ba3637bf..40b40ea94e 100644 --- a/actionpack/lib/action_dispatch/http/parameters.rb +++ b/actionpack/lib/action_dispatch/http/parameters.rb @@ -30,29 +30,6 @@ module ActionDispatch @env["action_dispatch.request.path_parameters"] ||= {} end - def filter_parameters - # TODO: Remove dependency on controller - if controller = @env['action_controller.instance'] - controller.send(:filter_parameters, params) - else - params - end - end - - def filter_env - if controller = @env['action_controller.instance'] - @env.map do |key, value| - if (key =~ /RAW_POST_DATA/i) - '[FILTERED]' - else - controller.send(:filter_parameters, {key => value}).values[0] - end - end - else - env - end - end - private # Convert nested Hashs to HashWithIndifferentAccess def normalize_parameters(value) diff --git a/actionpack/lib/action_dispatch/http/parameters_filter.rb b/actionpack/lib/action_dispatch/http/parameters_filter.rb new file mode 100644 index 0000000000..bec5e7427d --- /dev/null +++ b/actionpack/lib/action_dispatch/http/parameters_filter.rb @@ -0,0 +1,88 @@ +require 'active_support/core_ext/hash/keys' + +module ActionDispatch + module Http + module ParametersFilter + INTERNAL_PARAMS = %w(controller action format _method only_path) + + @@filter_parameters = nil + @@filter_parameters_block = nil + + # Specify sensitive parameters which will be replaced from the request log. + # Filters parameters that have any of the arguments as a substring. + # Looks in all subhashes of the param hash for keys to filter. + # If a block is given, each key and value of the parameter hash and all + # subhashes is passed to it, the value or key + # can be replaced using String#replace or similar method. + # + # Examples: + # + # ActionDispatch::Http::ParametersFilter.filter_parameters :password + # => replaces the value to all keys matching /password/i with "[FILTERED]" + # + # ActionDispatch::Http::ParametersFilter.filter_parameters :foo, "bar" + # => replaces the value to all keys matching /foo|bar/i with "[FILTERED]" + # + # ActionDispatch::Http::ParametersFilter.filter_parameters do |k,v| + # v.reverse! if k =~ /secret/i + # end + # => reverses the value to all keys matching /secret/i + # + # ActionDispatch::Http::ParametersFilter.filter_parameters(:foo, "bar") do |k,v| + # v.reverse! if k =~ /secret/i + # end + # => reverses the value to all keys matching /secret/i, and + # replaces the value to all keys matching /foo|bar/i with "[FILTERED]" + def self.filter_parameters(*filter_words, &block) + raise "You must filter at least one word" if filter_words.empty? and !block_given? + + @@filter_parameters = filter_words.empty? ? nil : Regexp.new(filter_words.join('|'), true) + @@filter_parameters_block = block + end + + # Return a hash of parameters with all sensitive data replaced. + def filtered_parameters + @filtered_parameters ||= process_parameter_filter(parameters) + end + alias_method :fitered_params, :filtered_parameters + + # Return a hash of request.env with all sensitive data replaced. + def filtered_env + @env.merge(@env) do |key, value| + if (key =~ /RAW_POST_DATA/i) + '[FILTERED]' + else + process_parameter_filter({key => value}, false).values[0] + end + end + end + + protected + + def process_parameter_filter(original_parameters, validate_block = true) + if @@filter_parameters or @@filter_parameters_block + filtered_params = {} + + original_parameters.each do |key, value| + if key =~ @@filter_parameters + value = '[FILTERED]' + elsif value.is_a?(Hash) + value = process_parameter_filter(value) + elsif value.is_a?(Array) + value = value.map { |item| process_parameter_filter({key => item}, validate_block).values[0] } + elsif validate_block and @@filter_parameters_block + key = key.dup + value = value.dup if value.duplicable? + value = @@filter_parameters_block.call(key, value) || value + end + + filtered_params[key] = value + end + filtered_params.except!(*INTERNAL_PARAMS) + else + original_parameters.except(*INTERNAL_PARAMS) + end + end + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 187ce7c15d..7c3a228149 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -11,6 +11,7 @@ module ActionDispatch include ActionDispatch::Http::Cache::Request include ActionDispatch::Http::MimeNegotiation include ActionDispatch::Http::Parameters + include ActionDispatch::Http::ParametersFilter include ActionDispatch::Http::Upload include ActionDispatch::Http::URL |