rails-profiler 0.44.15 → 0.44.17

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: 5494040c10e8c5e51c20aacf54788fdab573a5814f394ced5c7c7f7caa4693aa
4
- data.tar.gz: b84ed786eaed7a1f59ed8ee9b953a546be714432dbecd7ad372fee2b4446d567
3
+ metadata.gz: 35e6f32ca6ce4fe99441b079cd304f93539eacb3dffa267af96ce6945d14911b
4
+ data.tar.gz: '014679015c78eb282be8f5ee19cb6a97be988f182ae91e1e7e33ddade5db3bd5'
5
5
  SHA512:
6
- metadata.gz: 4a40f0b6d4d6c79de401838152198aae9528b795061f96c991e7a4bc1c65d8881af8f489d71f8e02078ee110a0de49b3d6715b82f818e00643fbbb67c9e84975
7
- data.tar.gz: 8b49e505d5a86c01debb8fed088e3e428ff1e28b1492009f8a86c655c70d258b0f323d394fee3a3d2fac49ab2369ba4a14e1a106f7e2a651ebdf22cb14c37222
6
+ metadata.gz: e66c36f8ac5120128e2f1ac069fa770ba141ab707f63f072fac704258333be9175065ad3fcac9940ef2fdbd3a0c22dc50eb452fa7b06bd17df4985492545d66d
7
+ data.tar.gz: 8637901e09759b2532300b92bbf9de2f0d4cd4ddf4aba1669cdc25b0ebc2ef2ca5045b0f91538e429d18d75f25301ebcb51f34f3e35b6acd31cd8db36a10b10c
data/CHANGELOG.md CHANGED
@@ -19,6 +19,26 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.44.17] - 2026-10-25
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Security
27
+
28
+ - **EnvOverrides:** the profiler now refuses to restore environment variables blocked by `EnvKeyPolicy` when a reset is requested, closing a bypass where protected keys (DATABASE_URL, RUBYOPT, etc.) could be restored from persisted overrides.
29
+ - **Authorization:** the profiler now validates the Host header against local names, `config.hosts` and `config.mcp_allowed_hosts` in all authorization modes, including `:allow_authorized`, preventing DNS rebinding attacks when using custom authorization blocks.
30
+
31
+ ## [0.44.16] - 2026-10-23
32
+
33
+ <!-- stamped -->
34
+
35
+ ### Fixed
36
+
37
+ - **LogCollector:** Prevent `BroadcastLogger` level from forcing `DEBUG` globally across all threads during profiling.
38
+ - **Middleware:** Skip toolbar injection for HTML downloads (`Content-Disposition: attachment`) and compressed responses (`Content-Encoding` present) to prevent file corruption.
39
+ - **Middleware:** Remove `ETag` and `Last-Modified` headers when toolbar is injected to prevent stale cached responses on 304 Not Modified.
40
+ - **Profiler:** Catch `Exception` (not just `StandardError`) to properly handle `Interrupt`, `SignalException`, and `Sidekiq::Shutdown`, marking profiles as failed instead of completed.
41
+
22
42
  ## [0.44.15] - 2026-10-23
23
43
 
24
44
  <!-- stamped -->
@@ -70,7 +70,22 @@ module Profiler
70
70
  return
71
71
  end
72
72
 
73
+ # EnvOverrideStore#reset already drops a blocked key from the overrides file instead of
74
+ # restoring it (see EnvKeyPolicy), so this removes a stale entry left by an older version
75
+ # the same way reset_all and the MCP reset_env_var tool do, rather than leaving it stuck
76
+ # until a full reset_all. An invalid or reserved name is a different, pre-existing case
77
+ # (cleaning up garbage left in the file): it is removed the same way, without an error.
73
78
  Profiler.env_override_store.reset(key)
79
+
80
+ # code_loading? also answers true for an invalid name (by design, for the test runner):
81
+ # excluded here, since an invalid name is cleaned up without an error, not "protected".
82
+ if Profiler::EnvKeyPolicy.valid_name?(key) &&
83
+ (Profiler::EnvKeyPolicy.protected?(key) || Profiler::EnvKeyPolicy.code_loading?(key))
84
+ render json: { error: "#{key} is protected; it was removed from the overrides but not restored" },
85
+ status: :unprocessable_entity
86
+ return
87
+ end
88
+
74
89
  render json: { key: key, value: redacted_value(key), reset: true }
75
90
  end
76
91
 
@@ -145,8 +145,10 @@ module Profiler
145
145
 
146
146
  if defined?(Rails) && Rails.logger
