logstruct 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/lib/log_struct/bounded_input.rb +101 -0
  4. data/lib/log_struct/concerns/configuration.rb +9 -9
  5. data/lib/log_struct/concerns/error_handling.rb +10 -0
  6. data/lib/log_struct/config_struct/filters.rb +24 -13
  7. data/lib/log_struct/config_struct/limits.rb +1 -1
  8. data/lib/log_struct/enums/level.rb +1 -1
  9. data/lib/log_struct/enums/log_field.rb +0 -3
  10. data/lib/log_struct/formatter.rb +38 -23
  11. data/lib/log_struct/hash_utils.rb +11 -4
  12. data/lib/log_struct/integrations/action_mailer/event_logging.rb +1 -0
  13. data/lib/log_struct/integrations/action_mailer/metadata_collection.rb +1 -0
  14. data/lib/log_struct/integrations/active_record.rb +1 -1
  15. data/lib/log_struct/integrations/ahoy.rb +4 -3
  16. data/lib/log_struct/integrations/carrierwave.rb +4 -6
  17. data/lib/log_struct/integrations/dotenv.rb +2 -1
  18. data/lib/log_struct/integrations/good_job.rb +2 -1
  19. data/lib/log_struct/integrations/host_authorization.rb +3 -6
  20. data/lib/log_struct/integrations/lograge.rb +23 -32
  21. data/lib/log_struct/integrations/puma.rb +1 -1
  22. data/lib/log_struct/integrations/rack_error_handler/error_lines.rb +1 -4
  23. data/lib/log_struct/integrations/shrine.rb +1 -1
  24. data/lib/log_struct/log/action_mailer/delivered.rb +19 -7
  25. data/lib/log_struct/log/action_mailer/delivery.rb +19 -7
  26. data/lib/log_struct/log/action_mailer/error.rb +19 -7
  27. data/lib/log_struct/log/action_mailer.rb +1 -0
  28. data/lib/log_struct/log/active_job/discard.rb +7 -5
  29. data/lib/log_struct/log/active_job/enqueue.rb +7 -5
  30. data/lib/log_struct/log/active_job/error.rb +7 -5
  31. data/lib/log_struct/log/active_job/finish.rb +7 -5
  32. data/lib/log_struct/log/active_job/retry.rb +7 -5
  33. data/lib/log_struct/log/active_job/retry_stopped.rb +7 -5
  34. data/lib/log_struct/log/active_job/schedule.rb +7 -5
  35. data/lib/log_struct/log/active_job/start.rb +7 -5
  36. data/lib/log_struct/log/active_job.rb +1 -0
  37. data/lib/log_struct/log/active_model_serializers.rb +7 -5
  38. data/lib/log_struct/log/active_storage/delete.rb +7 -5
  39. data/lib/log_struct/log/active_storage/download.rb +7 -5
  40. data/lib/log_struct/log/active_storage/exist.rb +7 -5
  41. data/lib/log_struct/log/active_storage/metadata.rb +7 -5
  42. data/lib/log_struct/log/active_storage/upload.rb +7 -5
  43. data/lib/log_struct/log/active_storage/url.rb +7 -5
  44. data/lib/log_struct/log/active_storage.rb +1 -0
  45. data/lib/log_struct/log/ahoy.rb +7 -5
  46. data/lib/log_struct/log/carrierwave/delete.rb +7 -9
  47. data/lib/log_struct/log/carrierwave/download.rb +7 -11
  48. data/lib/log_struct/log/carrierwave/upload.rb +7 -11
  49. data/lib/log_struct/log/carrierwave.rb +3 -8
  50. data/lib/log_struct/log/dotenv/load.rb +7 -5
  51. data/lib/log_struct/log/dotenv/restore.rb +7 -5
  52. data/lib/log_struct/log/dotenv/save.rb +7 -5
  53. data/lib/log_struct/log/dotenv/update.rb +7 -5
  54. data/lib/log_struct/log/error.rb +24 -14
  55. data/lib/log_struct/log/good_job/error.rb +7 -5
  56. data/lib/log_struct/log/good_job/log.rb +7 -5
  57. data/lib/log_struct/log/good_job.rb +1 -0
  58. data/lib/log_struct/log/plain.rb +19 -7
  59. data/lib/log_struct/log/puma/error.rb +19 -7
  60. data/lib/log_struct/log/puma/shutdown.rb +19 -7
  61. data/lib/log_struct/log/puma/start.rb +19 -7
  62. data/lib/log_struct/log/request.rb +24 -14
  63. data/lib/log_struct/log/security/blocked_host.rb +24 -14
  64. data/lib/log_struct/log/security/csrf_violation.rb +24 -14
  65. data/lib/log_struct/log/security/ip_spoof.rb +24 -14
  66. data/lib/log_struct/log/security.rb +1 -0
  67. data/lib/log_struct/log/shrine/delete.rb +7 -5
  68. data/lib/log_struct/log/shrine/download.rb +7 -5
  69. data/lib/log_struct/log/shrine/exist.rb +7 -5
  70. data/lib/log_struct/log/shrine/metadata.rb +7 -5
  71. data/lib/log_struct/log/shrine/upload.rb +7 -5
  72. data/lib/log_struct/log/sidekiq/error.rb +19 -7
  73. data/lib/log_struct/log/sidekiq/log.rb +19 -7
  74. data/lib/log_struct/log/sql.rb +19 -7
  75. data/lib/log_struct/monkey_patches/active_support/tagged_logging/formatter.rb +1 -0
  76. data/lib/log_struct/multi_error_reporter.rb +1 -1
  77. data/lib/log_struct/param_filters.rb +7 -26
  78. data/lib/log_struct/params_errors.rb +7 -10
  79. data/lib/log_struct/rails_boot_banner_silencer.rb +1 -0
  80. data/lib/log_struct/semantic_logger/concerns/log_methods.rb +1 -0
  81. data/lib/log_struct/semantic_logger/formatter.rb +1 -1
  82. data/lib/log_struct/semantic_logger/logger.rb +1 -1
  83. data/lib/log_struct/semantic_logger/setup.rb +1 -4
  84. data/lib/log_struct/shared/merge_additional_data_fields.rb +21 -5
  85. data/lib/log_struct/string_scrubber.rb +1 -1
  86. data/lib/log_struct/version.rb +1 -1
  87. data/logstruct.gemspec +1 -1
  88. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9e72813c2641a2cc336184ab2e6f15b0d38e1f005da1ade37e136487a9c4369
