From b3da5de10f3b1b259a24d07e4fc9f693dc500cf2 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Wed, 16 Nov 2022 11:35:01 -0800 Subject: [PATCH] ipn/localapi: also allow localhost as the LocalAPI host The Mac and iOS LocalAPI clients make requests to it. Signed-off-by: Mihai Parparita --- ipn/localapi/localapi.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ipn/localapi/localapi.go b/ipn/localapi/localapi.go index acfff428f..31f9b363d 100644 --- a/ipn/localapi/localapi.go +++ b/ipn/localapi/localapi.go @@ -168,15 +168,26 @@ func validHost(h string) bool { case "", apitype.LocalAPIHost: return true } - // Otherwise, any Host header we see should at most be an ip:port. - ap, err := netip.ParseAddrPort(h) + // Allow either localhost or loopback IP hosts. + host, portStr, err := net.SplitHostPort(h) if err != nil { return false } - if runtime.GOOS == "windows" && ap.Port() != safesocket.WindowsLocalPort { + port, err := strconv.ParseUint(portStr, 10, 16) + if err != nil { + return false + } + if runtime.GOOS == "windows" && port != safesocket.WindowsLocalPort { + return false + } + if host == "localhost" { + return true + } + addr, err := netip.ParseAddr(h) + if err != nil { return false } - return ap.Addr().IsLoopback() + return addr.IsLoopback() } // handlerForPath returns the LocalAPI handler for the provided Request.URI.Path.