Module: JsonLogging::Sanitizer::StructuredHash
- Defined in:
- lib/json_logging/structured_hash_sanitizer.rb
Defined Under Namespace
Classes: JsonableResult
Class Method Summary collapse
- .count_leaves_in_value(value, leaf_counter, depth:, seen:) ⇒ Object
- .jsonable_array(array, depth:, seen:, parent_key:, leaf_counter: nil) ⇒ Object
- .jsonable_tree(hash, depth: 0, seen: nil, parent_key: nil, omit_keys: nil, count_leaves: false, leaf_counter: nil) ⇒ Object
- .jsonable_value(value, depth:, seen:, parent_key:, leaf_counter: nil) ⇒ Object
- .omit_root_keys(hash, omit_keys, depth:) ⇒ Object
- .process_jsonable_entry(key, value, depth:, seen:, parent_key:, omit_keys:, leaf_counter:) ⇒ Object
- .root_key_omitted?(depth, key_string, omit_keys) ⇒ Boolean
- .sanitize(hash, depth: 0) ⇒ Object
- .sanitize_without_filter(hash, depth: 0) ⇒ Object
- .stringify(hash) ⇒ Object
- .stringify_keys_copy(hash) ⇒ Object
- .stringify_value(value) ⇒ Object
- .structured_log_array?(array, seen = nil) ⇒ Boolean
- .structured_log_hash?(hash, seen = nil) ⇒ Boolean
- .structured_log_value?(value, seen = nil) ⇒ Boolean
Class Method Details
.count_leaves_in_value(value, leaf_counter, depth:, seen:) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 164 def count_leaves_in_value(value, leaf_counter, depth:, seen:) case value when Hash return unless seen.add?(value) value.each_value do |entry| count_leaves_in_value(entry, leaf_counter, depth: depth + 1, seen: seen) end seen.delete(value) when Array value.each { |entry| count_leaves_in_value(entry, leaf_counter, depth: depth, seen: seen) } else leaf_counter[0] += 1 end end |
.jsonable_array(array, depth:, seen:, parent_key:, leaf_counter: nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 113 def jsonable_array(array, depth:, seen:, parent_key:, leaf_counter: nil) owned = false result = nil array.each_with_index do |entry, index| child = jsonable_value(entry, depth: depth, seen: seen, parent_key: parent_key, leaf_counter: leaf_counter) next unless child.owned owned = true result ||= array.dup result[index] = child.tree end JsonableResult.new(tree: owned ? result : array, owned: owned) end |
.jsonable_tree(hash, depth: 0, seen: nil, parent_key: nil, omit_keys: nil, count_leaves: false, leaf_counter: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 54 def jsonable_tree(hash, depth: 0, seen: nil, parent_key: nil, omit_keys: nil, count_leaves: false, leaf_counter: nil) seen ||= Set.new.compare_by_identity leaf_counter = [0] if count_leaves && leaf_counter.nil? if depth > Sanitizer::MAX_DEPTH return JsonableResult.new(tree: {"error" => "max_depth_exceeded"}, owned: true, leaf_count: leaf_counter&.[](0)) end unless seen.add?(hash) return JsonableResult.new(tree: {"error" => "cyclic_reference"}, owned: true, leaf_count: leaf_counter&.[](0)) end entries = [] owned = false hash.each do |key, value| entry = process_jsonable_entry( key, value, depth: depth, seen: seen, parent_key: parent_key, omit_keys: omit_keys, leaf_counter: leaf_counter ) next unless entry key_string, encoded_value, pair_owned = entry owned ||= pair_owned entries << [key_string, encoded_value, pair_owned] end seen.delete(hash) leaf_count = leaf_counter&.[](0) unless owned tree = omit_root_keys(hash, omit_keys, depth: depth) return JsonableResult.new(tree: tree, owned: false, leaf_count: leaf_count) end result = entries.each_with_object({}) do |(key_string, encoded_value, _pair_owned), object| object[key_string] = encoded_value end JsonableResult.new(tree: result, owned: true, leaf_count: leaf_count) end |
.jsonable_value(value, depth:, seen:, parent_key:, leaf_counter: nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 97 def jsonable_value(value, depth:, seen:, parent_key:, leaf_counter: nil) case value when String leaf_counter[0] += 1 if leaf_counter sanitized = Sanitizer.sanitize_string(value) JsonableResult.new(tree: sanitized, owned: sanitized != value) when Hash jsonable_tree(value, depth: depth + 1, seen: seen, parent_key: parent_key, leaf_counter: leaf_counter) when Array jsonable_array(value, depth: depth, seen: seen, parent_key: parent_key, leaf_counter: leaf_counter) else leaf_counter[0] += 1 if leaf_counter JsonableResult.new(tree: value, owned: false) end end |
.omit_root_keys(hash, omit_keys, depth:) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 156 def omit_root_keys(hash, omit_keys, depth:) return hash unless depth.zero? && omit_keys&.any? hash.each_with_object({}) do |(key, value), object| object[key] = value unless omit_keys.include?(key.to_s) end end |
.process_jsonable_entry(key, value, depth:, seen:, parent_key:, omit_keys:, leaf_counter:) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 129 def process_jsonable_entry(key, value, depth:, seen:, parent_key:, omit_keys:, leaf_counter:) key_string = key.to_s if root_key_omitted?(depth, key_string, omit_keys) count_leaves_in_value(value, leaf_counter, depth: depth, seen: seen) if leaf_counter return end if Sanitizer::SENSITIVE_KEY_PATTERNS.match?(key_string) return [Sanitizer.sensitive_filtered_key_name(key_string), "[FILTERED]", true] end nested_parent = parent_key ? "#{parent_key}.#{key_string}" : key_string child = jsonable_value( value, depth: depth, seen: seen, parent_key: nested_parent, leaf_counter: leaf_counter ) encoded_value = child.owned ? child.tree : value [key_string, encoded_value, child.owned] end |
.root_key_omitted?(depth, key_string, omit_keys) ⇒ Boolean
152 153 154 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 152 def root_key_omitted?(depth, key_string, omit_keys) depth.zero? && omit_keys&.include?(key_string) end |
.sanitize(hash, depth: 0) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 34 def sanitize(hash, depth: 0) return {"error" => "max_depth_exceeded"} if depth > Sanitizer::MAX_DEPTH filter = Sanitizer.rails_parameter_filter if filter filter.filter(stringify(hash)) else sanitize_without_filter(hash, depth: depth) end end |
.sanitize_without_filter(hash, depth: 0) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 45 def sanitize_without_filter(hash, depth: 0) jsonable = jsonable_tree(hash, depth: depth) if jsonable.owned stringify(jsonable.tree) else jsonable.tree.dup end end |
.stringify(hash) ⇒ Object
180 181 182 183 184 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 180 def stringify(hash) hash.each_with_object({}) do |(key, value), result| result[key.to_s] = stringify_value(value) end end |
.stringify_keys_copy(hash) ⇒ Object
199 200 201 202 203 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 199 def stringify_keys_copy(hash) return hash if hash.keys.all? { |key| key.is_a?(String) } hash.transform_keys(&:to_s) end |
.stringify_value(value) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 186 def stringify_value(value) case value when Hash stringify(value) when Array value.map { |entry| stringify_value(entry) } when String Sanitizer.sanitize_string(value) else value end end |
.structured_log_array?(array, seen = nil) ⇒ Boolean
30 31 32 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 30 def structured_log_array?(array, seen = nil) array.all? { |value| structured_log_value?(value, seen) } end |
.structured_log_hash?(hash, seen = nil) ⇒ Boolean
8 9 10 11 12 13 14 15 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 8 def structured_log_hash?(hash, seen = nil) seen ||= Set.new.compare_by_identity return false unless seen.add?(hash) result = hash.all? { |_key, value| structured_log_value?(value, seen) } seen.delete(hash) result end |
.structured_log_value?(value, seen = nil) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/json_logging/structured_hash_sanitizer.rb', line 17 def structured_log_value?(value, seen = nil) case value when String, Numeric, TrueClass, FalseClass, NilClass Sanitizer.primitive_log_value?(value) when Hash structured_log_hash?(value, seen) when Array structured_log_array?(value, seen) else false end end |