logstruct 0.2.1 → 0.3.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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -0
  3. data/README.md +1 -1
  4. data/lib/log_struct/bounded_input.rb +101 -0
  5. data/lib/log_struct/concerns/configuration.rb +10 -10
  6. data/lib/log_struct/concerns/error_handling.rb +21 -0
  7. data/lib/log_struct/config_struct/filters.rb +23 -9
  8. data/lib/log_struct/config_struct/limits.rb +1 -1
  9. data/lib/log_struct/enums/log_field.rb +0 -3
  10. data/lib/log_struct/formatter.rb +54 -58
  11. data/lib/log_struct/hash_utils.rb +11 -4
  12. data/lib/log_struct/integrations/ahoy.rb +3 -2
  13. data/lib/log_struct/integrations/carrierwave.rb +3 -6
  14. data/lib/log_struct/integrations/lograge.rb +4 -10
  15. data/lib/log_struct/integrations/shrine.rb +1 -1
  16. data/lib/log_struct/log/action_mailer/delivered.rb +10 -0
  17. data/lib/log_struct/log/action_mailer/delivery.rb +10 -0
  18. data/lib/log_struct/log/action_mailer/error.rb +10 -0
  19. data/lib/log_struct/log/carrierwave/delete.rb +0 -4
  20. data/lib/log_struct/log/carrierwave/download.rb +0 -6
  21. data/lib/log_struct/log/carrierwave/upload.rb +0 -6
  22. data/lib/log_struct/log/carrierwave.rb +2 -8
  23. data/lib/log_struct/log/error.rb +10 -0
  24. data/lib/log_struct/log/plain.rb +10 -0
  25. data/lib/log_struct/log/puma/error.rb +10 -0
  26. data/lib/log_struct/log/puma/shutdown.rb +10 -0
  27. data/lib/log_struct/log/puma/start.rb +10 -0
  28. data/lib/log_struct/log/request.rb +10 -0
  29. data/lib/log_struct/log/security/blocked_host.rb +10 -0
  30. data/lib/log_struct/log/security/csrf_violation.rb +10 -0
  31. data/lib/log_struct/log/security/ip_spoof.rb +10 -0
  32. data/lib/log_struct/log/sidekiq/error.rb +10 -0
  33. data/lib/log_struct/log/sidekiq/log.rb +10 -0
  34. data/lib/log_struct/log/sql.rb +10 -0
  35. data/lib/log_struct/monkey_patches/active_support/tagged_logging/formatter.rb +5 -3
  36. data/lib/log_struct/param_filters.rb +10 -28
  37. data/lib/log_struct/shared/merge_additional_data_fields.rb +21 -5
  38. data/lib/log_struct/string_scrubber.rb +7 -6
  39. data/lib/log_struct/version.rb +1 -1
  40. data/logstruct.gemspec +1 -1
  41. metadata +4 -2
@@ -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
@@ -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
@@ -49,7 +50,6 @@ module LogStruct
49
50
  # file_fields is a Hash Sorbet can't check against the struct's keyword arguments
