rails-profiler 0.43.0 → 0.44.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.
@@ -0,0 +1,363 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+ require_relative "storage/token"
7
+ require_relative "local_request"
8
+ require_relative "redaction"
9
+ require_relative "models/profile"
10
+
11
+ module Profiler
12
+ # Sends a profiled request again, for real, to the application that served it, so that the
13
+ # replay is profiled like any other request and linked to the original (Profile#replay_of).
14
+ #
15
+ # The target is the host name the original request was made to (the Host header, port removed)
16
+ # on the port the application really listens on: config.replay_port when set, otherwise the port
17
+ # of the listening socket captured with the profile (Profile#server_port, from Puma's
18
+ # env["puma.socket"]). Neither the Host header nor SERVER_PORT is ever a source: a client controls
19
+ # both under Puma, and neither config.hosts nor Rails look at the port. With no replay_port and no
20
+ # captured port (another server than Puma, or a profile captured before the port was) the replay
21
+ # is refused with an explicit error. It is read from the stored profile only: no option names a host, a
22
+ # port, a scheme or a URL, the ones that try are refused, and the Host header of an override is
23
+ # ignored. The captured host name must also be a local name or one config.hosts allows (the
24
+ # check authorization_mode :allow_local applies), so a profile that recorded a forged Host
25
+ # header cannot aim a replay elsewhere. Redirects
26
+ # are never followed. The call itself goes through Profiler.untracked_http: it is not
27
+ # recorded in whatever profile is current, and cannot profile itself in a loop.
28
+ #
29
+ # Every method is allowed, side effects included: whoever asks (the dashboard, after its
30
+ # confirmation; an MCP client, which answers for the call it makes) answers for the request.
31
+ module Replay
32
+ class Error < StandardError; end
33
+ class Disabled < Error; end
34
+ class NotFound < Error; end
35
+ class Invalid < Error; end
36
+
37
+ # Carries the source token to the replayed request; the middleware reads it (only while
38
+ # replay_enabled) to set Profile#replay_of.
39
+ SOURCE_HEADER = "X-Profiler-Replay-Of"
40
+ SOURCE_ENV_KEY = "HTTP_X_PROFILER_REPLAY_OF"
41
+ RESPONSE_TOKEN_HEADER = "X-Profiler-Token"
42
+
43
+ METHODS = %w[GET HEAD POST PUT PATCH DELETE OPTIONS].freeze
44
+ BODYLESS_METHODS = %w[GET HEAD DELETE OPTIONS].freeze
45
+ OPTIONS = %i[count method path query params body headers cookies timeout].freeze
46
+ # Options that would aim the request somewhere else: refused, never ignored in silence.
47
+ TARGET_OPTIONS = %w[host hostname port scheme protocol url uri base_url origin target server address
48
+ forwarded_host x_forwarded_host].freeze
49
+ # Headers an override may not set: those that name the target or that the transport owns.
50
+ FORBIDDEN_HEADERS = %w[host forwarded x-forwarded-host x-forwarded-server x-forwarded-proto x-forwarded-port
51
+ x-original-host x-host x-http-host-override content-length connection
52
+ transfer-encoding upgrade te trailer proxy-authorization
53
+ x-profiler-replay-of x-profiler-token].freeze
54
+ # Whitespace and control characters, which have no place in the request line.
55
+ REQUEST_LINE_UNSAFE = /[\x00-\x20\x7f]/
56
+ ROUTE_PARAMS = %w[controller action].freeze
57
+ MAX_TIMEOUT = 120
58
+
59
+ Result = Struct.new(:source_token, :target, :replays, :notes, keyword_init: true) do
60
+ def to_h
61
+ { source_token: source_token, target: target, replays: replays, notes: notes }
62
+ end
63
+ end
64
+
65
+ class << self
66
+ # +options+ (string or symbol keys): count, method, path, query, params, body, headers,
67
+ # cookies, timeout. Raises a Replay::Error subclass for everything the caller can fix.
68
+ def call(source_token, options = {}, storage: Profiler.storage)
69
+ options = normalize_options(options)
70
+ raise Disabled, disabled_message unless Profiler.configuration.replay_enabled
71
+
72
+ source = load_source(source_token, storage)
73
+ count = replay_count(options[:count])
74
+ target = target_for(source)
75
+ request = build_request(source, options)
76
+ timeout = replay_timeout(options[:timeout])
77
+
78
+ replays = []
79
+ Profiler.untracked_http do
80
+ count.times do |index|
81
+ replays << send_once(target, request, source.token, timeout, index + 1)
82
+ break if replays.last[:error]
83
+ end
84
+ end
85
+ Result.new(source_token: source.token, target: target[:origin], replays: replays,
86
+ notes: request[:notes])
87
+ end
88
+
89
+ def disabled_message
90
+ "Replaying requests is disabled. Set config.replay_enabled = true to allow it."
91
+ end
92
+
93
+ private
94
+
95
+ def normalize_options(options)
96
+ options = (options || {}).to_h.transform_keys(&:to_s)
97
+ target_keys = options.keys & TARGET_OPTIONS
98
+ unless target_keys.empty?
99
+ raise Invalid, "A replay always goes to the host of the original request: #{target_keys.join(", ")} " \
100
+ "cannot be set."
101
+ end
102
+ unknown = options.keys - OPTIONS.map(&:to_s)
103
+ raise Invalid, "Unknown replay option(s): #{unknown.join(", ")}." unless unknown.empty?
104
+
105
+ options.transform_keys(&:to_sym)
106
+ end
107
+
108
+ def load_source(token, storage)
109
+ raise NotFound, "Profile not found: #{token.to_s[0, 64]}" unless Storage::Token.valid?(token)
110
+
111
+ profile = storage.load(token)
112
+ raise NotFound, "Profile not found: #{token}" unless profile
113
+ unless profile.profile_type == "http" && profile.method && profile.path
114
+ raise Invalid, "Only a profiled HTTP request can be replayed."
115
+ end
116
+
117
+ profile
118
+ end
119
+
120
+ def replay_count(value)
121
+ count = value.nil? ? 1 : Integer(value, exception: false)
122
+ max = Profiler.configuration.replay_max_count.to_i
123
+ unless count && count >= 1 && count <= max
124
+ raise Invalid, "count must be a whole number from 1 to #{max} (config.replay_max_count)."
125
+ end
126
+
127
+ count
128
+ end
129
+
130
+ def replay_timeout(value)
131
+ default = Profiler.configuration.replay_timeout.to_f
132
+ seconds = value.nil? ? default : Float(value, exception: false)
133
+ raise Invalid, "timeout must be a number of seconds." unless seconds && seconds.positive?
134
+
135
+ [seconds, MAX_TIMEOUT].min
136
+ end
137
+
138
+ # The host name of the original request (Host header) on the port and scheme the application
139
+ # server really listens on: config.replay_port, else the socket port stored in the profile.
140
+ # Neither the Host header nor SERVER_PORT is used: both are whatever the client wrote.
141
+ def target_for(source)
142
+ headers = headers_by_name(source.headers)
143
+ host = headers["host"].to_s.strip
144
+ raise Invalid, "The profile has no Host header, so the original host is unknown." if host.empty?
145
+
146
+ configured = Profiler.configuration.replay_port
147
+ port = configured || source.server_port
148
+ scheme = source.url_scheme
149
+ scheme = "http" if configured && !%w[http https].include?(scheme)
150
+ unless port.is_a?(Integer) && port.between?(1, 65_535) && %w[http https].include?(scheme)
151
+ raise Invalid, "The port the application listens on is unknown: the profile did not capture the " \
152
+ "listening socket (only Puma exposes it) and config.replay_port is not set. Set " \
153
+ "config.replay_port to the port the application server listens on; the replay is " \
154
+ "refused rather than using the Host header or SERVER_PORT, which a client controls."
155
+ end
156
+
157
+ uri = parse_host(scheme, host)
158
+ unless replay_host_allowed?(uri.hostname)
159
+ raise Invalid, "The host #{uri.hostname.inspect} of the original request is neither a loopback name " \
160
+ "nor listed literally in config.hosts: refusing to send a request to it."
161
+ end
162
+
163
+ hostname = uri.hostname
164
+ authority = hostname.include?(":") ? "[#{hostname}]:#{port}" : "#{hostname}:#{port}"
165
+ { scheme: scheme, host: hostname, port: port, origin: "#{scheme}://#{authority}" }
166
+ end
167
+
168
+ # A host is replayed to only when it is a loopback name (Configuration::LOCAL_HTTP_HOSTS) or a
169
+ # literal String entry of config.hosts. Rails' own check is not used: it decides whether an
170
+ # incoming request is accepted, and in development config.hosts holds 0.0.0.0/0 and ::/0, which
171
+ # let any IP through (Host: 169.254.169.254). A wildcard (".example.com"), an IPAddr range, a
172
+ # Regexp or a Proc proves nothing about this one address, so a match through one is refused.
173
+ def replay_host_allowed?(hostname)
174
+ name = hostname.to_s.downcase.chomp(".")
175
+ return false if name.empty?
176
+ return true if Configuration::LOCAL_HTTP_HOSTS.any? { |pattern| pattern.match?(name) }
177
+
178
+ literal_hosts.include?(name)
179
+ end
180
+
181
+ def literal_hosts
182
+ entries = Rails.application.config.hosts if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
183
+ Array(entries).filter_map do |entry|
184
+ next unless entry.is_a?(String) && !entry.empty? && !entry.start_with?(".") && !entry.include?("*")
185
+
186
+ URI.parse("http://#{entry}").hostname&.downcase&.chomp(".")
187
+ rescue URI::InvalidURIError
188
+ nil
189
+ end
190
+ end
191
+
192
+ def parse_host(scheme, host)
193
+ uri = URI.parse("#{scheme}://#{host}")
194
+ valid = uri.is_a?(URI::HTTP) && uri.hostname && uri.userinfo.nil? &&
195
+ (uri.path.nil? || uri.path.empty?) && uri.query.nil? && uri.fragment.nil?
196
+ raise Invalid, "The Host header #{host.inspect} of the original request is not a host name." unless valid
197
+
198
+ uri
199
+ rescue URI::InvalidURIError
200
+ raise Invalid, "The Host header #{host.inspect} of the original request is not a host name."
201
+ end
202
+
203
+ def headers_by_name(headers)
204
+ (headers || {}).each_with_object({}) { |(name, value), out| out[name.to_s.downcase] = value }
205
+ end
206
+
207
+ def build_request(source, options)
208
+ notes = []
209
+ method = (options[:method] || source.method).to_s.upcase
210
+ raise Invalid, "Unsupported HTTP method #{method.inspect} (allowed: #{METHODS.join(", ")})." unless METHODS.include?(method)
211
+
212
+ path, path_query = split_path(options[:path] || source.path)
213
+ headers = base_headers(source, options, notes)
214
+ body, query = body_and_query(source, options, method, path_query, headers, notes)
215
+
216
+ { method: method, path: query.to_s.empty? ? path : "#{path}?#{query}", headers: headers, body: body,
217
+ notes: notes }
218
+ end
219
+
220
+ def split_path(value)
221
+ value = value.to_s
222
+ path, query = value.split("?", 2)
223
+ unless path.match?(%r{\A/(?!/)[^\s\\\x00-\x1f\x7f]*\z})
224
+ raise Invalid, "path must be an absolute path of the application (starting with a single \"/\")."
225
+ end
226
+ raise Invalid, "the query string of path must not contain whitespace or control characters." if query&.match?(REQUEST_LINE_UNSAFE)
227
+
228
+ [path, query]
229
+ end
230
+
231
+ def base_headers(source, options, notes)
232
+ headers = {}
233
+ redacted = []
234
+ (source.headers || {}).each do |name, value|
235
+ next if FORBIDDEN_HEADERS.include?(name.to_s.downcase)
236
+
237
+ if value == Redaction::MASK
238
+ redacted << name
239
+ next
240
+ end
241
+ headers[name.to_s] = value.to_s
242
+ end
243
+ notes << "Not sent, masked in the profile: #{redacted.join(", ")}." unless redacted.empty?
244
+
245
+ ignored = []
246
+ (options[:headers] || {}).each do |name, value|
247
+ if FORBIDDEN_HEADERS.include?(name.to_s.downcase) || name.to_s.match?(/[\r\n:\s]/) || value.to_s.match?(/[\r\n]/)
248
+ ignored << name.to_s
249
+ next
250
+ end
251
+ headers.delete_if { |existing, _| existing.casecmp?(name.to_s) }
252
+ headers[name.to_s] = value.to_s
253
+ end
254
+ notes << "Ignored headers (they name the target or belong to the transport): #{ignored.join(", ")}." unless ignored.empty?
255
+
256
+ apply_cookies(headers, options[:cookies])
257
+ headers
258
+ end
259
+
260
+ def apply_cookies(headers, cookies)
261
+ return if cookies.nil?
262
+
263
+ value = cookies.respond_to?(:map) && !cookies.is_a?(String) ? cookies.map { |k, v| "#{k}=#{v}" }.join("; ") : cookies.to_s
264
+ raise Invalid, "cookies must not contain line breaks." if value.match?(/[\r\n]/)
265
+
266
+ headers.delete_if { |name, _| name.casecmp?("Cookie") }
267
+ headers["Cookie"] = value unless value.empty?
268
+ end
269
+
270
+ def body_and_query(source, options, method, path_query, headers, notes)
271
+ params = options.key?(:params) ? options[:params] : default_params(source, notes)
272
+ query = options.key?(:query) ? encode_query(options[:query]) : path_query
273
+ body = nil
274
+
275
+ if options.key?(:body)
276
+ body = options[:body].to_s
277
+ elsif !options.key?(:params) && !BODYLESS_METHODS.include?(method) && (stored = stored_body(source, notes))
278
+ body = stored
279
+ end
280
+
281
+ if params.is_a?(Hash) && !params.empty? && !options.key?(:query) && path_query.nil? && body.nil?
282
+ if BODYLESS_METHODS.include?(method)
283
+ query = encode_query(params)
284
+ else
285
+ body, content_type = encode_params_body(params, headers)
286
+ headers["Content-Type"] ||= content_type
287
+ end
288
+ end
289
+
290
+ [body, query]
291
+ end
292
+
293
+ def default_params(source, notes)
294
+ return {} unless source.params.is_a?(Hash)
295
+
296
+ route = source.collector_data("request").to_h
297
+ params = source.params.reject { |key, _| route["controller_action"] && ROUTE_PARAMS.include?(key.to_s) }
298
+ masked = params.select { |_, value| value == Redaction::MASK }.keys
299
+ notes << "Parameters masked in the profile were not sent: #{masked.join(", ")}." unless masked.empty?
300
+ params.reject { |_, value| value == Redaction::MASK }
301
+ end
302
+
303
+ def stored_body(source, notes)
304
+ return nil if source.request_body.nil? || source.request_body.empty?
305
+
306
+ if source.request_body_truncated
307
+ notes << "The stored request body is truncated (max_captured_body_bytes): the replay sends what was kept."
308
+ end
309
+ if source.request_body_encoding == "base64"
310
+ Base64.strict_decode64(source.request_body)
311
+ else
312
+ Models::Profile.decode_body(source.request_body, source.request_body_encoding).first
313
+ end
314
+ rescue ArgumentError
315
+ nil
316
+ end
317
+
318
+ def encode_query(value)
319
+ case value
320
+ when nil then nil
321
+ when Hash then URI.encode_www_form(flatten_params(value))
322
+ else value.to_s.gsub(REQUEST_LINE_UNSAFE, "")
323
+ end
324
+ end
325
+
326
+ def encode_params_body(params, headers)
327
+ content_type = headers.find { |name, _| name.casecmp?("Content-Type") }&.last.to_s
328
+ if content_type.include?("json")
329
+ [JSON.generate(params), content_type]
330
+ else
331
+ [URI.encode_www_form(flatten_params(params)), "application/x-www-form-urlencoded"]
332
+ end
333
+ end
334
+
335
+ def flatten_params(value, prefix = nil)
336
+ case value
337
+ when Hash
338
+ value.flat_map { |k, v| flatten_params(v, prefix ? "#{prefix}[#{k}]" : k.to_s) }
339
+ when Array
340
+ value.flat_map { |v| flatten_params(v, "#{prefix}[]") }
341
+ else
342
+ [[prefix.to_s, value.to_s]]
343
+ end
344
+ end
345
+
346
+ def send_once(target, request, source_token, timeout, index)
347
+ http = Net::HTTP.new(target[:host], target[:port])
348
+ http.use_ssl = target[:scheme] == "https"
349
+ http.open_timeout = http.read_timeout = http.write_timeout = timeout
350
+ message = Net::HTTPGenericRequest.new(request[:method], !request[:body].nil?, request[:method] != "HEAD",
351
+ request[:path], request[:headers].merge(SOURCE_HEADER => source_token))
352
+ message.body = request[:body] if request[:body]
353
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
354
+ response = http.start { |connection| connection.request(message) }
355
+ token = response[RESPONSE_TOKEN_HEADER]
356
+ { index: index, status: response.code.to_i, token: Storage::Token.valid?(token) ? token : nil,
357
+ duration_ms: ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round(2) }
358
+ rescue StandardError => e
359
+ { index: index, status: nil, token: nil, error: "#{e.class}: #{e.message}" }
360
+ end
361
+ end
362
+ end
363
+ end
@@ -50,6 +50,26 @@ module Profiler
50
50
  raise NotImplementedError, "#{self.class} must implement #find_by_trace"
