|
| 1 | +package links |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// privateRanges are the IPv4 CIDR blocks that should not be reachable |
| 12 | +// via link validation. This covers loopback, RFC 1918 private networks, |
| 13 | +// link-local, and the cloud metadata endpoint range. |
| 14 | +var privateRanges []*net.IPNet |
| 15 | + |
| 16 | +func init() { |
| 17 | + for _, cidr := range []string{ |
| 18 | + "127.0.0.0/8", // loopback |
| 19 | + "10.0.0.0/8", // RFC 1918 |
| 20 | + "172.16.0.0/12", // RFC 1918 |
| 21 | + "192.168.0.0/16", // RFC 1918 |
| 22 | + "169.254.0.0/16", // link-local |
| 23 | + "::1/128", // IPv6 loopback |
| 24 | + "fc00::/7", // IPv6 unique local |
| 25 | + "fe80::/10", // IPv6 link-local |
| 26 | + } { |
| 27 | + _, block, _ := net.ParseCIDR(cidr) |
| 28 | + privateRanges = append(privateRanges, block) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// isPrivateIP reports whether ip falls within a private or reserved range. |
| 33 | +func isPrivateIP(ip net.IP) bool { |
| 34 | + for _, block := range privateRanges { |
| 35 | + if block.Contains(ip) { |
| 36 | + return true |
| 37 | + } |
| 38 | + } |
| 39 | + return false |
| 40 | +} |
| 41 | + |
| 42 | +// safeTransport returns an *http.Transport whose DialContext resolves the |
| 43 | +// target hostname and refuses to connect if the resolved IP is private. |
| 44 | +// This prevents SSRF when following URLs extracted from untrusted skill content. |
| 45 | +func safeTransport() *http.Transport { |
| 46 | + dialer := &net.Dialer{Timeout: 5 * time.Second} |
| 47 | + return &http.Transport{ |
| 48 | + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 49 | + host, port, err := net.SplitHostPort(addr) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + ips, err := net.DefaultResolver.LookupIPAddr(ctx, host) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + for _, ip := range ips { |
| 58 | + if isPrivateIP(ip.IP) { |
| 59 | + return nil, fmt.Errorf("link validation blocked request to private address %s (%s)", ip.IP, host) |
| 60 | + } |
| 61 | + } |
| 62 | + // Connect to the first resolved address. |
| 63 | + return dialer.DialContext(ctx, network, net.JoinHostPort(ips[0].IP.String(), port)) |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// newHTTPClient creates the HTTP client used for link checking. It is a |
| 69 | +// package-level variable so tests can replace it with a client that permits |
| 70 | +// loopback connections to httptest servers. |
| 71 | +var newHTTPClient = func() *http.Client { |
| 72 | + return &http.Client{ |
| 73 | + Transport: safeTransport(), |
| 74 | + Timeout: 10 * time.Second, |
| 75 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 76 | + if len(via) >= 10 { |
| 77 | + return fmt.Errorf("too many redirects") |
| 78 | + } |
| 79 | + return nil |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
0 commit comments