50
51
  Log::CarrierWave::Upload.new(
51
52
  **T.unsafe(file_fields),
52
- filename: file&.filename,
53
53
  mime_type: file&.content_type,
54
54
  size: file&.size,
55
55
  duration_ms: duration_ms
@@ -72,7 +72,6 @@ module LogStruct
72
72
  log_file_operation do
73
73
  Log::CarrierWave::Download.new(
74
74
  **T.unsafe(file_fields),
75
- filename: file&.filename,
76
75
  mime_type: file&.content_type,
77
76
  size: file&.size
78
77
  )
@@ -87,13 +86,11 @@ module LogStruct
87
86
  def file_fields
88
87
  {
89
88
  storage: storage.class.name.to_s.split("::").last.to_s.downcase.to_sym,
90
- file_id: identifier.to_s,
89
+ file_id: HashUtils.hash_value(identifier.to_s),
91
90
  uploader: self.class.name,
92
91
  model: model&.class&.name,
93
92
  mount_point: mounted_as&.to_s,
94
- version: version_name&.to_s,
95
- store_path: store_path,
96
- extension: file&.extension
93
+ version: version_name&.to_s
97
94
  }
98
95
  end
99
96
 
@@ -6,6 +6,7 @@ begin
6
6
  rescue LoadError
7
7
  # Lograge gem is not available, integration will be skipped
8
8
  end
9
+ require_relative "../bounded_input"
9
10
 
10
11
  module LogStruct
11
12
  module Integrations
@@ -196,12 +197,12 @@ module LogStruct
196
197
  result = custom_options_proc.call(event, options)
197
198
  return if !result.is_a?(Hash) || result.equal?(options)
198
199
 
199
- options.merge!(result.to_h { |key, value| [key.to_s.to_sym, value] })
200
+ options.merge!(BoundedInput.symbolize_keys(result))
200
201
  end
201
202
 
202
203
  sig { params(data: T::Hash[T.any(Symbol, String), T.untyped]).returns(Log::Request) }
203
204
  def build_request_log(data)
204
- normalized_data = normalize_lograge_data(data)
205
+ normalized_data = BoundedInput.symbolize_keys(data)
205
206
 
206
207
  # Coerce common fields to expected types
207
208
  status = ((s = normalized_data[:status]) && s.respond_to?(:to_i)) ? s.to_i : s
@@ -210,7 +211,7 @@ module LogStruct
210
211
  db = ((b = normalized_data[:db]) && b.respond_to?(:to_f)) ? b.to_f : b
211
212
 
212
213
  params = normalized_data[:params]
213
- params = params.deep_symbolize_keys if params&.respond_to?(:deep_symbolize_keys)
214
+ params = BoundedInput.symbolize_keys(params) if params.is_a?(Hash) || params.is_a?(::ActionController::Parameters)
214
215
 
215
216
  additional_data = extract_additional_data(normalized_data)
216
217
 
@@ -237,13 +238,6 @@ module LogStruct
237
238
  )
238
239
  end
239
240
 
240
- sig { params(data: T::Hash[T.any(Symbol, String), T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
241
- def normalize_lograge_data(data)
242
- data.each_with_object({}) do |(key, value), normalized|
243
- normalized[key.to_s.to_sym] = value
244
- end
245
- end
246
-
247
241
  sig { params(data: T::Hash[Symbol, T.untyped]).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
248
242
  def extract_additional_data(data)
249
243
  extras = T.let({}, T::Hash[Symbol, T.untyped])
@@ -104,7 +104,7 @@ module LogStruct
104
104
  end
105
105
 
106
106
  # Upload options with the file names taken out of a Content-Disposition (upload_options:
107
- # {content_disposition: ...}, which the upload_options plugin often sets): they can name a person
107
+ # `{content_disposition: ...}`, which the upload_options plugin often sets): they can name a person
108
108
  sig { params(options: T.untyped).returns(T.untyped) }
109
109
  def self.without_file_names(options)
110
110
  return options unless options.is_a?(Hash)
@@ -42,6 +42,16 @@ module LogStruct
42
42
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
43
43
  include LogStruct::Log::Shared::MergeAdditionalDataFields
44
44
 
45
+ FIELD_KEYS = T.let(
46
+ [LogField::To, LogField::From, LogField::Subject, LogField::MessageId, LogField::MailerClass, LogField::MailerAction, LogField::AttachmentCount].to_set(&:serialize).freeze,
47
+ T::Set[Symbol]
48
+ )
49
+
50
+ sig { override.returns(T::Set[Symbol]) }
51
+ def field_keys
52
+ FIELD_KEYS
53
+ end
54
+
45
55
  # Serialize shared fields
46
56
  include LogStruct::Log::Interfaces::CommonFields
47
57
  include LogStruct::Log::Shared::SerializeCommon
@@ -42,6 +42,16 @@ module LogStruct
42
42
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
43
43
  include LogStruct::Log::Shared::MergeAdditionalDataFields
44
44
 
45
+ FIELD_KEYS = T.let(
46
+ [LogField::To, LogField::From, LogField::Subject, LogField::MessageId, LogField::MailerClass, LogField::MailerAction, LogField::AttachmentCount].to_set(&:serialize).freeze,
47
+ T::Set[Symbol]
48
+ )
49
+
50
+ sig { override.returns(T::Set[Symbol]) }
51
+ def field_keys
52
+ FIELD_KEYS
53
+ end
54
+
45
55
  # Serialize shared fields
46
56
  include LogStruct::Log::Interfaces::CommonFields
47
57
  include LogStruct::Log::Shared::SerializeCommon
@@ -47,6 +47,16 @@ module LogStruct
47
47
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
48
48
  include LogStruct::Log::Shared::MergeAdditionalDataFields
49
49
 
50
+ FIELD_KEYS = T.let(
51
+ [LogField::To, LogField::From, LogField::Subject, LogField::MessageId, LogField::MailerClass, LogField::MailerAction, LogField::AttachmentCount, LogField::ErrorClass, LogField::Message, LogField::Backtrace].to_set(&:serialize).freeze,
52
+ T::Set[Symbol]
53
+ )
54
+
55
+ sig { override.returns(T::Set[Symbol]) }
56
+ def field_keys
57
+ FIELD_KEYS
58
+ end
59
+
50
60
  # Serialize shared fields
51
61
  include LogStruct::Log::Interfaces::CommonFields
52
62
  include LogStruct::Log::Shared::SerializeCommon
@@ -35,8 +35,6 @@ module LogStruct
35
35
  const :model, T.nilable(String), default: nil
36
36
  const :mount_point, T.nilable(String), default: nil
37
37
  const :version, T.nilable(String), default: nil
38
- const :store_path, T.nilable(String), default: nil
39
- const :extension, T.nilable(String), default: nil
40
38
 
41
39
  # Serialize shared fields
42
40
  include LogStruct::Log::Interfaces::CommonFields
@@ -51,8 +49,6 @@ module LogStruct
51
49
  h[LogField::Model] = model unless model.nil?
52
50
  h[LogField::MountPoint] = mount_point unless mount_point.nil?
53
51
  h[LogField::Version] = version unless version.nil?
54
- h[LogField::StorePath] = store_path unless store_path.nil?
55
- h[LogField::Extension] = extension unless extension.nil?
56
52
  h
57
53
  end
58
54
  end
@@ -35,11 +35,8 @@ module LogStruct
35
35
  const :model, T.nilable(String), default: nil
36
36
  const :mount_point, T.nilable(String), default: nil
37
37
  const :version, T.nilable(String), default: nil
38
- const :store_path, T.nilable(String), default: nil
39
- const :extension, T.nilable(String), default: nil
40
38
 
41
39
  # Event-specific fields
42
- const :filename, T.nilable(String), default: nil
43
40
  const :mime_type, T.nilable(String), default: nil
44
41
  const :size, T.nilable(Integer), default: nil
45
42
 
@@ -56,9 +53,6 @@ module LogStruct
56
53
  h[LogField::Model] = model unless model.nil?
57
54
  h[LogField::MountPoint] = mount_point unless mount_point.nil?
58
55
  h[LogField::Version] = version unless version.nil?
59
- h[LogField::StorePath] = store_path unless store_path.nil?
60
- h[LogField::Extension] = extension unless extension.nil?
61
- h[LogField::Filename] = filename unless filename.nil?
62
56
  h[LogField::MimeType] = mime_type unless mime_type.nil?
63
57
  h[LogField::Size] = size unless size.nil?
64
58
  h
@@ -35,11 +35,8 @@ module LogStruct
35
35
  const :model, T.nilable(String), default: nil
36
36
  const :mount_point, T.nilable(String), default: nil
37
37
  const :version, T.nilable(String), default: nil
38
- const :store_path, T.nilable(String), default: nil
39
- const :extension, T.nilable(String), default: nil
40
38
 
41
39
  # Event-specific fields
42
- const :filename, T.nilable(String), default: nil
43
40
  const :mime_type, T.nilable(String), default: nil
44
41
  const :size, T.nilable(Integer), default: nil
45
42
  const :metadata, T.nilable(T::Hash[String, T.untyped]), default: nil
@@ -58,9 +55,6 @@ module LogStruct
58
55
  h[LogField::Model] = model unless model.nil?
59
56
  h[LogField::MountPoint] = mount_point unless mount_point.nil?
60
57
  h[LogField::Version] = version unless version.nil?
61
- h[LogField::StorePath] = store_path unless store_path.nil?
62
- h[LogField::Extension] = extension unless extension.nil?
63
- h[LogField::Filename] = filename unless filename.nil?
64
58
  h[LogField::MimeType] = mime_type unless mime_type.nil?
65
59
  h[LogField::Size] = size unless size.nil?
66
60
  h[LogField::Metadata] = metadata unless metadata.nil?
@@ -21,8 +21,6 @@ module LogStruct
21
21
  const :model, T.nilable(String), default: nil
22
22
  const :mount_point, T.nilable(String), default: nil
23
23
  const :version, T.nilable(String), default: nil
24
- const :store_path, T.nilable(String), default: nil
25
- const :extension, T.nilable(String), default: nil
26
24
 
27
25
  Kwargs = T.type_alias do
28
26
  {
@@ -31,9 +29,7 @@ module LogStruct
31
29
  uploader: T.nilable(String),
32
30
  model: T.nilable(String),
33
31
  mount_point: T.nilable(String),
34
- version: T.nilable(String),
35
- store_path: T.nilable(String),
36
- extension: T.nilable(String)
32
+ version: T.nilable(String)
37
33
  }
38
34
  end
39
35
 
@@ -45,9 +41,7 @@ module LogStruct
45
41
  uploader: uploader,
46
42
  model: model,
47
43
  mount_point: mount_point,
48
- version: version,
49
- store_path: store_path,
50
- extension: extension
44
+ version: version
51
45
  }
52
46
  end
53
47
  end
@@ -48,6 +48,16 @@ module LogStruct
48
48
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
49
49
  include LogStruct::Log::Shared::MergeAdditionalDataFields
50
50
 
51
+ FIELD_KEYS = T.let(
52
+ [LogField::Path, LogField::HttpMethod, LogField::SourceIp, LogField::UserAgent, LogField::Referer, LogField::ErrorClass, LogField::Message, LogField::ErrorMessage, LogField::Backtrace, LogField::Status, LogField::CauseClass, LogField::CauseMessage].to_set(&:serialize).freeze,
53
+ T::Set[Symbol]
54
+ )
55
+
56
+ sig { override.returns(T::Set[Symbol]) }
57
+ def field_keys
58
+ FIELD_KEYS
59
+ end
60
+
51
61
  # Request fields (optional)
52
62
  include LogStruct::Log::Interfaces::RequestFields
53
63
 
@@ -36,6 +36,16 @@ module LogStruct
36
36
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
37
37
  include LogStruct::Log::Shared::MergeAdditionalDataFields
38
38
 
39
+ FIELD_KEYS = T.let(
40
+ [LogField::Message].to_set(&:serialize).freeze,
41
+ T::Set[Symbol]
42
+ )
43
+
44
+ sig { override.returns(T::Set[Symbol]) }
45
+ def field_keys
46
+ FIELD_KEYS
47
+ end
48
+
39
49
  # Serialize shared fields
40
50
  include LogStruct::Log::Interfaces::CommonFields
41
51
  include LogStruct::Log::Shared::SerializeCommon
@@ -42,6 +42,16 @@ module LogStruct
42
42
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
43
43
  include LogStruct::Log::Shared::MergeAdditionalDataFields
44
44
 
45
+ FIELD_KEYS = T.let(
46
+ [LogField::ErrorClass, LogField::Message, LogField::Backtrace, LogField::ErrorContext, LogField::HttpMethod, LogField::Path].to_set(&:serialize).freeze,
47
+ T::Set[Symbol]
48
+ )
49
+
50
+ sig { override.returns(T::Set[Symbol]) }
51
+ def field_keys
52
+ FIELD_KEYS
53
+ end
54
+
45
55
  # Serialize shared fields
46
56
  include LogStruct::Log::Interfaces::CommonFields
47
57
  include LogStruct::Log::Shared::SerializeCommon
@@ -37,6 +37,16 @@ module LogStruct
37
37
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
38
38
  include LogStruct::Log::Shared::MergeAdditionalDataFields
39
39
 
40
+ FIELD_KEYS = T.let(
41
+ [LogField::ProcessId].to_set(&:serialize).freeze,
42
+ T::Set[Symbol]
43
+ )
44
+
45
+ sig { override.returns(T::Set[Symbol]) }
46
+ def field_keys
47
+ FIELD_KEYS
48
+ end
49
+
40
50
  # Serialize shared fields
41
51
  include LogStruct::Log::Interfaces::CommonFields
42
52
  include LogStruct::Log::Shared::SerializeCommon
@@ -45,6 +45,16 @@ module LogStruct
45
45
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
46
46
  include LogStruct::Log::Shared::MergeAdditionalDataFields
47
47
 
48
+ FIELD_KEYS = T.let(
49
+ [LogField::Mode, LogField::PumaVersion, LogField::PumaCodename, LogField::RubyVersion, LogField::MinThreads, LogField::MaxThreads, LogField::Environment, LogField::ProcessId, LogField::ListeningAddresses].to_set(&:serialize).freeze,
50
+ T::Set[Symbol]
51
+ )
52
+
53
+ sig { override.returns(T::Set[Symbol]) }
54
+ def field_keys
55
+ FIELD_KEYS
56
+ end
57
+
48
58
  # Serialize shared fields
49
59
  include LogStruct::Log::Interfaces::CommonFields
50
60
  include LogStruct::Log::Shared::SerializeCommon
@@ -52,6 +52,16 @@ module LogStruct
52
52
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
53
53
  include LogStruct::Log::Shared::MergeAdditionalDataFields
54
54
 
55
+ FIELD_KEYS = T.let(
56
+ [LogField::Path, LogField::HttpMethod, LogField::SourceIp, LogField::UserAgent, LogField::Referer, LogField::Format, LogField::Controller, LogField::Action, LogField::Status, LogField::DurationMs, LogField::View, LogField::Database, LogField::Params, LogField::Host, LogField::ContentType, LogField::Accept].to_set(&:serialize).freeze,
57
+ T::Set[Symbol]
58
+ )
59
+
60
+ sig { override.returns(T::Set[Symbol]) }
61
+ def field_keys
62
+ FIELD_KEYS
63
+ end
64
+
55
65
  # Request fields (optional)
56
66
  include LogStruct::Log::Interfaces::RequestFields
57
67
 
@@ -46,6 +46,16 @@ module LogStruct
46
46
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
47
47
  include LogStruct::Log::Shared::MergeAdditionalDataFields
48
48
 
49
+ FIELD_KEYS = T.let(
50
+ [LogField::Path, LogField::HttpMethod, LogField::SourceIp, LogField::UserAgent, LogField::Referer, LogField::Message, LogField::BlockedHost, LogField::BlockedHosts, LogField::XForwardedFor].to_set(&:serialize).freeze,
51
+ T::Set[Symbol]
52
+ )
53
+
54
+ sig { override.returns(T::Set[Symbol]) }
55
+ def field_keys
56
+ FIELD_KEYS
57
+ end
58
+
49
59
  # Request fields (optional)
50
60
  include LogStruct::Log::Interfaces::RequestFields
51
61
 
@@ -43,6 +43,16 @@ module LogStruct
43
43
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
44
44
  include LogStruct::Log::Shared::MergeAdditionalDataFields
45
45
 
46
+ FIELD_KEYS = T.let(
47
+ [LogField::Path, LogField::HttpMethod, LogField::SourceIp, LogField::UserAgent, LogField::Referer, LogField::Message].to_set(&:serialize).freeze,
48
+ T::Set[Symbol]
49
+ )
50
+
51
+ sig { override.returns(T::Set[Symbol]) }
52
+ def field_keys
53
+ FIELD_KEYS
54
+ end
55
+
46
56
  # Request fields (optional)
47
57
  include LogStruct::Log::Interfaces::RequestFields
48
58
 
@@ -45,6 +45,16 @@ module LogStruct
45
45
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
46
46
  include LogStruct::Log::Shared::MergeAdditionalDataFields
47
47
 
48
+ FIELD_KEYS = T.let(
49
+ [LogField::Path, LogField::HttpMethod, LogField::SourceIp, LogField::UserAgent, LogField::Referer, LogField::Message, LogField::ClientIp, LogField::XForwardedFor].to_set(&:serialize).freeze,
50
+ T::Set[Symbol]
51
+ )
52
+
53
+ sig { override.returns(T::Set[Symbol]) }
54
+ def field_keys
55
+ FIELD_KEYS
56
+ end
57
+
48
58
  # Request fields (optional)
49
59
  include LogStruct::Log::Interfaces::RequestFields
50
60
 
@@ -42,6 +42,16 @@ module LogStruct
42
42
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
43
43
  include LogStruct::Log::Shared::MergeAdditionalDataFields
44
44
 
45
+ FIELD_KEYS = T.let(
46
+ [LogField::ErrorClass, LogField::Message, LogField::Backtrace, LogField::ErrorContext, LogField::QueueName, LogField::RetryCount].to_set(&:serialize).freeze,
47
+ T::Set[Symbol]
48
+ )
49
+
50
+ sig { override.returns(T::Set[Symbol]) }
51
+ def field_keys
52
+ FIELD_KEYS
53
+ end
54
+
45
55
  # Serialize shared fields
46
56
  include LogStruct::Log::Interfaces::CommonFields
47
57
  include LogStruct::Log::Shared::SerializeCommon
@@ -40,6 +40,16 @@ module LogStruct
40
40
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
41
41
  include LogStruct::Log::Shared::MergeAdditionalDataFields
42
42
 
43
+ FIELD_KEYS = T.let(
44
+ [LogField::Message, LogField::Context, LogField::ProcessId, LogField::ThreadId].to_set(&:serialize).freeze,
45
+ T::Set[Symbol]
46
+ )
47
+
48
+ sig { override.returns(T::Set[Symbol]) }
49
+ def field_keys
50
+ FIELD_KEYS
51
+ end
52
+
43
53
  # Serialize shared fields
44
54
  include LogStruct::Log::Interfaces::CommonFields
45
55
  include LogStruct::Log::Shared::SerializeCommon
@@ -47,6 +47,16 @@ module LogStruct
47
47
  const :additional_data, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]), default: nil
48
48
  include LogStruct::Log::Shared::MergeAdditionalDataFields
49
49
 
50
+ FIELD_KEYS = T.let(
51
+ [LogField::Message, LogField::Sql, LogField::Name, LogField::DurationMs, LogField::RowCount, LogField::Adapter, LogField::BindParams, LogField::DatabaseName, LogField::ConnectionPoolSize, LogField::ActiveConnections, LogField::OperationType, LogField::TableNames].to_set(&:serialize).freeze,
52
+ T::Set[Symbol]
53
+ )
54
+
55
+ sig { override.returns(T::Set[Symbol]) }
56
+ def field_keys
57
+ FIELD_KEYS
58
+ end
59
+
50
60
  # Serialize shared fields
51
61
  include LogStruct::Log::Interfaces::CommonFields
52
62
  include LogStruct::Log::Shared::SerializeCommon
@@ -6,8 +6,8 @@ require "active_support/tagged_logging"
6
6
  # Monkey-patch ActiveSupport::TaggedLogging::Formatter to work with structured logging.
7
7
  #
8
8
  # Problem: Rails' TaggedLogging prepends tags as text and converts messages to strings.
9
- # When we pass a hash to super(), Rails does "#{tags_text}#{msg}" which calls .to_s
10
- # on the hash, producing Ruby inspect format {message: "..."} instead of valid JSON.
9
+ # When we pass a hash to super(), Rails does `"#{tags_text}#{msg}"` which calls .to_s
10
+ # on the hash, producing Ruby inspect format `{message: "..."}` instead of valid JSON.
11
11
  #
12
12
  # Solution: When LogStruct is enabled, we add tags as a hash entry and delegate to
13
13
  # LogStruct::Formatter for proper JSON serialization with filtering and standard fields.
@@ -58,4 +58,6 @@ module ActiveSupport
58
58
  end
59
59
  end
60
60
 
61
- ActiveSupport::TaggedLogging::Formatter.prepend(ActiveSupport::TaggedLogging::FormatterExtension)
61
+ module ActiveSupport::TaggedLogging::Formatter
62
+ prepend ActiveSupport::TaggedLogging::FormatterExtension
63
+ end
@@ -43,8 +43,9 @@ module LogStruct
43
43
 
44
44
  matcher.matches?(normalized_key, value)
45
45
  rescue => e
46
- handle_filter_matcher_error(e, matcher.label, normalized_key)
47
- false
46
+ handle_filter_matcher_error(e, matcher.label)
47
+ # An unsuccessful filter must never expose the value it was meant to protect.
48
+ true
48
49
  end
49
50
  return true if matched
50
51
  return false if filters.path_matchers.empty?
@@ -54,8 +55,8 @@ module LogStruct
54
55
  filters.path_matchers.any? do |matcher|
55
56
  matcher.matches?(path, value, root)
56
57
  rescue => e
57
- handle_filter_matcher_error(e, matcher.label, normalized_key)
58
- false
58
+ handle_filter_matcher_error(e, matcher.label)
59
+ true
59
60
  end
60
61
  end
61
62
 
@@ -93,16 +94,14 @@ module LogStruct
93
94
  filtered_string
94
95
  end
95
96
 
96
- # Summarize a filtered Hash: its first 10 keys and how many there are. No size, which would say
97
- # how long the secrets in it are.
97
+ # A filtered Hash's child keys can themselves contain secrets.
98
98
  sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
99
99
  def summarize_hash(hash)
100
100
  return {_class: "Hash", _empty: true} if hash.empty?
101
101
 
102
102
  {
103
103
  _class: Hash,
104
- _keys_count: hash.keys.size,
105
- _keys: hash.keys.take(10).map { |key| normalize_summary_key(StringScrubber.scrub_name(key)) }
104
+ _keys_count: hash.size
106
105
  }
107
106
  end
108
107
 
@@ -119,26 +118,9 @@ module LogStruct
119
118
 
120
119
  private
121
120
 
122
- sig { params(key: T.any(String, Symbol, Integer, T.untyped)).returns(T.any(Symbol, String)) }
123
- def normalize_summary_key(key)
124
- if key.is_a?(Symbol)
125
- key
126
- elsif key.respond_to?(:to_sym)
127
- key.to_sym
128
- else
129
- key.to_s
130
- end
131
- rescue
132
- key.to_s
133
- end
134
-
135
- sig { params(error: StandardError, label: String, key: String).void }
136
- def handle_filter_matcher_error(error, label, key)
137
- context = {
138
- matcher: label,
139
- key: key
140
- }
141
- LogStruct.handle_exception(error, source: Source::Internal, context: context)
121
+ sig { params(error: StandardError, label: String).void }
122
+ def handle_filter_matcher_error(error, label)
123
+ LogStruct.handle_value_error(error, "filter matcher failed", context: {matcher: label})
142
124
  end
143
125
  end
144
126
  end
@@ -3,6 +3,7 @@
3
3
 
4
4
  require_relative "interfaces/additional_data_field"
5
5
  require_relative "../enums/source"
6
+ require_relative "../enums/log_field"
6
7
 
7
8
  module LogStruct
8
9
  module Log
@@ -14,17 +15,32 @@ module LogStruct
14
15
  requires_ancestor { T::Struct }
15
16
  requires_ancestor { Interfaces::AdditionalDataField }
16
17
 
17
- # Reserved keys that cannot be overwritten by additional_data.
18
- # These are the core log structure fields that must not be modified.
19
- RESERVED_KEYS = T.let(%i[src evt lvl ts].freeze, T::Array[Symbol])
18
+ # Every line's own fields, and the request ID the formatter adds unless the line has one
19
+ RESERVED_KEYS = T.let(
20
+ [LogField::Source, LogField::Event, LogField::Level, LogField::Timestamp, LogField::RequestId].to_set(&:serialize).freeze,
21
+ T::Set[Symbol]
22
+ )
23
+
24
+ NO_FIELD_KEYS = T.let(Set.new.freeze, T::Set[Symbol])
25
+
26
+ # The keys of every field of the struct's type, set or not on this line, which additional_data can't
27
+ # set: it would replace the field, or stand in for one the type didn't write. LogStruct's log types list
28
+ # theirs. A custom type's serialize writes its own fields after merging additional_data, so they win.
29
+ sig { overridable.returns(T::Set[Symbol]) }
30
+ def field_keys
31
+ NO_FIELD_KEYS
32
+ end
20
33
 
21
34
  sig { params(hash: T::Hash[Symbol, T.untyped]).void }
22
35
  def merge_additional_data_fields(hash)
23
36
  ad = additional_data
24
37
  return unless ad
25
- ad.each do |key, value|
38
+ ad.each_pair.with_index do |(key, value), index|
39
+ break if index >= LogStruct.config.limits.max_hash_keys
40
+ next if key.to_s.bytesize > LogStruct.config.limits.max_string_bytes
41
+
26
42
  sym_key = key.to_sym
27
- if RESERVED_KEYS.include?(sym_key)
43
+ if RESERVED_KEYS.include?(sym_key) || field_keys.include?(sym_key)
28
44
  LogStruct.handle_exception(
29
45
  ArgumentError.new("additional_data attempted to overwrite reserved key: #{sym_key}"),
30
46
  source: Source::Internal
@@ -239,7 +239,7 @@ module LogStruct
239
239
  def authorization_credentials?(scheme, credentials)
240
240
  return credentials.length >= 16 if scheme.casecmp?("bearer")
241
241
 
242
- credentials.length >= 8 && Base64.strict_decode64(credentials).include?(":")
242
+ Base64.strict_decode64(credentials).include?(":")
243
243
  rescue ArgumentError
244
244
  false
245
245
  end
@@ -260,7 +260,7 @@ module LogStruct
260
260
  end
261
261
  end
262
262
 
263
- # user[password] is checked as password, written plainly or URL-encoded
263
+ # Every part of a nested name is checked: both password[] and token[value] carry secrets.
264
264
  sig { params(name: String, value: String).returns(T::Boolean) }
265
265
  def sensitive_query_param?(name, value)
266
266
  decoded = begin
@@ -268,10 +268,11 @@ module LogStruct
268
268
  rescue ArgumentError
269
269
  name
270
270
  end
271
- key = decoded[/\[([^\[\]]*)\]\z/, 1] || decoded
272
- return false if key.empty?
273
-
274
- SENSITIVE_QUERY_PARAMS.include?(key.downcase) || ParamFilters.should_filter_key?(key, value)
271
+ path = T.let([], T::Array[String])
272
+ decoded.split(/[\[\]]/).reject(&:empty?).any? do |key|
273
+ path << key
274
+ SENSITIVE_QUERY_PARAMS.include?(key.downcase) || ParamFilters.should_filter_key?(key, value, path: path)
275
+ end
275
276
  end
276
277
 
277
278
  # The Luhn checksum that every card number passes
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module LogStruct
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0"
6
6
  end