Class: Bugsnag::Cleaner
- Inherits:
-
Object
- Object
- Bugsnag::Cleaner
- Defined in:
- lib/bugsnag/cleaner.rb
Constant Summary collapse
- ENCODING_OPTIONS =
{:invalid => :replace, :undef => :replace}.freeze
- FILTERED =
'[FILTERED]'.freeze
- RECURSION =
'[RECURSION]'.freeze
- OBJECT =
'[OBJECT]'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #clean_object(obj) ⇒ Object
- #clean_string(str) ⇒ Object
- #clean_url(url) ⇒ Object
-
#initialize(filters) ⇒ Cleaner
constructor
A new instance of Cleaner.
- #traverse_object(obj, seen, scope) ⇒ Object
Constructor Details
#initialize(filters) ⇒ Cleaner
Returns a new instance of Cleaner.
10 11 12 13 |
# File 'lib/bugsnag/cleaner.rb', line 10 def initialize(filters) @filters = Array(filters) @deep_filters = @filters.any? {|f| f.kind_of?(Regexp) && f.to_s.include?("\\.".freeze) } end |
Class Method Details
.clean_object_encoding(obj) ⇒ Object
73 74 75 |
# File 'lib/bugsnag/cleaner.rb', line 73 def self.clean_object_encoding(obj) new(nil).clean_object(obj) end |
Instance Method Details
#clean_object(obj) ⇒ Object
15 16 17 |
# File 'lib/bugsnag/cleaner.rb', line 15 def clean_object(obj) traverse_object(obj, {}, nil) end |
#clean_string(str) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bugsnag/cleaner.rb', line 59 def clean_string(str) if defined?(str.encoding) && defined?(Encoding::UTF_8) if str.encoding == Encoding::UTF_8 str.valid_encoding? ? str : str.encode('utf-16', ENCODING_OPTIONS).encode('utf-8') else str.encode('utf-8', ENCODING_OPTIONS) end elsif defined?(Iconv) Iconv.conv('UTF-8//IGNORE', 'UTF-8', str) || str else str end end |
#clean_url(url) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bugsnag/cleaner.rb', line 77 def clean_url(url) return url if @filters.empty? uri = URI(url) return url unless uri.query query_params = uri.query.split('&').map { |pair| pair.split('=') } query_params.map! do |key, val| if filters_match?(key) "#{key}=#{FILTERED}" else "#{key}=#{val}" end end uri.query = query_params.join('&') uri.to_s end |
#traverse_object(obj, seen, scope) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bugsnag/cleaner.rb', line 19 def traverse_object(obj, seen, scope) return nil unless obj # Protect against recursion of recursable items protection = if obj.is_a?(Hash) || obj.is_a?(Array) || obj.is_a?(Set) return seen[obj] if seen[obj] seen[obj] = RECURSION end value = case obj when Hash clean_hash = {} obj.each do |k,v| if filters_match_deeply?(k, scope) clean_hash[k] = FILTERED else clean_hash[k] = traverse_object(v, seen, [scope, k].compact.join('.')) end end clean_hash when Array, Set obj.map { |el| traverse_object(el, seen, scope) }.compact when Numeric, TrueClass, FalseClass obj when String clean_string(obj) else str = obj.to_s # avoid leaking potentially sensitive data from objects' #inspect output if str =~ /#<.*>/ OBJECT else clean_string(str) end end seen[obj] = value if protection value end |