Skip to content

Commit fc45adb

Browse files
committed
cmd/go/internal/web: improve IP check testing on ipv6 env
The existing implementation lacks consideration of running test on a machine which has ipv6 address but no ipv4 address. Use net.IP.IsLoopback and net.IP.IsUnspecified instead of hardcoded addresses. Fixes: #48575
1 parent be571a3 commit fc45adb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/cmd/go/internal/web/http.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"errors"
1818
"fmt"
1919
"mime"
20+
"net"
2021
"net/http"
2122
urlpkg "net/url"
2223
"os"
@@ -84,8 +85,15 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
8485
if url.Host == "localhost.localdev" {
8586
return nil, fmt.Errorf("no such host localhost.localdev")
8687
}
87-
if os.Getenv("TESTGONETWORK") == "panic" && !strings.HasPrefix(url.Host, "127.0.0.1") && !strings.HasPrefix(url.Host, "0.0.0.0") {
88-
panic("use of network: " + url.String())
88+
if os.Getenv("TESTGONETWORK") == "panic" {
89+
host := url.Host
90+
if h, _, err := net.SplitHostPort(url.Host); err == nil && h != "" {
91+
host = h
92+
}
93+
addr := net.ParseIP(host)
94+
if addr == nil || (!addr.IsLoopback() && !addr.IsUnspecified()) {
95+
panic("use of network: " + url.String())
96+
}
8997
}
9098

9199
fetch := func(url *urlpkg.URL) (*urlpkg.URL, *http.Response, error) {

0 commit comments

Comments
 (0)