147
147
  if Rails.logger.respond_to?(:broadcast_to)
148
- @capture_logger = CaptureLogger.new(@logs)
149
148
  @broadcaster = Rails.logger
149
+ level = @broadcaster.level
150
+ @capture_logger = CaptureLogger.new(@logs)
151
+ @capture_logger.level = level
150
152
  @broadcaster.broadcast_to(@capture_logger)
151
153
  elsif defined?(ActiveSupport::Logger) && ActiveSupport::Logger.respond_to?(:broadcast)
152
154
  self.class.attach_legacy_broadcast(Rails.logger)
@@ -403,6 +403,17 @@ module Profiler
403
403
  LocalRequest.warn_once(reason)
404
404
  false
405
405
  when :allow_authorized
406
+ # DNS rebinding protection: validate the Host header even when using a custom
407
+ # authorization block. The block may rely on REMOTE_ADDR (e.g., docker.include?(REMOTE_ADDR)),
408
+ # which is not enough: an attacker can reach an allowed address with a Host header under
409
+ # their control, bypassing the block's intent. A deployment that is reached under a
410
+ # non-loopback name (a reverse proxy, a Docker network alias) must list it in config.hosts
411
+ # or config.mcp_allowed_hosts, same as the MCP HTTP transport already requires.
412
+ if (denial = authorized_host_denial_reason(request))
413
+ warn_once_authorized_host(denial)
414
+ return false
415
+ end
416
+
406
417
  run_authorize_block(request)
407
418
  else
408
419
  false
@@ -500,6 +511,47 @@ module Profiler
500
511
  "host name (#{e.message})"
501
512
  end
502
513
 
514
+ # The DNS rebinding check for :allow_authorized: nil when the Host and X-Forwarded-Host
515
+ # headers are allowed, otherwise a sentence naming the cause. A host is allowed when it is
516
+ # local, listed in config.hosts, or listed in config.mcp_allowed_hosts: the same rule the MCP
517
+ # HTTP transport already applies (Profiler::MCP::HttpGuard.allowed_host?), so a deployment
518
+ # that already lists its host for MCP does not have to list it twice.
519
+ def authorized_host_denial_reason(request)
520
+ require_relative "mcp/http_guard"
521
+
522
+ # An empty Host header, like a missing one, does not come from a browser (HTTP/1.1
523
+ # requires it): HttpGuard.allowed_host? only treats nil that way, so blank is checked here.
524
+ raw_host = request.get_header("HTTP_HOST").to_s
525
+ unless raw_host.empty? || MCP::HttpGuard.allowed_host?(raw_host, self)
526
+ return "the Host header #{raw_host.inspect} is not allowed: list it in config.hosts or " \
527
+ "config.mcp_allowed_hosts"
528
+ end
529
+
530
+ forwarded_hosts = request.get_header("HTTP_X_FORWARDED_HOST").to_s.split(",")
531
+ remote = forwarded_hosts.find { |h| !MCP::HttpGuard.allowed_host?(h, self) }
532
+ return "the X-Forwarded-Host header #{remote.inspect} is not allowed: list it in config.hosts or " \
533
+ "config.mcp_allowed_hosts" if remote
534
+
535
+ nil
536
+ end
537
+
538
+ # Logged once per process, like LocalRequest.warn_once for :allow_local: an operator seeing
539
+ # the profiler go dark under :allow_authorized needs to know the Host check is the cause, not
540
+ # a silently rejecting authorize_with block.
541
+ def warn_once_authorized_host(reason)
542
+ @warn_authorized_host_mutex ||= Mutex.new
543
+ @warn_authorized_host_mutex.synchronize do
544
+ return if @warned_authorized_host
545
+
546
+ @warned_authorized_host = true
547
+ end
548
+
549
+ Profiler.log_warn(
550
+ "Request refused by authorization_mode :allow_authorized before the authorize_with " \
551
+ "block ran: #{reason}. This message is logged once per process."
552
+ )
553
+ end
554
+
503
555
  # An exception in the application's own authorization code is a denial, never an error
504
556
  # for the request: it would otherwise break every request. The detail stays in the log.
505
557
  def run_authorize_block(request)
@@ -59,7 +59,7 @@ module Profiler
59
59
  result = block.call
60
60
  console_collector.set_return_value(result)
61
61
  result
62
- rescue => e
62
+ rescue Exception => e # rubocop:disable Lint/RescueException
63
63
  console_status = "failed"
64
64
  exception_collector&.capture(e)
65
65
  raise
@@ -81,8 +81,13 @@ module Profiler
81
81
  surfacing("reset #{key}") do
82
82
  update_overrides do |overrides|
83
83
  entry = overrides[key]
84
- # Keep a RESTORE entry so Sidekiq workers pick it up on next job
85
- overrides[key] = restore_entry(entry["original"]) if entry
84
+ # Keep a RESTORE entry so Sidekiq workers pick it up on next job, but remove the key
85
+ # entirely if it is blocked by EnvKeyPolicy: refusing to restore protected keys
86
+ # applies even when reading from persisted overrides.
87
+ if entry
88
+ overrides[key] = EnvKeyPolicy.allowed?(key) ? restore_entry(entry["original"]) : nil
89
+ overrides.delete(key) if overrides[key].nil?
90
+ end
86
91
  end
87
92
  end
88
93
 
@@ -98,6 +103,14 @@ module Profiler
98
103
  # Already reset: its reset_at stays, so that it still expires.
99
104
  next if restore_entry?(entry)
100
105
 
106
+ # Remove keys blocked by EnvKeyPolicy entirely instead of leaving a RESTORE sentinel,
107
+ # preventing persisted overrides from restoring protected variables.
108
+ unless EnvKeyPolicy.allowed?(key)
109
+ originals[key] = nil
110
+ overrides.delete(key)
111
+ next
112
+ end
113
+
101
114
  originals[key] = entry.is_a?(Hash) ? entry["original"] : nil
102
115
  # Leave a RESTORE sentinel for Sidekiq workers to pick up
103
116
  overrides[key] = restore_entry(originals[key])
@@ -289,6 +302,10 @@ module Profiler
289
302
  # entry is reset in the file, and there is nothing to put back here.
290
303
  return false unless EnvKeyPolicy.valid_name?(key)
291
304
  return false if EnvKeyPolicy.value_rejection(key, original)
305
+ # Do not restore environment variables blocked by EnvKeyPolicy: refusal to restore a
306
+ # protected key (DATABASE_URL, RUBYOPT, etc.) applies even when reloading persisted
307
+ # overrides from file, preventing bypasses through legacy override files.
308
+ return false unless EnvKeyPolicy.allowed?(key)
292
309
 
293
310
  original.nil? ? ENV.delete(key) : ENV[key] = original
294
311
  true
@@ -86,7 +86,7 @@ module Profiler
86
86
  begin
87
87
  result = block.call
88
88
  result
89
- rescue => e
89
+ rescue Exception => e # rubocop:disable Lint/RescueException
90
90
  job_status = "failed"
91
91
  error_message = "#{e.class}: #{e.message}"
92
92
  exception_collector&.capture(e)
@@ -136,11 +136,15 @@ module Profiler
136
136
 
137
137
  set_header(headers, TOKEN_HEADER, profile.token)
138
138
 
139
- if html_response?(headers)
139
+ if html_response?(headers) && !attachment_response?(headers) && !encoded_response?(headers)
140
140
  nonce = env['action_dispatch.content_security_policy_nonce']
141
141
  injected = ToolbarInjector.new(body, profile.token, nonce).inject
142
142
  # A HEAD answer, or a page without </body>, keeps the length it announced.
143
- update_content_length(headers, injected) unless injected.equal?(body)
143
+ unless injected.equal?(body)
144
+ update_content_length(headers, injected)
145
+ delete_header(headers, "ETag")
146
+ delete_header(headers, "Last-Modified")
147
+ end
144
148
  body = injected
145
149
  end
146
150
 
@@ -346,6 +350,15 @@ module Profiler
346
350
  content_type(headers).include?("text/html")
347
351
  end
348
352
 
353
+ def attachment_response?(headers)
354
+ disposition = header(headers, "content-disposition")
355
+ disposition&.split(";", 2)&.first&.strip&.casecmp?("attachment")
356
+ end
357
+
358
+ def encoded_response?(headers)
359
+ header(headers, "content-encoding").present?
360
+ end
361
+
349
362
  def content_type(headers)
350
363
  header(headers, "content-type").to_s
351
364
  end
@@ -368,6 +381,11 @@ module Profiler
368
381
  headers[existing.first || name] = value
369
382
  end
370
383
 
384
+ def delete_header(headers, name)
385
+ existing = headers.keys.select { |key| key.to_s.casecmp?(name) }
386
+ existing.each { |key| headers.delete(key) }
387
+ end
388
+
371
389
  # Only a length the application announced is corrected: a chunked or unsized answer
372
390
  # stays without one.
373
391
  def update_content_length(headers, body)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.15"
4
+ VERSION = "0.44.17"
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.15
4
+ version: 0.44.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy