| 1 | package installsource |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | ) |
| 9 | |
| 10 | // ssrfGuardClient wraps base so every fetch refuses to connect to private, |
| 11 | // link-local, CGNAT, or unspecified addresses — the SSRF surface a prompt- |
| 12 | // injected install source would aim at (cloud metadata at 169.254.169.254, |
| 13 | // RFC1918 internal services). Loopback is allowed: the agent can already reach |
| 14 | // localhost via bash, and the install tests serve over 127.0.0.1. The check |
| 15 | // runs at dial time on the resolved IP and then dials that vetted IP, so a |
| 16 | // public host that DNS-rebinds to an internal address is caught too. |
| 17 | // |
| 18 | // This mirrors web_fetch's guard (internal/tool/builtin/webfetch.go); the |
| 19 | // install_source tool fetches the same kind of untrusted URLs and must not be |
| 20 | // the one un-guarded path. Kept in sync by hand — both block the same set. |
| 21 | func ssrfGuardClient(base *http.Client) *http.Client { |
| 22 | guarded := *base // copy Timeout etc. |
| 23 | if t, ok := base.Transport.(*http.Transport); ok && t != nil { |
| 24 | ct := t.Clone() |
| 25 | inner := ct.DialContext |
| 26 | if inner == nil { |
| 27 | inner = (&net.Dialer{}).DialContext |
| 28 | } |
| 29 | ct.DialContext = ssrfDial(inner) |
| 30 | guarded.Transport = ct |
| 31 | } else { |
| 32 | // Non-*http.Transport (or nil Transport): build a fresh guarded transport. |
| 33 | // The real paths — boot's netclient and the tests' httptest client — are |
| 34 | // always *http.Transport, so this branch only covers a bare &http.Client{}. |
| 35 | guarded.Transport = &http.Transport{DialContext: ssrfDial((&net.Dialer{}).DialContext)} |
| 36 | } |
| 37 | return &guarded |
| 38 | } |
| 39 | |
| 40 | func ssrfDial(inner func(context.Context, string, string) (net.Conn, error)) func(context.Context, string, string) (net.Conn, error) { |
| 41 | return func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 42 | host, port, err := net.SplitHostPort(addr) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | ips, err := net.DefaultResolver.LookupIPAddr(ctx, host) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | for _, ip := range ips { |
| 51 | if blockedFetchIP(ip.IP) { |
| 52 | return nil, fmt.Errorf("refusing to fetch internal address %s (resolves to %s)", host, ip.IP) |
| 53 | } |
| 54 | } |
| 55 | // Dial the vetted IP, not the hostname, so the connection can't re-resolve |
| 56 | // to a different (internal) address (DNS rebinding). |
| 57 | return inner(ctx, network, net.JoinHostPort(ips[0].IP.String(), port)) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // cgnatRange is RFC 6598 shared address space (100.64.0.0/10). Go's IsPrivate |
| 62 | // doesn't cover it, yet some clouds host instance metadata there (Alibaba Cloud |
| 63 | // at 100.100.100.200), so it's an SSRF target to refuse too. |
| 64 | var cgnatRange = mustCIDR("100.64.0.0/10") |
| 65 | |
| 66 | func mustCIDR(s string) *net.IPNet { |
| 67 | _, n, err := net.ParseCIDR(s) |
| 68 | if err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | return n |
| 72 | } |
| 73 | |
| 74 | // blockedFetchIP reports whether ip is an address install_source must not reach. |
| 75 | // Loopback is intentionally allowed (see ssrfGuardClient). |
| 76 | func blockedFetchIP(ip net.IP) bool { |
| 77 | return ip.IsPrivate() || // RFC1918 + IPv6 unique-local (fc00::/7) |
| 78 | ip.IsLinkLocalUnicast() || // 169.254.0.0/16 (incl. cloud metadata) + fe80::/10 |
| 79 | ip.IsLinkLocalMulticast() || |
| 80 | ip.IsUnspecified() || // 0.0.0.0 / :: |
| 81 | cgnatRange.Contains(ip) // 100.64.0.0/10 (incl. Alibaba Cloud metadata) |
| 82 | } |
| 83 |