rails-profiler 0.44.21 → 0.44.24

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.
@@ -20,7 +20,14 @@ module Profiler
20
20
  # and no cluster_secret, the behaviour of 0.30.6.
21
21
  def secret_required?
22
22
  config = Profiler.configuration
23
- config.cluster_require_secret || configured_secret?
23
+ config.cluster_require_secret || configured_secret? || padded_secret?
24
+ end
25
+
26
+ # A secret that was meant to be set but is padded (a trailing newline from a file) stays
27
+ # required: dropping it would turn a configured secret into no authentication at all.
28
+ def padded_secret?
29
+ secret = Profiler.configuration.cluster_secret.to_s
30
+ !secret.strip.empty? && secret != secret.strip
24
31
  end
25
32
 
26
33
  def configured_secret?
@@ -31,6 +38,7 @@ module Profiler
31
38
  def secret_problem
32
39
  secret = Profiler.configuration.cluster_secret.to_s
33
40
  return "No config.cluster_secret is configured" if secret.strip.empty?
41
+ return "config.cluster_secret is surrounded by spaces or newlines, which breaks HTTP requests" if secret != secret.strip
34
42
  return nil if secret.strip.length >= MIN_SECRET_LENGTH
35
43
 
36
44
  "config.cluster_secret is shorter than #{MIN_SECRET_LENGTH} characters, so it is ignored " \
@@ -21,7 +21,8 @@ module Profiler
21
21
  # and which certificates it trusts, or the profiler's own read-only switch.
22
22
  PROTECTED_KEYS = %w[
23
23
  RAILS_ENV RACK_ENV DATABASE_URL SECRET_KEY_BASE RAILS_MASTER_KEY PROFILER_TEST_RUNNER_CHILD
24
- PROFILER_MCP_READ_ONLY SSL_CERT_FILE SSL_CERT_DIR
24
+ PROFILER_MCP_READ_ONLY SSL_CERT_FILE SSL_CERT_DIR LOCALDOMAIN RES_OPTIONS HOSTALIASES
25
+ NODE_EXTRA_CA_CERTS TMPDIR
25
26
  ].freeze
26
27
  # PG: libpq (PGHOST, PGPASSWORD, PGSERVICEFILE...). MYSQL_: the MySQL client (MYSQL_PWD,
27
28
  # MYSQL_HOST...). OPENSSL_: OPENSSL_CONF and OPENSSL_MODULES can load a shared library.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "mcp"
4
+ require "net/http"
4
5
  require_relative "http_guard"
5
6
 
6
7
  module Profiler
@@ -467,7 +468,7 @@ module Profiler
467
468
  ::MCP::Tool.define(name: name, description: description, input_schema: input_schema) do |server_context: nil, **args|
468
469
  result = handler.call(args.transform_keys(&:to_s))
469
470
  result.is_a?(::MCP::Tool::Response) ? result : ::MCP::Tool::Response.new(result)
470
- rescue Profiler::Storage::Unavailable::Error, Profiler::Error => e
471
+ rescue Profiler::Storage::Unavailable::Error, Profiler::Error, SystemCallError, Net::OpenTimeout, Net::ReadTimeout => e
471
472
  ::MCP::Tool::Response.new([{ type: "text", text: e.message }], error: true)
472
473
  end
473
474
  end
@@ -400,9 +400,10 @@ module Profiler
400
400
  if http_data["requests"] && !http_data["requests"].empty?
401
401
  lines << "### Request List"
402
402
  http_data["requests"].each_with_index do |req, index|
403
- flag = req["duration"] >= threshold ? " [SLOW]" : ""
404
- err = req["status"] >= 400 || req["status"] == 0 ? " [ERROR]" : ""
405
- lines << "\n**#{index + 1}. #{req['method']} #{req['url']}** - #{req['status'] == 0 ? 'error' : req['status']} - #{req['duration'].round(2)} ms#{flag}#{err}"
403
+ flag = (req["duration"] || 0).to_f >= threshold ? " [SLOW]" : ""
404
+ status = req["status"].to_i
405
+ err = status >= 400 || status == 0 ? " [ERROR]" : ""
406
+ lines << "\n**#{index + 1}. #{req['method']} #{req['url']}** - #{status == 0 ? 'error' : status} - #{(req['duration'] || 0).to_f.round(2)} ms#{flag}#{err}"
406
407
 
407
408
  if req["request_body"] && !req["request_body"].empty?
408
409
  lines << "**Request Body:**"
@@ -76,15 +76,16 @@ module Profiler
76
76
  if requests && !requests.empty?
77
77
  lines << "## Request Details"
78
78
  requests.each_with_index do |req, i|
79
- slow_flag = req["duration"] >= threshold ? " [SLOW]" : ""
80
- err_flag = req["status"] >= 400 || req["status"] == 0 ? " [ERROR]" : ""
79
+ slow_flag = (req["duration"] || 0).to_f >= threshold ? " [SLOW]" : ""
80
+ status = req["status"].to_i
81
+ err_flag = status >= 400 || status == 0 ? " [ERROR]" : ""
81
82
  lines << "\n### Request #{i + 1}#{slow_flag}#{err_flag}"
82
83
  lines << "- **ID:** #{req['id']}" if req["id"]
83
84
  lines << "- **Started at:** #{req['started_at']}" if req["started_at"]
84
85
  lines << "- **Method:** #{req['method']}"
85
86
  lines << "- **URL:** #{req['url']}"
