guardedDialControl is a net.Dialer.Control hook that rejects a connection whose resolved address is a disallowed (private/loopback/link-local/…) IP. It runs AFTER DNS resolution with the concrete address the socket will connect to, so the IP that is validated is exactly the IP that is dialed — no re
(_, address string, _ syscall.RawConn)
| 36 | // no resolve-then-re-resolve TOCTOU. This is the second line of defense the |
| 37 | // registration-time check cannot provide once a hostname's DNS can change. |
| 38 | func guardedDialControl(_, address string, _ syscall.RawConn) error { |
| 39 | host, _, err := net.SplitHostPort(address) |
| 40 | if err != nil { |
| 41 | return fmt.Errorf("webhook dial: bad address %q: %w", address, err) |
| 42 | } |
| 43 | ip := net.ParseIP(host) |
| 44 | if ip == nil { |
| 45 | return fmt.Errorf("webhook dial: unparseable resolved address %q", host) |
| 46 | } |
| 47 | if IsDisallowedWebhookIP(ip) { |
| 48 | return fmt.Errorf("webhook dial blocked: resolved to disallowed IP %s", ip) |
| 49 | } |
| 50 | return nil |
| 51 | } |