net/netutil: add NewDefaultTransport to avoid http.DefaultTransport panics

Several packages built their HTTP transports with

    http.DefaultTransport.(*http.Transport).Clone()

The standard library only documents http.DefaultTransport as an
http.RoundTripper, so an application is free to replace it with a
RoundTripper that is not a *http.Transport (e.g. an instrumented or
tracing wrapper). When such an application embeds tsnet.Server, the
unchecked type assertion panics as soon as tsnet brings up its control
connection, DNS bootstrap, or log uploader.

Add netutil.NewDefaultTransport, which returns a clone of the global
when it is still the standard *http.Transport (preserving existing
behavior) and otherwise returns a fresh transport mirroring the stdlib
defaults. Route every clone site through it.

Updates #19937

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
This commit is contained in:
Achille Roussel
2026-06-01 12:28:36 -07:00
committed by Brad Fitzpatrick
co-authored by Claude
parent 5495eb7e1a
commit 7f3bbc9865
13 changed files with 107 additions and 11 deletions
+55
View File
@@ -6,8 +6,10 @@ package netutil
import (
"io"
"net"
"net/http"
"runtime"
"testing"
"time"
)
type conn struct {
@@ -53,6 +55,59 @@ func TestOneConnListener(t *testing.T) {
}
}
// roundTripperFunc is an http.RoundTripper that is not a *http.Transport,
// used to exercise the fallback path of NewDefaultTransport.
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestNewDefaultTransport(t *testing.T) {
// Standard case: http.DefaultTransport is still a *http.Transport, so we
// get a clone of it with the stdlib defaults.
tr := NewDefaultTransport()
if tr == nil {
t.Fatal("got nil transport")
}
if got, want := tr.MaxIdleConns, 100; got != want {
t.Errorf("MaxIdleConns = %d; want %d", got, want)
}
if got, want := tr.IdleConnTimeout, 90*time.Second; got != want {
t.Errorf("IdleConnTimeout = %v; want %v", got, want)
}
if !tr.ForceAttemptHTTP2 {
t.Error("ForceAttemptHTTP2 = false; want true")
}
// Regression case: an application has replaced http.DefaultTransport with
// a RoundTripper that is not a *http.Transport. NewDefaultTransport must
// not panic and must still return a usable transport with stdlib defaults.
orig := http.DefaultTransport
defer func() { http.DefaultTransport = orig }()
http.DefaultTransport = roundTripperFunc(func(*http.Request) (*http.Response, error) {
return nil, nil
})
tr = NewDefaultTransport()
if tr == nil {
t.Fatal("got nil transport on fallback path")
}
if got, want := tr.MaxIdleConns, 100; got != want {
t.Errorf("fallback MaxIdleConns = %d; want %d", got, want)
}
if got, want := tr.IdleConnTimeout, 90*time.Second; got != want {
t.Errorf("fallback IdleConnTimeout = %v; want %v", got, want)
}
if !tr.ForceAttemptHTTP2 {
t.Error("fallback ForceAttemptHTTP2 = false; want true")
}
if tr.DialContext == nil {
t.Error("fallback DialContext = nil; want non-nil")
}
if tr.Proxy == nil {
t.Error("fallback Proxy = nil; want non-nil")
}
}
func TestIPForwardingEnabledLinux(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("skipping on %s", runtime.GOOS)