Skip to content

Warn when Net::HTTP#set_debug_output enables debug output - #320

Open
Jetbo wants to merge 2 commits into
ruby:masterfrom
Jetbo:warn-when-debug-output-enabled
Open

Warn when Net::HTTP#set_debug_output enables debug output#320
Jetbo wants to merge 2 commits into
ruby:masterfrom
Jetbo:warn-when-debug-output-enabled

Conversation

@Jetbo

@Jetbo Jetbo commented Aug 6, 2026

Copy link
Copy Markdown

Background

Net::HTTP#set_debug_output writes every request and response to the given stream in plain text, including Authorization and Cookie headers and message bodies.

Its documentation reads: "This method opens a serious security hole. Never use this method in production code."

It behaves identically in a console and in a production worker. Debug output committed by accident, or enabled by hand during an incident and never reverted, keeps writing credentials to whatever stream it was given.

Proposal

This writes a warning to $stderr naming what is being logged:

$ ruby -rnet/http -e 'Net::HTTP.new("example.com").set_debug_output($stdout)'
    -e:1: warning: Net::HTTP#set_debug_output: every request and response, including Authorization and Cookie headers and message bodies, will be written to the given stream in plain text; never enable this in production

Nothing is written when...

  • $DEBUG is set, since Ruby already has a flag meaning the process is being debugged and this library's own test harness reads it that way in test/net/http/utils.rb.
  • When output is nil, which disables debug output.
  • Or when the warning has already been written on the current thread.

There is deliberately no attempt to detect whether the process is running in "production". Ruby has no concept of a framework environment. No file in lib/ reads RAILS_ENV, RACK_ENV or APP_ENV.

The once-per-thread flag is kept in thread-local storage. set_debug_output is a configuration setter, so a connection configured once and reused pays nothing per request. Wrappers that build a Net::HTTP per request and configure it each time would otherwise repeat an identical message on every request and flood the log. Thread-local rather than a class-level instance variable because assigning the latter from a non-main Ractor raises Ractor::IsolationError. Thread#thread_variable_set has been available since 2.0, so this needs no version guard and the gem's supported Ruby range is unchanged.

The first commit is an unrelated documentation fix that can be taken on its own:

The Security section of the class documentation indexes the TLS accessors but omits set_debug_output, which is listed only under Debugging.

Implements Feature #22233.

Jetbo added 2 commits August 6, 2026 12:32
The Security section indexes the TLS accessors but omits the one method
whose own documentation calls it "a serious security hole". A reader
scanning that section for anything affecting the safety of a connection
would not find it; it is listed only under Debugging.
Debug output writes every request and response to the given stream in
plain text, including Authorization and Cookie headers and message
bodies. The documentation has said "This method opens a serious security
hole. Never use this method in production code." for as long as the
method has existed, but nothing tells the caller when that has happened:
the method behaves identically in a console and in a production worker,
so output committed by accident, or enabled during an incident and never
reverted, keeps writing credentials to whatever stream it was given.

Write a warning to $stderr naming what is being logged. Ruby has no
concept of an application environment, so there is nothing to consult to
decide whether this process is in production -- reading RAILS_ENV or
RACK_ENV would put knowledge of specific frameworks into the standard
library, and no file in lib/ does that today. Warn unconditionally
instead, and rely on $DEBUG to recognise the case where debugging is
deliberate: that is the flag Ruby already has for it, and net/http's own
test harness treats it the same way in test/net/http/utils.rb.

Warn at most once per thread. set_debug_output is a configuration setter
that net/http never calls itself, so a connection configured once and
reused pays nothing per request, but wrappers that build a Net::HTTP per
request and configure it each time would otherwise repeat an identical
message on every request and flood the log it is meant to draw attention
to. The flag lives in thread-local storage rather than on Net::HTTP
because assigning a class-level instance variable from a non-main Ractor
raises Ractor::IsolationError, and Thread#thread_variable_set has been
available since 2.0, so it needs no version guard.

The warning is plain Kernel#warn rather than a Warning category. Of the
categories Ruby 4.0 defines, only :experimental is enabled by default,
and none of them describes this; a guardrail nobody has enabled does not
guard anything. It can still be intercepted by overriding Warning.warn.

Implements [Feature #22233]
@Jetbo
Jetbo marked this pull request as ready for review August 6, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant