rails-profiler 0.45.1 → 0.45.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af2f64f9bd55d0aa4a85949ef9b1acd256be278cff05a4659541f270f8a6f7fa
4
- data.tar.gz: 36ca8a5422e3fef0333d10b7d57a9c15cf9a66ac11fbe2daadc3a18685d95f8d
3
+ metadata.gz: 5e77f6e8c5a2b0148fc9288da73e9c9f464e57e3fadad34df3c5e96b7d8af2de
4
+ data.tar.gz: 6877c02a32028e0c501bf0ac4e0249b91d97c7865b94ccbba9fe497ebfa3eebd
5
5
  SHA512:
6
- metadata.gz: 9cb61932d90337b7428435ad21ebceb9ac29cb8c634c2ef61bfe2fbde6ea9330201286f54d3d0ddffe034158f8113650c52a1d3ea2b5ccc367c147859cea0be0
7
- data.tar.gz: 327222d2579665d70af75527d59459aac46fbd454f5c94476cbda59d516fa314790a7575405c8145ea12023c6635a74bc59cafcdccc8e3e7f02ae2b4b82d029c
6
+ metadata.gz: b2e27e15a4774ffbf717c7f76e8f6ff8f46001e452e59adfcfdfb9219ec3fd9611e19790ba061c846c964f71a1c1787e3486b3fcdc3e60fe89bbe68244289c9d
7
+ data.tar.gz: a9c1be674583b25ba7a2a6d901b36e76ac188c19be8f219c2a607fe280bb37b77022c3ca91a2b69f5c58508582267613f9e72a8eb004dd6eb645f5dcddc9265b
data/CHANGELOG.md CHANGED
@@ -19,6 +19,14 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.45.2] - 2026-10-27
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **HTTP capture:** A JSON, NDJSON or form body longer than `max_captured_body_bytes` is now redacted whole and the result cut to the limit, so a large response or request shows its first bytes with sensitive values masked instead of a single `[FILTERED: unparseable ... body]` mask. A body whose whole text is not available (inflation stopped at the limit, a partly read `body_stream`) or that exceeds 8 MiB is masked with a `[FILTERED: truncated ... body, kept N of M bytes, ...]` label that says it was truncated, and only a genuinely invalid JSON keeps the `unparseable` label.
29
+
22
30
  ## [0.45.1] - 2026-10-27
23
31
 
24
32
  <!-- stamped -->
@@ -37,7 +37,15 @@ module Profiler
37
37
  req_headers = Redaction.filter_headers(req.to_hash.transform_values { |v| v.join(", ") })
38
38
  req_content_type = req["content-type"].to_s
39
39
  req_body = captured[:content]
40
- processed_req = req_body.nil? || req_body.empty? ? { body: nil, encoding: "text" } : NetHttpInstrumentation.process_body(req_body, req_content_type)
40
+ processed_req = if req_body.nil? || req_body.empty?
41
+ { body: nil, encoding: "text" }
42
+ else
43
+ NetHttpInstrumentation.process_body(
44
+ req_body, req_content_type,
45
+ limit: captured[:truncated] ? limit : nil,
46
+ size: captured[:size_is_minimum] ? nil : captured[:size]
47
+ )
48
+ end
41
49
 
42
50
  request_id = SecureRandom.hex(8)
43
51
  started_at = Time.now.iso8601(3)
@@ -75,9 +83,13 @@ module Profiler
75
83
  resp_content_encoding = response["content-encoding"].to_s.strip.downcase
76
84
  resp_body = NetHttpInstrumentation.decompress_body(resp_body_raw, resp_content_encoding, limit)
77
85
  resp_truncated = limit ? resp_body.bytesize > limit : false
78
- resp_body = Redaction.cut_bytes(resp_body, limit) if resp_truncated
79
86
  resp_content_type = response["content-type"].to_s
80
- processed_resp = NetHttpInstrumentation.process_body(resp_body, resp_content_type)
87
+ # Cut after the redaction, not before: a cut JSON text cannot be parsed. Only an
88
+ # inflation stopped at the limit leaves a beginning with no known whole size.
89
+ resp_whole_size = !resp_truncated || resp_body.equal?(resp_body_raw) ? resp_body.bytesize : nil
90
+ processed_resp = NetHttpInstrumentation.process_body(
91
+ resp_body, resp_content_type, limit: resp_truncated ? limit : nil, size: resp_whole_size
92
+ )
81
93
 
82
94
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
83
95
 
@@ -200,8 +212,8 @@ module Profiler
200
212
  unless content.nil? || content.to_s.empty?
201
213
  content = content.to_s
202
214
  truncated = limit ? content.bytesize > limit : false
203
- return { content: truncated ? Redaction.cut_bytes(content, limit) : content,
204
- size: content.bytesize, size_is_minimum: false, truncated: truncated, not_captured: false }
215
+ # Kept whole: process_body redacts it, then cuts the result.
216
+ return { content: content, size: content.bytesize, size_is_minimum: false, truncated: truncated, not_captured: false }
205
217
  end
206
218
 
207
219
  stream = req.body_stream
@@ -295,18 +307,24 @@ module Profiler
295
307
  defined?(Net::ReadAdapter) && response.body.is_a?(Net::ReadAdapter)
296
308
  end
297
309
 
298
- def self.process_body(body, content_type)
310
+ # +limit+ is set when the body is kept only in part: it is then redacted whole and the result
311
+ # cut to +limit+ bytes (Redaction.filter_truncated_body). +size+ is the length of the whole
312
+ # body, nil when it is not known.
313
+ def self.process_body(body, content_type, limit: nil, size: body&.bytesize)
299
314
  return { body: nil, encoding: "text" } if body.nil? || body.empty?