4
- data.tar.gz: b91cbaa7d64ea075db6937d415db0d0e7f86e58902083d54e0b985570a2573c5
3
+ metadata.gz: b257a68c1c28d1475a75a22d5114ad27325c6da9c327edb4eba4a035a2ec3de0
4
+ data.tar.gz: f32b4ea82a198a0a7bae0b6454411ddd28ddf92ae09d96948d02074399cc242c
5
5
  SHA512:
6
- metadata.gz: cb4d9ebb39a98d73f022c853daac9069599fe83528d0ca6234f2f63517803f48df932762827b37e5b4aae3b89e1da16c6d3b85005f54da7cab95dcfe8ee263d0
7
- data.tar.gz: 7843625a239673d36ac68c092568927a9f5c112c830dd1b00caaf3c0e9d495fd6c9fb7514c18b5119000dc940f9a6706bbc2fb987506d55fefdd6264d9f0c568
6
+ metadata.gz: 53ded423b2a206e38da2799efa3a5a8687a68acc86da9270548bd1e08e722cf2862604733f413e71e88d9d8d54ae637e1b7316b893fd1ee4a81e0e0f42fd8efc
7
+ data.tar.gz: 5fde1dfdb59100833384f59126d64b0cd5466abcc4304278f1312c3bca2fad6f0461b16f47720c142600c6f8e059faf27f032b235d9c34e400fc08738fb29382
data/CHANGELOG.md CHANGED
@@ -7,6 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2026-11-06
11
+
12
+ ### Changed
13
+
14
+ - **Breaking**: Requires `sorbet-runtime` 0.6.12698 or later, the first version with `T::Module`, which `LogStruct::SemanticLogger::Logger`'s signature uses.
15
+ - **Breaking**: Log structs mark the props that implement their interfaces with `override: true`, which current versions of Sorbet require. A custom log struct needs the same on its `level`, `timestamp` and `additional_data` props, with the interfaces included above them.
16
+ - **Development**: Update the locked gems and the docs and tooling packages, including Sorbet 0.6, Tapioca 0.20 and RuboCop 1.91. CI tests Rails 7.2.4, 8.0.5.2 and 8.1.4.1.
17
+ - **Development**: `task ci` runs `jscpd`. GitHub CI runs the dependency audits, and a release waits for them.
18
+
19
+ ### Fixed
20
+
21
+ - **Development**: Update the locked `crass` dependency to 1.0.8, which fixes GHSA-pq4g-8rxf-4g29, and move `glob` and `minimatch` in the development tools to patched versions.
22
+ - **Generation**: An interrupted run leaves the generated files as they were, because they're written to a staging directory and moved into place.
23
+
24
+ ## [0.3.0] - 2026-10-23
25
+
26
+ ### Changed
27
+
28
+ - **Breaking**: CarrierWave lines log `file_id` as an HMAC of the file's identifier (keyed like email hashes, see `hash_salt`), and no longer log `filename`, `store_path` or `ext`. CarrierWave names stored files after the uploaded file by default, and an uploaded file's name often names a person. `LogField::Filename`, `LogField::StorePath` and `LogField::Extension` are removed.
29
+ - **Breaking**: A filtered Hash's summary has its `_keys_count` but not its first 10 keys (`_keys`): a Hash's keys can be secrets themselves, like tokens or email addresses used as keys.
30
+ - **Breaking**: `additional_data` can't set any field of its line's own type, whether or not the line has it (an error line's `msg`, `backtrace` or `error_message`), or `req_id`. Like `src`, `evt`, `lvl` and `ts`, such a key is left out and handled as a LogStruct error (raised in `local_environments`). It used to replace the field. Other types' field names, like `name` on a plain line, can still be used.
31
+ - **Filtering**: Without a `hash_salt`, values hashed before the Rails app has its `secret_key_base` use a random key for each process, instead of a fixed key that was public in LogStruct's source. Set `hash_salt` to keep these hashes the same across processes.
32
+ - **Development**: `task ci` checks the locked Ruby and docs dependencies for known advisories, with `bundler-audit` and `pnpm audit`.
33
+
34
+ ### Fixed
35
+
36
+ - **Formatting**: Hash keys longer than `max_string_bytes` are left out with their values, and counted in `_oversized_keys`. A request's parameter names could make a line any size.
37
+ - **Formatting**: `ActionController::Parameters` are written like a Hash, within the size limits, instead of being converted whole first. Lograge's `params` and Ahoy's `properties` are converted at their first level only, and only as many keys as the line keeps; `_more_keys` and `_oversized_keys` count the rest.
38
+ - **Filtering**: A Rails filter block with three arguments gets a copy of the parameters that stops at 10,000 values, 20 levels of nesting or a value that contains itself. Past those, every key the block would check is filtered. The parameters used to be copied whole.
39
+ - **Errors**: An error raised while formatting, serializing or filtering a value is reported with its class and backtrace but not its message, which can quote the value. Errors in Rails filter blocks and LogStruct's filter matchers aren't reported with the key, which can be the secret. An object whose `as_json` raises is logged as `[SERIALIZATION_ERROR]` instead of its `to_s`, and its `inspect` isn't reported.
40
+ - **Formatting**: A Hash key that isn't a String or Symbol is written as its string, scrubbed, so an email address in its `to_s` is scrubbed.
41
+ - **Filtering**: A value longer than `max_string_bytes` is hashed by its first `max_string_bytes` bytes and its length.
42
+ - **Development**: Update Next.js to 16.4.1 for the docs site, which fixes three advisories in features the static site doesn't use, and the test-only Dalli to 3.2.13.
43
+
10
44
  ## [0.2.2] - 2026-10-21
11
45
 
12
46
  ### Changed
@@ -0,0 +1,101 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "action_controller"
5
+
6
+ module LogStruct
7
+ # Copies of logged Hashes and Arrays that stop before an input too large to copy in full
8
+ module BoundedInput
9
+ extend T::Sig
10
+
11
+ # A Rails filter block with three arguments gets every parameter, so a copy for it keeps what the formatter
12
+ # leaves out of the line. These limits only stop values no request sends: nesting deeper than the formatter
13
+ # writes, and more values than a form or JSON body has. A copy that reaches one is truncated, and the
14
+ # filters that read it fail closed.
15
+ MAX_DEPTH = 20
16
+ MAX_NODES = 10_000
17
+
18
+ # The keys that count what a Hash's copy left out. The formatter writes them as it writes its own counts:
19
+ # Rails' default filter_parameters include :_key, which would filter them.
20
+ COUNT_KEYS = T.let(Set[:_more_keys, :_oversized_keys].freeze, T::Set[Symbol])
21
+
22
+ class State < T::Struct
23
+ prop :remaining_nodes, Integer, default: MAX_NODES
24
+ prop :truncated, T::Boolean, default: false
25
+ end
26
+
27
+ sig do
28
+ params(value: T.untyped, state: State, depth: Integer, ancestors: T::Hash[T.untyped, T::Boolean]).returns(T.untyped)
29
+ end
30
+ def self.copy(value, state:, depth: 0, ancestors: {}.compare_by_identity)
31
+ if depth > MAX_DEPTH || ancestors.key?(value) || state.remaining_nodes <= 0
32
+ state.truncated = true
33
+ return nil
34
+ end
35
+ state.remaining_nodes -= 1
36
+
37
+ case value
38
+ when Hash, ::ActionController::Parameters
39
+ ancestors[value] = true
40
+ begin
41
+ result = {}
42
+ value.each_pair do |key, child|
43
+ break if state.truncated
44
+
45
+ result[key] = copy(child, state: state, depth: depth + 1, ancestors: ancestors)
46
+ end
47
+ result
48
+ ensure
49
+ ancestors.delete(value)
50
+ end
51
+ when Array
52
+ ancestors[value] = true
53
+ begin
54
+ result = []
55
+ value.each do |child|
56
+ break if state.truncated
57
+
58
+ result << copy(child, state: state, depth: depth + 1, ancestors: ancestors)
59
+ end
60
+ result
61
+ ensure
62
+ ancestors.delete(value)
63
+ end
64
+ else
65
+ value
66
+ end
67
+ end
68
+
69
+ # How many keys a Hash or ActionController::Parameters has, without converting its values
70
+ sig { params(hash: T.untyped).returns(Integer) }
71
+ def self.key_count(hash)
72
+ hash.is_a?(Hash) ? hash.size : hash.keys.size
73
+ end
74
+
75
+ # The first keys of a Hash or ActionController::Parameters as Symbols, for a log struct's Hash field, without
76
+ # converting the keys the formatter would leave out. The formatter writes max_hash_keys entries of a Hash,
77
+ # and the counts of the keys left out here are entries too: the last key's slot goes to _more_keys, and an
78
+ # omitted oversized key's slot to _oversized_keys, so the formatter writes both counts as they are.
79
+ sig { params(hash: T.untyped).returns(T::Hash[Symbol, T.untyped]) }
80
+ def self.symbolize_keys(hash)
81
+ limits = LogStruct.config.limits
82
+ total = key_count(hash)
83
+ examined = (total > limits.max_hash_keys) ? [limits.max_hash_keys - 1, 0].max : total
84
+ result = T.let({}, T::Hash[Symbol, T.untyped])
85
+ oversized_keys = 0
86
+ hash.each_pair.with_index do |(key, value), index|
87
+ break if index >= examined
88
+
89
+ if key.to_s.bytesize > limits.max_string_bytes
90
+ oversized_keys += 1
91
+ next
92
+ end
93
+
94
+ result[key.to_s.to_sym] = value
95
+ end
96
+ result[:_more_keys] = total - examined if examined < total
97
+ result[:_oversized_keys] = oversized_keys if oversized_keys.positive?
98
+ result
99
+ end
100
+ end
101
+ end
@@ -12,7 +12,7 @@ module LogStruct
12
12
  module ClassMethods
13
13
  extend T::Sig
14
14
 
15
- CONSOLE_COMMAND_ARGS = T.let(["console", "c"].freeze, T::Array[String])
15
+ CONSOLE_COMMAND_ARGS = ["console", "c"].freeze
16
16
  EMPTY_ARGV = T.let([].freeze, T::Array[String])
17
17
  RAILS_PATTERNS_LABEL = "Rails filter_parameters"
18
18
  RAILS_REGEXPS_LABEL = "Rails filter_parameters (Regexps)"
@@ -237,7 +237,7 @@ module LogStruct
237
237
  # there or from the field's value down, as it would in Rails.
238
238
  sig { params(patterns: T::Array[T.untyped]).returns(ConfigStruct::PathFilterMatcher) }
239
239
  def build_rails_path_matcher(patterns)
240
- regexps = patterns.select { |entry| entry.is_a?(Regexp) }
240
+ regexps = patterns.grep(Regexp)
241
241
  strings = patterns.reject { |entry| entry.is_a?(Regexp) }.map { |entry| Regexp.escape(entry.to_s) }
242
242
  regexps << Regexp.new(strings.join("|"), true) if strings.any?
243
243
  callable = Kernel.lambda do |path, _value, _root|
@@ -259,7 +259,7 @@ module LogStruct
259
259
  result = filter.call(key, value, root)
260
260
  !!result || (value.is_a?(String) && value != original_value)
261
261
  rescue => e
262
- handle_filter_error(e, filter, original_key || "")
262
+ handle_filter_error(e, filter)
263
263
  true
264
264
  end
265
265
  ConfigStruct::PathFilterMatcher.new(callable: callable, label: filter.inspect)
@@ -290,11 +290,11 @@ module LogStruct
290
290
  begin
291
291
  !!filter.call(key)
292
292
  rescue => e
293
- handle_filter_error(e, filter, key)
293
+ handle_filter_error(e, filter)
294
294
  true
295
295
  end
296
296
  rescue => e
297
- handle_filter_error(e, filter, key)
297
+ handle_filter_error(e, filter)
298
298
  true
299
299
  end
300
300
  ConfigStruct::FilterMatcher.new(callable: callable, label: filter.inspect)
@@ -305,11 +305,11 @@ module LogStruct
305
305
  filter.respond_to?(:arity) ? filter.arity : 2
306
306
  end
307
307
 
308
- sig { params(error: StandardError, filter: T.untyped, key: String).void }
309
- def handle_filter_error(error, filter, key)
308
+ # The key isn't reported: a key can be the secret (an email address or a token used as a key)
309
+ sig { params(error: StandardError, filter: T.untyped).void }
310
+ def handle_filter_error(error, filter)
310
311
  context = {
311
312
  filter: filter.class.name,
312
- key: key,
313
313
  filter_label: begin
314
314
  filter.inspect
315
315
  rescue
@@ -317,7 +317,7 @@ module LogStruct
317
317
  end
318
318
  }
319
319
 
320
- LogStruct.handle_exception(error, source: Source::Internal, context: context)
320
+ LogStruct.handle_value_error(error, "filter_parameters entry failed", context: context)
321
321
  end
322
322
  end
323
323
  end
@@ -89,6 +89,16 @@ module LogStruct
89
89
  Thread.current[:logstruct_formatting_internal_error] = previous_internal_error
90
90
  end
91
91
 
92
+ # Handle an error raised while LogStruct read a value it was logging, without the error's message: the
93
+ # message can quote that value, as a NoMethodError quotes its receiver's inspect before Ruby 3.4.
94
+ # failure says what failed, and the reported error keeps the original's class and backtrace.
95
+ sig { params(error: Exception, failure: String, context: T.nilable(T::Hash[Symbol, T.untyped])).void }
96
+ def handle_value_error(error, failure, context: nil)
97
+ reported = RuntimeError.new("#{error.class}: #{failure}")
98
+ reported.set_backtrace(error.backtrace)
99
+ handle_exception(reported, source: Source::Internal, context: context)
100
+ end
101
+
92
102
  # Handle an error according to the configured error handling mode (log, report, raise, etc).
93
103
  # Pass `log: false` when the caller has already logged the error in its own structured line.
94
104
  sig { params(error: StandardError, source: Source, context: T.nilable(T::Hash[Symbol, T.untyped]), log: T::Boolean).void }
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "active_support/hash_with_indifferent_access"
5
+ require_relative "../bounded_input"
5
6
 
6
7
  module LogStruct
7
8
  # A key's words, lowercase and joined by underscores, so that keys match however they're written:
@@ -38,8 +39,8 @@ module LogStruct
38
39
  # A filter that needs more than the key: Rails' entries with a dot (`card.number`) match a key by its
39
40
  # path, and Rails gives filter blocks with three arguments the parameters around the key. The path
40
41
  # starts at the line's top-level field (`["params", "card", "number"]`); `root` is that field's value,
41
- # or the whole line for a top-level key, with indifferent access as Rails gives it. Building it copies
42
- # every value inside it, so a matcher that doesn't read it sets uses_root to false and gets nil.
42
+ # or the whole line for a top-level key, with indifferent access as Rails gives it. A matcher
43
+ # that doesn't read the root sets uses_root to false and gets nil.
43
44
  class PathFilterMatcher < T::Struct
44
45
  extend T::Sig
45
46
 
@@ -50,7 +51,10 @@ module LogStruct
50
51
  sig { params(path: T::Array[String], value: T.untyped, root: T.untyped).returns(T::Boolean) }
51
52
  def matches?(path, value, root)
52
53
  root = nil unless uses_root
53
- callable.call(path, value, root.is_a?(LazyRoot) ? root.value : root)
54
+ root = root.value if root.is_a?(LazyRoot)
55
+ return true if root.equal?(LazyRoot::UNAVAILABLE)
56
+
57
+ callable.call(path, value, root)
54
58
  end
55
59
  end
56
60
 
@@ -58,6 +62,8 @@ module LogStruct
58
62
  class LazyRoot
59
63
  extend T::Sig
60
64
 
65
+ UNAVAILABLE = Object.new.freeze
66
+
61
67
  sig { params(value: T.untyped).void }
62
68
  def initialize(value)
63
69
  @source = value
@@ -70,12 +76,20 @@ module LogStruct
70
76
  def value
71
77
  return @value if @built
72
78
 
73
- @built = true
74
- @value = begin
75
- @source.is_a?(Hash) ? ActiveSupport::HashWithIndifferentAccess.new(@source) : @source
76
- rescue SystemStackError, StandardError
77
- @source
79
+ state = BoundedInput::State.new
80
+ bounded = BoundedInput.copy(@source, state: state)
81
+ @value = if state.truncated
82
+ UNAVAILABLE
83
+ elsif bounded.is_a?(Hash)
84
+ ActiveSupport::HashWithIndifferentAccess.new(bounded)
85
+ else
86
+ bounded
78
87
  end
88
+ @built = true
89
+ @value
90
+ rescue SystemStackError, StandardError
91
+ @built = true
92
+ @value = UNAVAILABLE
79
93
  end
80
94
  end
81
95
 
@@ -86,7 +100,7 @@ module LogStruct
86
100
  # A filtered value is replaced by a summary of it, with no length (which would say how long a
87
101
  # secret is):
88
102
  #
89
- # { _filtered: { _class: "Hash", _keys_count: 3, _keys: [:key1, :key2, :key3] } } # first 10 keys
103
+ # { _filtered: { _class: "Hash", _keys_count: 3 } }
90
104
  # { _filtered: { _class: "Array", _count: 2 } }
91
105
  # { _filtered: { _class: "String" } } # plus its _hash for filter_keys_with_hashes
92
106
  #
@@ -173,10 +187,7 @@ module LogStruct
173
187
  # - api_key, secret_key, private_key, access_key, encryption_key, signing_key, master_key: X-Api-Key
174
188
  # - signature, sig, salt and certificate as the last word: X-Amz-Signature (but not signature_field_id)
175
189
  # Matching whole words avoids false positives like "keyboard", "author" or "cron_key".
176
- DEFAULT_SENSITIVE_KEY_PATTERN = T.let(
177
- /(?:\A|_)(?:passwords?|passwd|passphrases?|passcodes?|pwd|tokens?|secrets?|auth|authorization|authentication|creds?|credentials?|cookies?|sessions?|jwts?|bearer|otps?|apikeys?|cvv|cvc|ssn|social_security(?:_numbers?)?|credit_cards?|card_numbers?|crypt|crypted)(?:_|\z)|(?:\A|_)(?:api|secret|private|access|encryption|signing|master)_keys?(?:_|\z)|(?:\A|_)(?:signature|sig|salt|certificate)s?\z/,
178
- Regexp
179
- )
190
+ DEFAULT_SENSITIVE_KEY_PATTERN = /(?:\A|_)(?:passwords?|passwd|passphrases?|passcodes?|pwd|tokens?|secrets?|auth|authorization|authentication|creds?|credentials?|cookies?|sessions?|jwts?|bearer|otps?|apikeys?|cvv|cvc|ssn|social_security(?:_numbers?)?|credit_cards?|card_numbers?|crypt|crypted)(?:_|\z)|(?:\A|_)(?:api|secret|private|access|encryption|signing|master)_keys?(?:_|\z)|(?:\A|_)(?:signature|sig|salt|certificate)s?\z/
180
191
 
181
192
  # Matchers for keys that aren't listed by name in filter_keys. Each matcher receives the key (String)
182
193
  # and optional value, returning true when the pair should be filtered. Includes a regex matcher for
@@ -21,7 +21,7 @@ module LogStruct
21
21
  # Default: 20
22
22
  prop :max_backtrace_lines, Integer, default: 20
23
23
 
24
- # Strings longer than this many bytes are cut, and say how long they were
24
+ # Strings longer than this many bytes are cut; oversized Hash keys and their values are omitted
25
25
  # Default: 32768 (32 KB)
26
26
  prop :max_string_bytes, Integer, default: 32_768
27
27
  end
@@ -32,7 +32,7 @@ module LogStruct
32
32
  end
33
33
 
34
34
  # Convert a string or integer severity to a Level
35
- sig { params(severity: T.any(String, Symbol, Integer, NilClass)).returns(Level) }
35
+ sig { params(severity: T.nilable(T.any(String, Symbol, Integer))).returns(Level) }
36
36
  def self.from_severity(severity)
37
37
  return Unknown if severity.nil?
38
38
  return from_severity_int(severity) if severity.is_a?(Integer)
@@ -120,7 +120,6 @@ module LogStruct
120
120
  Storage = new(:storage)
121
121
  Operation = new(:op)
122
122
  FileId = new(:file_id)
123
- Filename = new(:filename)
124
123
  MimeType = new(:mime_type)
125
124
  Size = new(:size)
126
125
  Metadata = new(:metadata)
@@ -134,8 +133,6 @@ module LogStruct
134
133
  Model = new(:model)
135
134
  MountPoint = new(:mount_point)
136
135
  Version = new(:version)
137
- StorePath = new(:store_path)
138
- Extension = new(:ext)
139
136
 
140
137
  # SQL-specific fields
141
138
  Sql = new(:sql)
@@ -10,6 +10,7 @@ require_relative "enums/source"
10
10
  require_relative "enums/event"
11
11
  require_relative "enums/log_field"
12
12
  require_relative "string_scrubber"
13
+ require_relative "bounded_input"
13
14
  require_relative "log"
14
15
  require_relative "param_filters"
15
16
  require_relative "multi_error_reporter"
@@ -80,13 +81,13 @@ module LogStruct
80
81
  return summarize_unprocessed_value(arg) if recursion_depth > MAX_RECURSION_DEPTH || ancestors.key?(arg)
81
82
 
82
83
  case arg
83
- when Hash, Array
84
+ when Hash, Array, ::ActionController::Parameters
84
85
  ancestors[arg] = true
85
86
  begin
86
- if arg.is_a?(Hash)
87
- process_hash(arg, recursion_depth: recursion_depth, ancestors: ancestors, path: path, root: root)
88
- else
87
+ if arg.is_a?(Array)
89
88
  process_array(arg, recursion_depth: recursion_depth, ancestors: ancestors, path: path, root: root)
89
+ else
90
+ process_hash(arg, recursion_depth: recursion_depth, ancestors: ancestors, path: path, root: root)
90
91
  end
91
92
  ensure
92
93
  ancestors.delete(arg)
@@ -107,7 +108,7 @@ module LogStruct
107
108
  scrub_string(String(object))
108
109
  end
109
110
  rescue => e
110
- LogStruct.handle_exception(e, source: Source::Internal)
111
+ LogStruct.handle_value_error(e, "GlobalID formatting failed")
111
112
  "[GLOBALID_ERROR]"
112
113
  end
113
114
  end
@@ -125,26 +126,24 @@ module LogStruct
125
126
  when Numeric, TrueClass, FalseClass, NilClass
126
127
  arg
127
128
  else
128
- # Structs, ActionController::Parameters and other objects: filter and scrub what
129
+ # Structs and other objects: filter and scrub what
129
130
  # to_json would write
130
131
  process_values(arg.as_json, recursion_depth: recursion_depth + 1, ancestors: ancestors, path: path, root: root)
131
132
  end
132
133
  rescue SystemStackError, StandardError => e
133
134
  # A SystemStackError comes from an object whose as_json refers back to itself
134
- e = RuntimeError.new("#{e.class}: #{e.message}") unless e.is_a?(StandardError)
135
- # Report error through LogStruct's framework
136
135
  context = {
137
136
  processor_method: "process_values",
138
137
  value_type: arg.class.name,
139
138
  recursion_depth: recursion_depth
140
139
  }
141
- LogStruct.handle_exception(e, source: Source::Internal, context: context)
140
+ LogStruct.handle_value_error(e, "value formatting failed", context: context)
142
141
  summarize_unprocessed_value(arg)
143
142
  end
144
143
 
145
144
  sig do
146
145
  params(
147
- hash: T::Hash[T.untyped, T.untyped],
146
+ hash: T.untyped,
148
147
  recursion_depth: Integer,
149
148
  ancestors: T::Hash[T.untyped, T::Boolean],
150
149
  path: T.nilable(T::Array[String]),
@@ -158,13 +157,30 @@ module LogStruct
158
157
  line_root = (path && recursion_depth == 0) ? ConfigStruct::LazyRoot.new(hash) : nil
159
158
 
160
159
  max_keys = LogStruct.config.limits.max_hash_keys
161
- hash.each_with_index do |(key, value), index|
160
+ max_key_bytes = LogStruct.config.limits.max_string_bytes
161
+ oversized_keys = 0
162
+ hash.each_pair.with_index do |(key, value), index|
162
163
  if index >= max_keys
163
- result[:_more_keys] = hash.size - max_keys
164
+ result[:_more_keys] = BoundedInput.key_count(hash) - max_keys
164
165
  break
165
166
  end
166
167
 
168
+ if BoundedInput::COUNT_KEYS.include?(key) && value.is_a?(Integer)
169
+ result[key] = value
170
+ next
171
+ end
172
+
173
+ if key.to_s.bytesize > max_key_bytes
174
+ oversized_keys += 1
175
+ next
176
+ end
177
+
178
+ # Checked again: each byte that isn't valid UTF-8 becomes a 3-byte U+FFFD
167
179
  key = valid_utf8_key(key)
180
+ if key.to_s.bytesize > max_key_bytes
181
+ oversized_keys += 1
182
+ next
183
+ end
168
184
  logstruct_field = recursion_depth == 0 && LOG_FIELD_KEYS.include?(key)
169
185
  key_path = path && [*path, key.to_s]
170
186
  # A top-level key's root is the line; the keys below it get the top-level field's value
@@ -173,7 +189,9 @@ module LogStruct
173
189
  # Check if this key should be filtered at any depth
174
190
  filtered = ParamFilters.should_filter_key?(key, value, logstruct_field: logstruct_field, path: key_path, root: key_root)
175
191
  # Keys are scrubbed too: an email address can be a key
176
- result[StringScrubber.scrub_name(key)] = if !filtered
192
+ scrubbed_key = StringScrubber.scrub_name(key)
193
+ scrubbed_key = scrub_limited_string(scrubbed_key) if scrubbed_key.is_a?(String) && scrubbed_key.bytesize > max_key_bytes
194
+ result[scrubbed_key] = if !filtered
177
195
  process_values(value, recursion_depth: recursion_depth + 1, ancestors: ancestors, path: key_path, root: children_root)
178
196
  elsif value == RAILS_FILTERED_MASK
179
197
  # Rails' parameter filter has already replaced the value
@@ -183,6 +201,8 @@ module LogStruct
183
201
  end
184
202
  end
185
203
 
204
+ result[:_oversized_keys] = oversized_keys if oversized_keys.positive?
205
+
186
206
  result
187
207
  end
188
208
 
@@ -205,7 +225,7 @@ module LogStruct
205
225
  fixed = valid_utf8(name)
206
226
  fixed.equal?(name) ? key : fixed.to_sym
207
227
  else
208
- key
228
+ valid_utf8(key.to_s)
209
229
  end
210
230
  end
211
231
 
@@ -295,15 +315,10 @@ module LogStruct
295
315
  log_value.to_s
296
316
  end
297
317
  rescue => e
298
- # Handle serialization errors
299
- context = {
300
- object_class: log_value.class.name,
301
- object_inspect: log_value.inspect.truncate(100)
302
- }
303
- LogStruct.handle_exception(e, source: Source::Internal, context: context)
304
-
305
- # Fall back to the string representation to ensure we continue processing
306
- log_value.to_s
318
+ LogStruct.handle_value_error(e, "value serialization failed", context: {object_class: log_value.class.name})
319
+
320
+ # An object's string representation may contain the same secret that broke serialization.
321
+ "[SERIALIZATION_ERROR]"
307
322
  end
308
323
  end
309
324
 
@@ -2,13 +2,14 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "openssl"
5
+ require "securerandom"
5
6
 
6
7
  module LogStruct
7
8
  # Utility module for hashing sensitive data
8
9
  module HashUtils
9
- # The key when there's neither a configured hash_salt nor a Rails app with a secret_key_base.
10
- # It's public, so these hashes can be matched to a list of candidate values.
11
- FALLBACK_KEY = "l0g5t0p"
10
+ # The key without a hash_salt, before the Rails app has its secret_key_base. A fixed key would be public,
11
+ # and anyone could match its hashes to a list of candidate emails; these hashes differ between processes.
12
+ FALLBACK_KEY = T.let(::SecureRandom.random_bytes(32).freeze, String)
12
13
 
13
14
  class << self
14
15
  extend T::Sig
@@ -17,7 +18,13 @@ module LogStruct
17
18
  sig { params(value: String).returns(String) }
18
19
  def hash_value(value)
19
20
  length = LogStruct.config.filters.hash_length
20
- OpenSSL::HMAC.hexdigest("SHA256", hash_key, value)[0...length] || "error"
21
+ max_bytes = LogStruct.config.limits.max_string_bytes
22
+ input = if value.bytesize > max_bytes
23
+ value.byteslice(0, max_bytes).to_s.b + "\0".b + value.bytesize.to_s.b
24
+ else
25
+ value
26
+ end
27
+ OpenSSL::HMAC.hexdigest("SHA256", hash_key, input)[0...length] || "error"
21
28
  end
22
29
 
23
30
  # The configured hash_salt, or a key derived from the app's secret_key_base
@@ -9,6 +9,7 @@ module LogStruct
9
9
  extend ActiveSupport::Concern
10
10
  extend T::Sig
11
11
  extend T::Helpers
12
+
12
13
  requires_ancestor { ::ActionMailer::Base }
13
14
  requires_ancestor { ErrorHandling }
14
15
 
@@ -7,6 +7,7 @@ module LogStruct
7
7
  # Handles collection of metadata for email logging
8
8
  module MetadataCollection
9
9
  extend T::Sig
10
+
10
11
  # Add message-specific metadata to log data
11
12
  sig { params(mailer: T.untyped, log_data: T::Hash[Symbol, T.untyped]).void }
12
13
  def self.add_message_metadata(mailer, log_data)
@@ -43,7 +43,7 @@ module LogStruct
43
43
 
44
44
  # Track subscription state keyed to the current Notifications.notifier instance
45
45
  State = ::Struct.new(:subscribed, :notifier_id)
46
- STATE = T.let(State.new(false, nil), State)
46
+ STATE = State.new(false, nil)
47
47
 
48
48
  # Set up SQL query logging integration
49
49
  sig { override.params(config: LogStruct::Configuration).returns(T.nilable(T::Boolean)) }
@@ -1,6 +1,8 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "../bounded_input"
5
+
4
6
  module LogStruct
5
7
  module Integrations
6
8
  # Ahoy analytics integration. If Ahoy is present, prepend a small hook to
@@ -18,7 +20,7 @@ module LogStruct
18
20
 
19
21
  # Takes whatever Ahoy's track takes and passes it on as given, so Ahoy's defaults still apply
20
22
  sig { params(name: T.untyped, args: T.untyped).returns(T.untyped) }
21
- def track(name, *args)
23
+ def track(name, *args) # rubocop:disable Sorbet/BlockMethodDefinition -- it's prepended and calls implicit super, which define_method can't do
22
24
  result = super
23
25
  begin
24
26
  LogStruct.info(
@@ -45,10 +47,9 @@ module LogStruct
45
47
  # Event properties can come from the browser, so they can be anything; a non-Hash is logged as its class
46
48
  sig { params(properties: T.untyped).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
47
49
  def self.properties_for_log(properties)
48
- properties = properties.to_unsafe_h if properties.respond_to?(:to_unsafe_h)
49
50
  case properties
50
51
  when nil then nil
51
- when Hash then properties.to_h { |key, value| [key.to_s.to_sym, value] }
52
+ when Hash, ::ActionController::Parameters then BoundedInput.symbolize_keys(properties)
52
53
  else {_class: properties.class.name}
53
54
  end
54
55
  end
@@ -6,6 +6,7 @@ begin
6
6
  rescue LoadError
7
7
  # CarrierWave gem is not available, integration will be skipped
8
8
  end
9
+ require_relative "../hash_utils"
9
10
 
10
11
  module LogStruct
11
12
  module Integrations
@@ -34,6 +35,7 @@ module LogStruct
34
35
  module LoggingMethods
35
36
  extend T::Sig
36
37
  extend T::Helpers
38
+
37
39
  requires_ancestor { ::CarrierWave::Uploader::Base }
38
40
 
39
41
  sig { params(args: T.untyped).returns(T.untyped) }
@@ -49,7 +51,6 @@ module LogStruct
49
51
  # file_fields is a Hash Sorbet can't check against the struct's keyword arguments
50
52
  Log::CarrierWave::Upload.new(
51
53
  **T.unsafe(file_fields),
52
- filename: file&.filename,
53
54
  mime_type: file&.content_type,
54
55
  size: file&.size,
55
56
  duration_ms: duration_ms
@@ -72,7 +73,6 @@ module LogStruct
72
73
  log_file_operation do
73
74
  Log::CarrierWave::Download.new(
74
75
  **T.unsafe(file_fields),
75
- filename: file&.filename,
76
76
  mime_type: file&.content_type,
77
77
  size: file&.size
78
78
  )
@@ -87,13 +87,11 @@ module LogStruct
87
87
  def file_fields
88
88
  {
89
89
  storage: storage.class.name.to_s.split("::").last.to_s.downcase.to_sym,
90
- file_id: identifier.to_s,
90
+ file_id: HashUtils.hash_value(identifier.to_s),
91
91
  uploader: self.class.name,
92
92
  model: model&.class&.name,
93
93
  mount_point: mounted_as&.to_s,
94
- version: version_name&.to_s,
95
- store_path: store_path,
96
- extension: file&.extension
94
+ version: version_name&.to_s
97
95
  }
98
96
  end
99
97
 
@@ -17,11 +17,12 @@ module LogStruct
17
17
  module Dotenv
18
18
  extend T::Sig
19
19
  extend IntegrationInterface
20
+
20
21
  @original_logger_setter = T.let(nil, T.nilable(UnboundMethod))
21
22
 
22
23
  # Internal state holder to avoid duplicate subscriptions in a Sorbet-friendly way
23
24
  State = ::Struct.new(:subscribed)
24
- STATE = T.let(State.new(false), State)
25
+ STATE = State.new(false)
25
26
 
26
27
  sig { override.params(config: LogStruct::Configuration).returns(T.nilable(T::Boolean)) }
27
28
  def self.setup(config)