guard against a replaced http.DefaultTransport in ssrf.NewTransport

Type-asserting http.DefaultTransport.(*http.Transport) panics if a dependency swaps the global for a different RoundTripper. Fall back to a fresh transport with the standard defaults so boot can't crash.
This commit is contained in:
Abhinav Raut
2026-07-18 11:40:12 +05:30
parent c7f55e2394
commit 8f34fc5ee5
+12 -1
View File
@@ -40,7 +40,18 @@ func NewControl(enabled bool, allowedCIDRs []string, lo *logf.Logger) Control {
// NewTransport clones http.DefaultTransport (keeping proxy and connection-pool defaults) and applies the SSRF dial guard; callers may override the returned transport's timeouts.
func NewTransport(control Control, dialTimeout time.Duration) *http.Transport {
t := http.DefaultTransport.(*http.Transport).Clone()
base, ok := http.DefaultTransport.(*http.Transport)
if !ok {
base = &http.Transport{
Proxy: http.ProxyFromEnvironment,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
t := base.Clone()
t.DialContext = (&net.Dialer{
Timeout: dialTimeout,
KeepAlive: 30 * time.Second,