51
51
  end
52
52
 
53
+ # The profiles that replay the profile +source_token+ (Profile#replay_of), oldest first.
54
+ # Unlike parent_token, which links an AJAX call to its page and is indexed by each store,
55
+ # replay_of is carried by the summary every store keeps for its lists, so one scan of the
56
+ # summaries serves the four backends. Replays are rare and the scan is bounded by
57
+ # max_profiles.
58
+ def find_by_replay_of(source_token, page_size: 200)
59
+ return [] unless Token.valid?(source_token)
60
+
61
+ found = []
62
+ offset = 0
63
+ loop do
64
+ page = list(limit: page_size, offset: offset, summary: true)
65
+ found.concat(page.select { |profile| profile.replay_of == source_token })
66
+ break if page.size < page_size
67
+
68
+ offset += page_size
69
+ end
70
+ found.sort_by { |profile| profile.started_at || Time.at(0) }
71
+ end
72
+
53
73
  def delete(token)
54
74
  raise NotImplementedError, "#{self.class} must implement #delete"
55
75
  end
@@ -332,6 +332,7 @@ module Profiler
332
332
  end
333
333
 
334
334
  req_collector = collectors_data["request"] || {}
335
+ summary = parse_json(row["summary"], {})
335
336
 
336
337
  Models::Profile.from_hash(
337
338
  token: row["token"],
@@ -348,6 +349,11 @@ module Profiler
348
349
  trace_id: row["trace_id"],
349
350
  span_id: row["span_id"],
350
351
  parent_span_id: row["parent_span_id"],
352
+ # Not columns: the summary column holds the whole to_h, replay_of and the listening
353
+ # endpoint included.
354
+ replay_of: summary["replay_of"],
355
+ server_port: summary["server_port"],
356
+ url_scheme: summary["url_scheme"],
351
357
  is_ajax: row["is_ajax"] == 1,
352
358
  tabs: parse_json(row["tabs"], []),
353
359
  params: parse_json(row["params"], {}),
@@ -49,7 +49,7 @@ module Profiler
49
49
  token
50
50
  end
51
51
 
52
- %i[load list cleanup exists? find_by_parent delete clear].each do |name|
52
+ %i[load list cleanup exists? find_by_parent find_by_replay_of delete clear].each do |name|
53
53
  define_method(name) { |*, **| raise @error }
54
54
  end
55
55
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.43.0"
4
+ VERSION = "0.44.0"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.43.0
4
+ version: 0.44.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-20 00:00:00.000000000 Z
11
+ date: 2026-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -129,6 +129,7 @@ files:
129
129
  - app/controllers/profiler/api/jobs_controller.rb
130
130
  - app/controllers/profiler/api/outbound_http_controller.rb
131
131
  - app/controllers/profiler/api/profiles_controller.rb
132
+ - app/controllers/profiler/api/replays_controller.rb
132
133
  - app/controllers/profiler/api/slave_proxy_controller.rb
133
134
  - app/controllers/profiler/api/test_runner_controller.rb
134
135
  - app/controllers/profiler/api/tests_controller.rb
@@ -232,6 +233,7 @@ files:
232
233
  - lib/profiler/mcp/tools/query_mailers.rb
233
234
  - lib/profiler/mcp/tools/query_profiles.rb
234
235
  - lib/profiler/mcp/tools/query_test_profiles.rb
236
+ - lib/profiler/mcp/tools/replay_request.rb
235
237
  - lib/profiler/mcp/tools/reset_all_env_vars.rb
236
238
  - lib/profiler/mcp/tools/reset_env_var.rb
237
239
  - lib/profiler/mcp/tools/run_tests.rb
@@ -255,6 +257,7 @@ files:
255
257
  - lib/profiler/profile_diff.rb
256
258
  - lib/profiler/railtie.rb
257
259
  - lib/profiler/redaction.rb
260
+ - lib/profiler/replay.rb
258
261
  - lib/profiler/sql_statement.rb
259
262
  - lib/profiler/sse/bus.rb
260
263
  - lib/profiler/sse/event_bus.rb