rails-profiler 0.45.2 → 0.45.4

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: 5e77f6e8c5a2b0148fc9288da73e9c9f464e57e3fadad34df3c5e96b7d8af2de
4
- data.tar.gz: 6877c02a32028e0c501bf0ac4e0249b91d97c7865b94ccbba9fe497ebfa3eebd
3
+ metadata.gz: 9b4332eee9a830876c3ffe5c137d94543461a2ad84f05e9c32943c8aac2ed8a1
4
+ data.tar.gz: b004d3659cf24ddddae10cbdc416922ccf827ece582b1681417b456c1037d96c
5
5
  SHA512:
6
- metadata.gz: b2e27e15a4774ffbf717c7f76e8f6ff8f46001e452e59adfcfdfb9219ec3fd9611e19790ba061c846c964f71a1c1787e3486b3fcdc3e60fe89bbe68244289c9d
7
- data.tar.gz: a9c1be674583b25ba7a2a6d901b36e76ac188c19be8f219c2a607fe280bb37b77022c3ca91a2b69f5c58508582267613f9e72a8eb004dd6eb645f5dcddc9265b
6
+ metadata.gz: ea60c6cfdac1a5be97ea9c4b7f7e12b6968ada0b0a1e3d7df5edead621cd2b43fa567b69b217351ac46df0af149c772a98591e4b24f84586d9c677f748422ab7
7
+ data.tar.gz: 995ee37e14b80fca5949f3a49c2ee11fedf0a08e992b0c37435b83c6fea24c4e6193b3971a93df125288f681897e630f5e7ae06a940f4413c9592d9e4825a6fa
data/CHANGELOG.md CHANGED
@@ -19,6 +19,22 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.45.4] - 2026-10-27
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - A JSON, NDJSON or form request or response body longer than `max_captured_body_bytes` is no longer masked as `[FILTERED: unparseable ...]` in a profile captured by the Rack middleware. The body is redacted whole, then cut, as Net::HTTP bodies already are.
29
+
30
+ ## [0.45.3] - 2026-10-27
31
+
32
+ <!-- stamped -->
33
+
34
+ ### Fixed
35
+
36
+ - **Outbound HTTP:** The body of a request sent through a `body_stream` that cannot be rewound (RestClient's payload, a pipe) is now captured, redacted and cut at `max_captured_body_bytes` like any other, instead of showing "Request body not captured". The profiler copies the bytes Net::HTTP reads and leaves the stream and what the server receives untouched.
37
+
22
38
  ## [0.45.2] - 2026-10-27
23
39
 
24
40
  <!-- stamped -->
@@ -91,6 +91,12 @@ module Profiler
91
91
  save_if_collected
92
92
  end
93
93
 
94
+ # Called from NetHttpInstrumentation to add what is only known once the request is sent.
95
+ def update_request(entry, **data)
96
+ @mutex.synchronize { entry.merge!(data) }
97
+ save_if_collected
98
+ end
99
+
94
100
  # Called from NetHttpInstrumentation when the HTTP call raises.
95
101
  def fail_request(entry, error:, duration:)
96
102
  @mutex.synchronize { entry.merge!(in_flight: false, status: 0, duration: duration, error: error) }
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Instrumentation
5
+ # Only the methods the original has are defined: IO.copy_stream picks readpartial over
6
+ # read when it finds it, and Net::HTTP rewinds a stream only if it can.
7
+ module Partial
8
+ def readpartial(*args)
9
+ result = @original.readpartial(*args)
10
+ record(result, false)
11
+ result
12
+ rescue EOFError
13
+ @finished = true
14
+ raise
15
+ end
16
+
17
+ def read_nonblock(*args, **opts)
18
+ result = @original.read_nonblock(*args, **opts)
19
+ record(result, false) if result.is_a?(String)
20
+ result
21
+ rescue EOFError
22
+ @finished = true
23
+ raise
24
+ end
25
+ end
26
+
27
+ module Rewindable
28
+ # Net::HTTP rewinds a stream before it retries an idempotent request: what was copied
29
+ # belongs to the attempt that failed.
30
+ def rewind(*args)
31
+ result = @original.rewind(*args)
32
+ @buffer.clear
33
+ @total_read = 0
34
+ @finished = false
35
+ result
36
+ end
37
+ end
38
+
39
+ # Stands in for a request body_stream that cannot be rewound (a pipe, a socket, a payload
40
+ # object such as RestClient's) while Net::HTTP sends it. It hands every read to the original
41
+ # stream untouched and copies what came out, up to one byte past +limit+, into a buffer; nothing is
42
+ # read ahead, so the server gets exactly the bytes it would have got.
43
+ #
44
+ # to_io and to_path are deliberately not forwarded: IO.copy_stream would use them to go around
45
+ # this object and read the original directly.
46
+ class CapturingStream
47
+ attr_reader :original, :total_read
48
+
49
+ def initialize(original, limit)
50
+ @original = original
51
+ @limit = limit
52
+ @buffer = String.new(encoding: Encoding::BINARY)
53
+ @total_read = 0
54
+ @finished = false
55
+ extend Partial if original.respond_to?(:readpartial)
56
+ extend Rewindable if original.respond_to?(:rewind)
57
+ end
58
+
59
+ # The bytes seen so far, at most one past +limit+ (so that the caller can tell the body was
60
+ # cut, and cut it with Redaction.cut_bytes, which holds back what could start a credential).
61
+ def captured
62
+ @buffer.dup
63
+ end
64
+
65
+ # True once a read has reported the end of the stream.
66
+ def finished?
67
+ @finished
68
+ end
69
+
70
+ def read(*args)
71
+ result = @original.read(*args)
72
+ record(result, args.first.nil?)
73
+ result
74
+ end
75
+
76
+ def respond_to_missing?(name, include_private = false)
77
+ return false if %i[to_io to_path].include?(name)
78
+
79
+ @original.respond_to?(name, include_private) || super
80
+ end
81
+
82
+ def method_missing(name, *args, **opts, &block)
83
+ if %i[to_io to_path].include?(name) || !@original.respond_to?(name)
84
+ return super
85
+ end
86
+
87
+ opts.empty? ? @original.public_send(name, *args, &block) : @original.public_send(name, *args, **opts, &block)
88
+ end
89
+
90
+ private
91
+
92
+ # +whole+: a read without a length, which returns the rest and so ends the stream.
93
+ def record(chunk, whole)
94
+ @finished = true if chunk.nil? || whole
95
+ return if chunk.nil? || chunk.empty?
96
+
97
+ @total_read += chunk.bytesize
98
+ room = @limit ? @limit + 1 - @buffer.bytesize : chunk.bytesize
99
+ @buffer << chunk.byteslice(0, room).b if room.positive?
100
+ end
101
+ end
102
+ end
103
+ end
@@ -10,6 +10,7 @@ require_relative "../body_content"
10
10
  require_relative "../configuration"
11
11
  require_relative "../current_context"
12
12
  require_relative "../trace_context"
13
+ require_relative "capturing_stream"
13
14
 
14
15
  module Profiler
15
16
  module Instrumentation
@@ -34,6 +35,7 @@ module Profiler
34
35
  url = Redaction.filter_url(build_url(host, port, req.path, use_ssl?))
35
36
  limit = Profiler.configuration.max_captured_body_bytes
36
37
  captured = NetHttpInstrumentation.capture_request_body(req, body, limit)
38
+ tee = captured[:tee]
37
39
  req_headers = Redaction.filter_headers(req.to_hash.transform_values { |v| v.join(", ") })
38
40
  req_content_type = req["content-type"].to_s
39
41
  req_body = captured[:content]
@@ -118,6 +120,7 @@ module Profiler
118
120
  raise
119
121
  ensure
120
122
  Thread.current[:profiler_http_recording] = false
123
+ NetHttpInstrumentation.finish_stream_capture(req, tee, entry, collector) if defined?(tee) && tee
121
124
  end
122
125
 
123
126
  private
@@ -222,7 +225,9 @@ module Profiler
222
225
  declared = req["content-length"].to_s
223
226
  declared = declared.match?(/\A\d+\z/) ? declared.to_i : nil
224
227
  unless rewindable?(stream)
225
- return { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true }
228
+ # Read as Net::HTTP sends it, by a stand-in that copies what passes (see finish_stream_capture).
229
+ tee = tee_stream(req, stream, limit)
230
+ return { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true, tee: tee }
226
231
  end
227
232
 
228
233
  position = stream.pos
@@ -246,6 +251,48 @@ module Profiler
246
251
  { content: nil, size: declared, size_is_minimum: false, truncated: false, not_captured: true }
247
252
  end
248
253
 
254
+ # Puts a CapturingStream in place of +stream+ on the request; nil (the request untouched) when
255
+ # the stream cannot be read or the stand-in cannot be installed.
256
+ def self.tee_stream(req, stream, limit)
257
+ return nil unless stream.respond_to?(:read)
258
+
259
+ # A JSON, NDJSON or form body is redacted whole before it is cut, so more of it is copied.
260
+ mime = req["content-type"].to_s.split(";").first.to_s.strip.downcase
261
+ copy = limit && mime.match?(%r{json|x-www-form-urlencoded}) ? Redaction::MAX_REDACTABLE_BODY_BYTES : limit
262
+ tee = CapturingStream.new(stream, copy)
263
+ req.body_stream = tee
264
+ tee
265
+ rescue StandardError => e
266
+ Profiler.log_error_once(:net_http_body_stream_tee, "NetHttpInstrumentation: could not capture a request body_stream", e)
267
+ nil
268
+ end
269
+
270
+ # Once the request has been sent (or has failed): the original stream goes back on the
271
+ # request, and the entry gets the body that passed, redacted like any other. An entry whose
272
+ # stream was never read stays "not captured".
273
+ def self.finish_stream_capture(req, tee, entry, collector)
274
+ req.body_stream = tee.original if req.body_stream.equal?(tee)
275
+ return unless entry && tee.total_read.positive?
276
+
277
+ limit = Profiler.configuration.max_captured_body_bytes
278
+ declared = req["content-length"].to_s
279
+ declared = declared.match?(/\A\d+\z/) ? declared.to_i : nil
280
+ raw = tee.captured
281
+ size = declared || tee.total_read
282
+ cut = limit && raw.bytesize > limit
283
+ processed = process_body(raw.force_encoding(Encoding::UTF_8), req["content-type"].to_s,
284
+ limit: (limit if cut), size: size)
285
+ kept = cut ? limit : raw.bytesize
286
+ collector.update_request(
287
+ entry,
288
+ request_body: processed[:body], request_body_encoding: processed[:encoding],
289
+ request_size: size, request_size_is_minimum: declared.nil? && !tee.finished?,
290
+ request_body_truncated: kept < size, request_body_not_captured: false
291
+ )
292
+ rescue StandardError => e
293
+ Profiler.log_error_once(:net_http_body_stream_tee, "NetHttpInstrumentation: could not record a request body_stream", e)
294
+ end
295
+
249
296
  # What is left to read of a stream that knows its size (a StringIO, a file), else nil.
250
297
  def self.remaining_size(stream, position)
251
298
  size = stream.respond_to?(:size) ? stream.size : nil
@@ -98,14 +98,13 @@ module Profiler
98
98
  chunk = chunk.to_s
99
99
  @encoding ||= chunk.encoding
100
100
  @size += chunk.bytesize
101
- # One byte past the limit, so that Redaction.cut_bytes knows the body goes on.
101
+ # One byte past the limit, so that the profile knows the body goes on.
102
102
  room = @limit ? @limit + 1 - @captured.bytesize : chunk.bytesize
103
103
  @captured << chunk.byteslice(0, room).b if room.positive?
104
104
  end
105
105
 
106
106
  def captured
107
- text = @captured.dup.force_encoding(@encoding || Encoding::UTF_8)
108
- @limit ? Redaction.cut_bytes(text, @limit) : text
107
+ @captured.dup.force_encoding(@encoding || Encoding::UTF_8)
109
108
  end
110
109
  end
111
110
  end
@@ -194,7 +194,7 @@ module Profiler
194
194
  finish_streamed(env, streamed, collectors, request_body, allocations_before, status, profiled_headers,
195
195
  captured, size, complete, error)
196
196
  end
197
- limit = Profiler.configuration.max_captured_body_bytes
197
+ limit = Redaction.capture_limit(Profiler.configuration.max_captured_body_bytes, content_type(headers))
198
198
  streamed.body = CapturingBody.new(body, limit: limit, context: streamed) do |captured, size, complete, error|
199
199
  streamed.finish(captured, size, complete, error)
200
200
  end
@@ -248,9 +248,6 @@ module Profiler
248
248
 
249
249
  # +complete+: whether +size+ is the whole body's, or only what was seen before it stopped.
250
250
  def record_response(env, profile, request_body, status, headers, content, size, complete)
251
- limit = Profiler.configuration.max_captured_body_bytes
252
- content = Redaction.cut_bytes(content, limit) if limit
253
-
254
251
  profile.finish(status, headers)
255
252
  profile.set_bodies(
256
253
  request_body: request_body[:content],
@@ -475,7 +472,7 @@ module Profiler
475
472
  input = env["rack.input"]
476
473
  return { content: "", size: nil } unless input.respond_to?(:rewind)
477
474
 
478
- limit = Profiler.configuration.max_captured_body_bytes
475
+ limit = Redaction.capture_limit(Profiler.configuration.max_captured_body_bytes, env["CONTENT_TYPE"])
479
476
  begin
480
477
  input.rewind
481
478
  content = (limit ? input.read(limit + 1) : input.read).to_s
@@ -488,7 +485,6 @@ module Profiler
488
485
  # Without a Content-Length, the size of a cut body is only known to exceed the limit.
489
486
  size = known ? declared.to_i : content.bytesize
490
487
  size_is_minimum = !known && limit && content.bytesize > limit
491
- content = Redaction.cut_bytes(content, limit) if limit
492
488
  { content: content, size: size, size_is_minimum: size_is_minimum || false }
493
489
  rescue => e
494
490
  Profiler.log_error("ProfilerMiddleware: could not read the request body", e)
@@ -58,14 +58,15 @@ module Profiler
58
58
  def set_bodies(request_body:, response_body:, req_content_type:, resp_content_type:,
59
59
  request_body_size: nil, response_body_size: nil,
60
60
  request_body_size_is_minimum: false, response_body_size_is_minimum: false)
61
- req = process_body(request_body, req_content_type)
62
- resp = process_body(response_body, resp_content_type)
61
+ limit = Profiler.configuration.max_captured_body_bytes
62
+ req = process_body(request_body, req_content_type, limit, body_whole_size(request_body, request_body_size, request_body_size_is_minimum, limit))
63
+ resp = process_body(response_body, resp_content_type, limit, body_whole_size(response_body, response_body_size, response_body_size_is_minimum, limit))
63
64
  @request_body = req[:body]
64
65
  @request_body_encoding = req[:encoding]
65
66
  @response_body = resp[:body]
66
67
  @response_body_encoding = resp[:encoding]
67
- @request_body_size, @request_body_truncated = body_size(request_body, request_body_size)
68
- @response_body_size, @response_body_truncated = body_size(response_body, response_body_size)
68
+ @request_body_size, @request_body_truncated = body_size(request_body, request_body_size, limit)
69
+ @response_body_size, @response_body_truncated = body_size(response_body, response_body_size, limit)
69
70
  @request_body_size_is_minimum = request_body_size_is_minimum ? true : false
70
71
  @response_body_size_is_minimum = response_body_size_is_minimum ? true : false
71
72
  end
@@ -268,15 +269,22 @@ module Profiler
268
269
 
269
270
  private
270
271
 
271
- def process_body(raw, content_type)
272
+ # +limit+ is max_captured_body_bytes: a body longer than it is redacted whole and the result
273
+ # cut (Redaction.filter_truncated_body), so a cut JSON text is never what the filter sees.
274
+ # +size+ is the length of the whole body, to tell a body held whole from a beginning.
275
+ def process_body(raw, content_type, limit = nil, size = nil)
272
276
  return { body: nil, encoding: "text" } if raw.nil? || raw.empty?
273
277
 
274
278
  if BodyContent.binary?(raw, content_type)
275
279
  # Masked on the raw bytes, before the encoding hides them from any later search. A
276
- # compressed format (zip, png, gzip...) does not hold the secret as it is.
277
- { body: Base64.strict_encode64(Redaction.hide_credentials(raw.b)), encoding: "base64" }
280
+ # compressed format (zip, png, gzip...) does not hold the secret as it is, and a
281
+ # credential cut in two would escape the masking, hence the cut after it.
282
+ masked = Redaction.hide_credentials(raw.b)
283
+ masked = Redaction.cut_bytes(masked, limit) if limit
284
+ { body: Base64.strict_encode64(masked), encoding: "base64" }
278
285
  else
279
- text = utf8_text(Redaction.filter_body(raw, content_type), content_type)
286
+ filtered = limit ? Redaction.filter_truncated_body(raw, content_type, limit, size) : Redaction.filter_body(raw, content_type)
287
+ text = utf8_text(filtered, content_type)
280
288
  if compress_body?(text)
281
289
  # The fastest level: paid on the request, a quarter larger than the default level.
282
290
  { body: Base64.strict_encode64(Zlib::Deflate.deflate(text, Zlib::BEST_SPEED)), encoding: "gzip+base64" }
@@ -319,12 +327,22 @@ module Profiler
319
327
  end
320
328
  end
321
329
 
322
- def body_size(captured, total)
330
+ def body_size(captured, total, limit = nil)
323
331
  captured_size = captured.to_s.bytesize
324
- total ||= captured_size
332
+ captured_size = limit if limit && captured_size > limit
333
+ total ||= captured.to_s.bytesize
325
334
  [total, total > captured_size]
326
335
  end
327
336
 
337
+ # The length of the whole body as far as it is known; one more than what was seen when
338
+ # the total is only a minimum and the body is longer than the limit, so that it counts as a beginning.
339
+ def body_whole_size(captured, total, minimum, limit)
340
+ seen = captured.to_s.bytesize
341
+ return seen + 1 if minimum && limit && seen > limit
342
+
343
+ total || seen
344
+ end
345
+
328
346
  def compress_body?(text)
329
347
  Profiler.configuration.compress_bodies &&
330
348
  text.bytesize > Profiler.configuration.compress_body_threshold
@@ -255,6 +255,16 @@ module Profiler
255
255
  "[FILTERED: truncated body that could not be filtered, #{raw.bytesize} bytes]"
256
256
  end
257
257
 
258
+ # How many bytes of a body to copy so that it can be redacted whole before it is cut: a JSON,
259
+ # NDJSON or form body is copied up to MAX_REDACTABLE_BODY_BYTES, any other type (and a nil
260
+ # limit) up to +limit+.
261
+ def capture_limit(limit, content_type)
262
+ return limit if limit.nil? || limit >= MAX_REDACTABLE_BODY_BYTES
263
+
264
+ mime = content_type.to_s.split(";").first.to_s.strip.downcase
265
+ structured_type?(mime) ? MAX_REDACTABLE_BODY_BYTES : limit
266
+ end
267
+
258
268
  # Masks the filtered values of a query string, pair by pair, leaving the
259
269
  # rest of the string exactly as it was.
260
270
  def filter_query(query)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.45.2"
4
+ VERSION = "0.45.4"
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.2
4
+ version: 0.45.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -194,6 +194,7 @@ files:
194
194
  - lib/profiler/env_override_store.rb
195
195
  - lib/profiler/explain_runner.rb
196
196
  - lib/profiler/instrumentation/active_job_instrumentation.rb
197
+ - lib/profiler/instrumentation/capturing_stream.rb
197
198
  - lib/profiler/instrumentation/executor_context_propagation.rb
198
199
  - lib/profiler/instrumentation/irb_instrumentation.rb
199
200
  - lib/profiler/instrumentation/net_http_instrumentation.rb