feat(web): add SafeHTTPClient with IP-pinning transport (S2)
This commit is contained in:
1 parent
5d5dcd1902
commit
083122a81f
2 files changed
+99
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SafeHTTPClient returns an *http.Client whose transport resolves and validates
|
||||
// the destination host, then dials the exact validated IP — closing the
|
||||
// TOCTOU/DNS-rebinding window where a stock http.Client would re-resolve the
|
||||
// hostname independently of ValidateURLNotInternal. Redirects are re-validated.
|
||||
func SafeHTTPClient(timeout time.Duration) *http.Client {
|
||||
dialer := &net.Dialer{Timeout: timeout}
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot resolve %q — blocking to prevent SSRF", host)
|
||||
}
|
||||
var dialIP net.IP
|
||||
for _, ip := range ips {
|
||||
if isPrivateIP(ip) {
|
||||
return nil, fmt.Errorf("access to internal address %s (%s) is blocked", host, ip)
|
||||
}
|
||||
if dialIP == nil {
|
||||
dialIP = ip
|
||||
}
|
||||
}
|
||||
if dialIP == nil {
|
||||
return nil, fmt.Errorf("no usable address for %q", host)
|
||||
}
|
||||
return dialer.DialContext(ctx, network, net.JoinHostPort(dialIP.String(), port))
|
||||
},
|
||||
}
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
if len(via) >= 10 {
|
||||
return fmt.Errorf("too many redirects (max 10)")
|
||||
}
|
||||
if err := ValidateURLNotInternal(req.URL.String()); err != nil {
|
||||
return fmt.Errorf("redirect blocked: %w", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user