Skip to content

Commit 4e35e5f

Browse files
calmhbradfitz
authored andcommitted
net/http: fix ProxyFromEnvironment panic on invalid $NO_PROXY value
Given an entry in $no_proxy like ":1" we would interpret it as an empty host name and a port number, then check the first character of the host name for dots. This would then cause an index out of range panic. This change simply skips these entries, as the following checks would anyway have returned false. Fixes #19536 Change-Id: Iafe9c7a77ad4a6278c8ccb00a1575b56e4bdcd79 Reviewed-on: https://go-review.googlesource.com/38067 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent df68afd commit 4e35e5f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/net/http/proxy_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ func ResetProxyEnv() {
7979
}
8080
ResetCachedEnvironment()
8181
}
82+
83+
func TestInvalidNoProxy(t *testing.T) {
84+
ResetProxyEnv()
85+
os.Setenv("NO_PROXY", ":1")
86+
useProxy("example.com:80") // should not panic
87+
}

src/net/http/transport.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,10 @@ func useProxy(addr string) bool {
12271227
if addr == p {
12281228
return false
12291229
}
1230+
if len(p) == 0 {
1231+
// There is no host part, likely the entry is malformed; ignore.
1232+
continue
1233+
}
12301234
if p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:]) {
12311235
// no_proxy ".foo.com" matches "bar.foo.com" or "foo.com"
12321236
return false

0 commit comments

Comments
 (0)