Skip to content
This repository
Browse code

fix filtering of nested params

  • Loading branch information...
commit cc0a0ccada1ba00556e4cf65fe3d82140865a388 1 parent 0d6bf34
Hrvoje Simic shime authored
13 lib/exceptional/controller_exception_data.rb
@@ -29,12 +29,13 @@ def extra_stuff
29 29 end
30 30
31 31 def filter_hash(keys_to_filter, hash)
  32 + keys_to_filter.map! {|x| x.to_s}
32 33 if keys_to_filter.is_a?(Array) && !keys_to_filter.empty?
33 34 hash.each do |key, value|
34   - if value.respond_to?(:to_hash)
35   - filter_hash(keys_to_filter, hash[key])
36   - elsif key_match?(key, keys_to_filter)
  35 + if key_match?(key, keys_to_filter)
37 36 hash[key] = "[FILTERED]"
  37 + elsif value.respond_to?(:to_hash)
  38 + filter_hash(keys_to_filter, hash[key])
38 39 end
39 40 end
40 41 end
@@ -45,13 +46,13 @@ def filter_hash(keys_to_filter, hash)
45 46 # https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/parameter_filter.rb
46 47 # https://github.com/exceptional/exceptional/issues/20
47 48 def key_match?(key, keys_to_filter)
48   - keys_to_filter.any? { |k|
49   - regexp = k.is_a?(Regexp)? k : Regexp.new(k, true)
  49 + keys_to_filter.any? { |k|
  50 + regexp = k.is_a?(Regexp)? k : Regexp.new(k, true)
50 51 key =~ regexp
51 52 }
52 53 end
53 54
54   - def filter_parameters(hash)
  55 + def filter_parameters(hash)
55 56 if @request.respond_to?(:env) && @request.env["action_dispatch.parameter_filter"]
56 57 filter_hash(@request.env["action_dispatch.parameter_filter"], hash)
57 58 elsif @controller.respond_to?(:filter_parameters)
9 spec/exceptional/exception_data_spec.rb
@@ -127,6 +127,13 @@ def to_json
127 127 data.to_hash['request']['parameters'].should == {'var1' => '[FILTERED]'}
128 128 end
129 129
  130 + it "filter nested params specified in env['action_dispatch.parameter_filter']" do
  131 + @request.stub!(:env).and_return({'SOME_VAR' => 'abc', 'HTTP_CONTENT_TYPE' => 'text/html', 'action_dispatch.parameter_filter' => [:var1]})
  132 + @request.stub!(:parameters).and_return({'var1' => {'var2' => 'abc','var3' => "abc"}})
  133 + data = Exceptional::ControllerExceptionData.new(@error, @controller, @request)
  134 + data.to_hash['request']['parameters'].should == {'var1' => '[FILTERED]'}
  135 + end
  136 +
130 137 it "formats the occurred_at as iso8601" do
131 138 @request.stub!(:env).and_return({'SOME_VAR' => 'abc', 'HTTP_CONTENT_TYPE' => 'text/html', 'action_dispatch.parameter_filter' => [:var1]})
132 139 @request.stub!(:parameters).and_return({'var1' => 'abc'})
@@ -195,4 +202,4 @@ def initialize
195 202 data = Exceptional::ControllerExceptionData.new(exception)
196 203 data.uniqueness_hash.should == nil
197 204 end
198   -end
  205 +end

0 comments on commit cc0a0cc

Please sign in to comment.
Something went wrong with that request. Please try again.