300
315
 
301
316
  if BodyContent.binary?(body, content_type)
302
- # Masked on the raw bytes, before the encoding hides them from any later search.
303
- { body: Base64.strict_encode64(Redaction.hide_credentials(body.b)), encoding: "base64" }
317
+ # Masked on the raw bytes, before the encoding hides them from any later search, and
318
+ # cut after the masking: a credential cut in two would escape it.
319
+ masked = Redaction.hide_credentials(body.b)
320
+ masked = Redaction.cut_bytes(masked, limit) if limit
321
+ { body: Base64.strict_encode64(masked), encoding: "base64" }
304
322
  else
305
323
  # Bytes are read as UTF-8, as a text body on the wire is; what is not valid UTF-8 shows
306
324
  # as "?".
307
325
  body = body.dup.force_encoding(Encoding::UTF_8) if body.encoding == Encoding::BINARY
308
- text = Redaction.filter_body(body, content_type)
309
- .encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
326
+ filtered = limit ? Redaction.filter_truncated_body(body, content_type, limit, size) : Redaction.filter_body(body, content_type)
327
+ text = filtered.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
310
328
  text = text.scrub("?") unless text.valid_encoding?
311
329
  { body: text, encoding: "text" }
312
330
  end
@@ -36,6 +36,11 @@ module Profiler
36
36
  FORM_TYPE = "application/x-www-form-urlencoded"
37
37
  MULTIPART_TYPE = %r{\Amultipart/}i
38
38
 
39
+ # The largest body filter_truncated_body parses to redact it whole. A bigger JSON, NDJSON or
40
+ # form body is not parsed (a parse costs several times its size in memory) and is masked with
41
+ # a label instead, never passed through unredacted.
42
+ MAX_REDACTABLE_BODY_BYTES = 8 * 1024 * 1024
43
+
39
44
  # The characters outside ASCII whose case folding yields ASCII letters
40
45
  # (Unicode CaseFolding.txt): with any of them, a case-insensitive regexp
41
46
  # can match an ASCII filter where a plain ASCII search cannot.
@@ -52,7 +57,7 @@ module Profiler
52
57
  # profile can be read on any node. Wraps every public filter, so that each
53
58
  # capture point gets it.
54
59
  module CredentialMasking
55
- %i[filter_hash filter_value filter_named filter_headers filter_body filter_query filter_url
60
+ %i[filter_hash filter_value filter_named filter_headers filter_body filter_truncated_body filter_query filter_url
56
61
  env_snapshot env_value env_overrides].each do |name|
57
62
  define_method(name) { |*args| hide_credentials(super(*args)) }
58
63
  end
@@ -221,6 +226,35 @@ module Profiler
221
226
  "[FILTERED: #{mime} body that could not be filtered, #{raw.bytesize} bytes]"
222
227
  end
223
228
 
229
+ # The part of a body a profile keeps when it is longer than +limit+ bytes, redacted. The body
230
+ # is redacted whole first and the result is cut after, so the part kept is redaction output
231
+ # and never shows a value the filter masks. +size+ is the length of the whole body when
232
+ # known; the body is whole when +raw+ has that length, and only a beginning otherwise
233
+ # (an inflation stopped at the limit, a body_stream read in part).
234
+ #
235
+ # A JSON, NDJSON or form body that cannot be redacted whole (only its beginning is known, or
236
+ # it is over MAX_REDACTABLE_BODY_BYTES) is masked entirely with a label that says it was
237
+ # truncated: a cut JSON text cannot be parsed and a text search would miss keys. Other types
238
+ # are cut first, as filter_body leaves them as they are or masks them whole anyway. A whole
239
+ # body that is not valid JSON keeps the "unparseable" label of filter_body.
240
+ def filter_truncated_body(raw, content_type, limit, size = raw.is_a?(String) ? raw.bytesize : nil)
241
+ return raw unless raw.is_a?(String) && !raw.empty?
242
+ return filter_body(raw, content_type) if limit.nil? || (size == raw.bytesize && raw.bytesize <= limit)
243
+ return cut_bytes(raw, limit) unless enabled?
244
+
245
+ mime = content_type.to_s.split(";").first.to_s.strip.downcase
246
+ return filter_body(cut_bytes(raw, limit), content_type) unless structured_type?(mime)
247
+
248
+ whole = size == raw.bytesize
249
+ return cut_bytes(filter_body(raw, content_type), limit) if whole && raw.bytesize <= MAX_REDACTABLE_BODY_BYTES
250
+
251
+ reason = whole ? "too large to redact" : "only its beginning is known, it cannot be redacted"
252
+ "[FILTERED: truncated #{mime} body, kept #{limit} of #{size || "more than #{limit}"} bytes, #{reason}]"
253
+ rescue StandardError => e
254
+ report(e, "filtering a truncated body")
255
+ "[FILTERED: truncated body that could not be filtered, #{raw.bytesize} bytes]"
256
+ end
257
+
224
258
  # Masks the filtered values of a query string, pair by pair, leaving the
225
259
  # rest of the string exactly as it was.
226
260
  def filter_query(query)
@@ -481,6 +515,10 @@ module Profiler
481
515
  utf8.valid_encoding? ? utf8 : string.b
482
516
  end
483
517
 
518
+ def structured_type?(mime)
519
+ mime.match?(JSON_TYPES) || mime.match?(NDJSON_TYPES) || mime == FORM_TYPE
520
+ end
521
+
484
522
  def filter_json(text, mime)
485
523
  return text unless json_needs_parse?(text)
486
524
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.1"
4
+ VERSION = "0.45.2"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.1
4
+ version: 0.45.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy