Warn when Net::HTTP#set_debug_output enables debug output - #320
Open
Jetbo wants to merge 2 commits into
Open
Conversation
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
marked this pull request as ready for review
August 6, 2026 19:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Net::HTTP#set_debug_outputwrites every request and response to the given stream in plain text, includingAuthorizationandCookieheaders 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
$stderrnaming what is being logged:Nothing is written when...
$DEBUGis set, since Ruby already has a flag meaning the process is being debugged and this library's own test harness reads it that way intest/net/http/utils.rb.outputisnil, which disables debug output.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/readsRAILS_ENV,RACK_ENVorAPP_ENV.The once-per-thread flag is kept in thread-local storage.
set_debug_outputis a configuration setter, so a connection configured once and reused pays nothing per request. Wrappers that build aNet::HTTPper 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 raisesRactor::IsolationError.Thread#thread_variable_sethas 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
Securitysection of the class documentation indexes the TLS accessors but omitsset_debug_output, which is listed only underDebugging.Implements Feature #22233.