utils: enhance localhost detection and support Docker Desktop host alias

Kim committed Dec 12, 2025 at 12:33 UTC 30e9e23d1e3a138e5c8a68739bb759a52e6916ab
1 file changed +29 -13
utils/utils.go
+29 -13
@@ -292,33 +292,49 @@ func DefaultBootstrapFrom(base string) string {
292 }
293
294 func IsLocalhost(r *http.Request) bool {
295 - host, _, err := net.SplitHostPort(r.RemoteAddr)
296 - if err != nil {
297 - host = r.RemoteAddr
295 + host := r.RemoteAddr
296 + if h, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
297 + host = h
298 + }
299 +
300 + // If a proxy/adapter reports a hostname, allow Docker Desktop host alias.
301 + if strings.EqualFold(host, "host.docker.internal") {
302 + return true
303 }
304
305 ip := net.ParseIP(host)
306 if ip == nil {
307 + // Try resolving hostnames to IPs (best-effort).
308 + if addrs, err := net.LookupIP(host); err == nil {
309 + for _, a := range addrs {
310 + if a.IsLoopback() || isPrivateOrDockerIP(a) {
311 + return true
312 + }
313 + }
314 + }
315 return false
316 }
304 - // Normal localhost
305 - if ip.IsLoopback() {
306 - return true
307 - }
317
309 - // Docker networks & private networks
310 - dockerRanges := []string{
311 - "172.17.0.0/16", // Linux Docker
318 + return ip.IsLoopback() || isPrivateOrDockerIP(ip)
319 +}
320 +
321 +func isPrivateOrDockerIP(ip net.IP) bool {
322 + // Common Docker bridge/Desktop ranges only.
323 + ranges := []string{
324 + "172.16.0.0/12", // Docker user-defined bridges (default pool)
325 + "172.17.0.0/16", // Linux Docker default bridge
326 "192.168.64.0/24", // Docker Desktop macOS/Windows
327 "192.168.65.0/24",
328 }
329
316 - for _, cidr := range dockerRanges {
317 - _, subnet, _ := net.ParseCIDR(cidr)
330 + for _, cidr := range ranges {
331 + _, subnet, err := net.ParseCIDR(cidr)
332 + if err != nil || subnet == nil {
333 + continue
334 + }
335 if subnet.Contains(ip) {
336 return true
337 }
338 }
322 -
339 return false
340 }