Module: LogStruct::BoundedInput
- Extended by:
- T::Sig
- Defined in:
- lib/log_struct/bounded_input.rb
Overview
Copies of logged Hashes and Arrays that stop before an input too large to copy in full
Defined Under Namespace
Classes: State
Constant Summary collapse
- MAX_DEPTH =
A Rails filter block with three arguments gets every parameter, so a copy for it keeps what the formatter leaves out of the line. These limits only stop values no request sends: nesting deeper than the formatter writes, and more values than a form or JSON body has. A copy that reaches one is truncated, and the filters that read it fail closed.
20- MAX_NODES =
10_000- COUNT_KEYS =
The keys that count what a Hash's copy left out. The formatter writes them as it writes its own counts: Rails' default filter_parameters include :_key, which would filter them.
T.let(Set[:_more_keys, :_oversized_keys].freeze, T::Set[Symbol])
Class Method Summary collapse
- .copy(value, state:, depth: 0, ancestors: {}.compare_by_identity) ⇒ T.untyped
-
.key_count(hash) ⇒ Integer
How many keys a Hash or ActionController::Parameters has, without converting its values.
-
.symbolize_keys(hash) ⇒ Hash{Symbol => T.untyped}
The first keys of a Hash or ActionController::Parameters as Symbols, for a log struct's Hash field, without converting the keys the formatter would leave out.
Class Method Details
.copy(value, state:, depth: 0, ancestors: {}.compare_by_identity) ⇒ T.untyped
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 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/log_struct/bounded_input.rb', line 30 def self.copy(value, state:, depth: 0, ancestors: {}.compare_by_identity) if depth > MAX_DEPTH || ancestors.key?(value) || state.remaining_nodes <= 0 state.truncated = true return nil end state.remaining_nodes -= 1 case value when Hash, ::ActionController::Parameters ancestors[value] = true begin result = {} value.each_pair do |key, child| break if state.truncated result[key] = copy(child, state: state, depth: depth + 1, ancestors: ancestors) end result ensure ancestors.delete(value) end when Array ancestors[value] = true begin result = [] value.each do |child| break if state.truncated result << copy(child, state: state, depth: depth + 1, ancestors: ancestors) end result ensure ancestors.delete(value) end else value end end |
.key_count(hash) ⇒ Integer
How many keys a Hash or ActionController::Parameters has, without converting its values
71 72 73 |
# File 'lib/log_struct/bounded_input.rb', line 71 def self.key_count(hash) hash.is_a?(Hash) ? hash.size : hash.keys.size end |
.symbolize_keys(hash) ⇒ Hash{Symbol => T.untyped}
The first keys of a Hash or ActionController::Parameters as Symbols, for a log struct's Hash field, without converting the keys the formatter would leave out. The formatter writes max_hash_keys entries of a Hash, and the counts of the keys left out here are entries too: the last key's slot goes to _more_keys, and an omitted oversized key's slot to _oversized_keys, so the formatter writes both counts as they are.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/log_struct/bounded_input.rb', line 80 def self.symbolize_keys(hash) limits = LogStruct.config.limits total = key_count(hash) examined = (total > limits.max_hash_keys) ? [limits.max_hash_keys - 1, 0].max : total result = T.let({}, T::Hash[Symbol, T.untyped]) oversized_keys = 0 hash.each_pair.with_index do |(key, value), index| break if index >= examined if key.to_s.bytesize > limits.max_string_bytes oversized_keys += 1 next end result[key.to_s.to_sym] = value end result[:_more_keys] = total - examined if examined < total result[:_oversized_keys] = oversized_keys if oversized_keys.positive? result end |