86
- lines << "- **Status:** #{req['status'] == 0 ? 'connection error' : req['status']}"
87
- lines << "- **Duration:** #{req['duration'].round(2)} ms"
87
+ lines << "- **Status:** #{status == 0 ? 'connection error' : status}"
88
+ lines << "- **Duration:** #{(req['duration'] || 0).to_f.round(2)} ms"
88
89
  lines << "- **Request Size:** #{req['request_size'].nil? ? 'unknown' : "#{'at least ' if req['request_size_is_minimum']}#{req['request_size']} bytes"}"
89
90
  lines << "- **Response Size:** #{req['response_size']} bytes"
90
91
  if req["request_body_not_captured"]
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profiler
4
+ module Middleware
5
+ # Validation of CORS configuration at boot time.
6
+ module CorsPolicy
7
+ module_function
8
+
9
+ # Logged once at boot when cors_allowed_origins contains "*", which is dangerous.
10
+ def warn_about_configuration(logger)
11
+ return unless Profiler.configuration.extension_cors_enabled
12
+ return unless Array(Profiler.configuration.cors_allowed_origins).include?("*")
13
+
14
+ Profiler.log_warn(
15
+ "CORS: '*' (wildcard origin) is set in cors_allowed_origins, which allows any website to access " \
16
+ "the profiler if a visitor browses the profiler. Set cors_allowed_origins to specific origins instead.",
17
+ logger: logger
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
@@ -56,6 +56,7 @@ module Profiler
56
56
  def token_problem
57
57
  token = Profiler.configuration.peer_token.to_s
58
58
  return "No config.peer_token is configured" if token.strip.empty?
59
+ return "config.peer_token is surrounded by spaces or newlines, which breaks HTTP requests" if token != token.strip
59
60
  return nil if token.strip.length >= MIN_TOKEN_LENGTH
60
61
 
61
62
  "config.peer_token is shorter than #{MIN_TOKEN_LENGTH} characters, so it is ignored as if none were configured"
@@ -123,6 +123,13 @@ module Profiler
123
123
  end
124
124
  end
125
125
 
126
+ initializer "profiler.warn_cors_configuration", after: :load_config_initializers do
127
+ next unless Profiler.configuration.enabled
128
+
129
+ require_relative "middleware/cors_policy"
130
+ Profiler::Middleware::CorsPolicy.warn_about_configuration(Rails.logger)
131
+ end
132
+
126
133
  initializer "profiler.start_cluster_client" do
127
134
  # Check slave? inside on_load, the app's own initializers (config/initializers/profiler.rb)
128
135
  # set master_url AFTER railtie initializers run, so the check must happen after_initialize.
@@ -5,6 +5,7 @@ require "set"
5
5
  require "rack/utils"
6
6
  require "active_support/parameter_filter"
7
7
  require_relative "cluster/security"
8
+ require_relative "peers"
8
9
 
9
10
  module Profiler
10
11
  # The single filter applied to everything the profiler captures: request
@@ -289,13 +290,16 @@ module Profiler
289
290
 
290
291
  private
291
292
 
292
- # Only a secret the cluster would use (Cluster::Security.secret_problem
293
- # nil: 32 characters or more): a shorter one is ignored by the cluster,
294
- # and masking "true" or "/" everywhere would hide the very data profiled.
293
+ # Only secrets the cluster or peers would use (long enough once stripped): a
294
+ # shorter one is ignored, and masking "true" or "/" everywhere would hide the
295
+ # very data profiled. Padded values are refused for requests but stay masked.
295
296
  def credential_values
296
- return [] unless Cluster::Security.configured_secret?
297
-
298
- [Profiler.configuration.cluster_secret.to_s.strip]
297
+ secret = Profiler.configuration.cluster_secret.to_s.strip
298
+ token = Profiler.configuration.peer_token.to_s.strip
299
+ credentials = []
300
+ credentials << secret if secret.length >= Cluster::Security::MIN_SECRET_LENGTH
301
+ credentials << token if token.length >= Peers::MIN_TOKEN_LENGTH
302
+ credentials
299
303
  end
300
304
 
301
305
  def hide_in(value, credentials)
@@ -86,7 +86,7 @@ namespace :profiler do
86
86
  task mcp: :environment do
87
87
  require_relative "../mcp/server"
88
88
 
89
- transport = ENV["MCP_TRANSPORT"]&.to_sym || Profiler.configuration.mcp_transport
89
+ transport = ENV["MCP_TRANSPORT"]&.to_sym || :stdio
90
90
  $stderr.puts "Starting Profiler MCP server (#{transport} transport)..."
91
91
 
92
92
  server = Profiler::MCP::Server.new
@@ -165,11 +165,11 @@ module Profiler
165
165
  Profiler::TestRunner.run_store.update(run.id, pid: io.pid)
166
166
 
167
167
  begin
168
- while (chunk = io.read(256))
169
- break if chunk.empty?
170
-
168
+ while (chunk = io.readpartial(4096))
171
169
  Profiler::TestRunner.run_store.append_output(run.id, chunk)
172
170
  end
171
+ rescue EOFError
172
+ # End of stream reached
173
173
  ensure
174
174
  # The process printed everything, killed or not: what was held back can be shown.
175
175
  Profiler::TestRunner.run_store.finish_output(run.id)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.21"
4
+ VERSION = "0.44.24"
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.44.21
4
+ version: 0.44.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
@@ -240,6 +240,7 @@ files:
240
240
  - lib/profiler/mcp/tools/set_env_var.rb
241
241
  - lib/profiler/middleware/capturing_body.rb
242
242
  - lib/profiler/middleware/cors_middleware.rb
243
+ - lib/profiler/middleware/cors_policy.rb
243
244
  - lib/profiler/middleware/profiler_middleware.rb
244
245
  - lib/profiler/middleware/streamed_profile.rb
245
246
  - lib/profiler/middleware/toolbar_injector.rb