Skip to content

Commit a02ddfa

Browse files
nicholashusingopherbot
authored andcommitted
http/httpproxy: prioritize lowercase proxy environment variables
For HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables, prioritize the lowercase version if both the uppercase and lowercase versions are set. While there is not an official standard, prioritizing the lowercase environment variables seem to be what most implementations have converged on. Therefore, we might as well follow. This change should be relatively safe. Users will only observe a behavior difference should they be setting both an uppercase and lowercase proxy environment variables with different values. For golang/go#79656 Change-Id: Ife71d260bcc61c52617a05cf5c3d53fb6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/801700 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Auto-Submit: Nicholas Husin <[email protected]>
1 parent 574e5eb commit a02ddfa

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎http/httpproxy/proxy.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ type config struct {
8080
domainMatchers []matcher
8181
}
8282

83-
// FromEnvironment returns a Config instance populated from the
84-
// environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the
85-
// lowercase versions thereof).
83+
// FromEnvironment returns a Config instance populated from the environment
84+
// variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions
85+
// thereof). When both the uppercase and lowercase versions are provided, the
86+
// lowercase versions are prioritized.
8687
//
8788
// The environment values may be either a complete URL or a
8889
// "host[:port]", in which case the "http" scheme is assumed. An error
8990
// is returned if the value is a different form.
9091
func FromEnvironment() *Config {
9192
return &Config{
92-
HTTPProxy: getEnvAny("HTTP_PROXY", "http_proxy"),
93-
HTTPSProxy: getEnvAny("HTTPS_PROXY", "https_proxy"),
94-
NoProxy: getEnvAny("NO_PROXY", "no_proxy"),
93+
HTTPProxy: getEnvAny("http_proxy", "HTTP_PROXY"),
94+
HTTPSProxy: getEnvAny("https_proxy", "HTTPS_PROXY"),
95+
NoProxy: getEnvAny("no_proxy", "NO_PROXY"),
9596
CGI: os.Getenv("REQUEST_METHOD") != "",
9697
}
9798
}

‎http/httpproxy/proxy_test.go‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"fmt"
1111
"net/url"
12-
"os"
1312
"strings"
1413
"testing"
1514

@@ -260,10 +259,10 @@ func TestProxyForURL(t *testing.T) {
260259
}
261260

262261
func TestFromEnvironment(t *testing.T) {
263-
os.Setenv("HTTP_PROXY", "httpproxy")
264-
os.Setenv("HTTPS_PROXY", "httpsproxy")
265-
os.Setenv("NO_PROXY", "noproxy")
266-
os.Setenv("REQUEST_METHOD", "")
262+
t.Setenv("HTTP_PROXY", "httpproxy")
263+
t.Setenv("HTTPS_PROXY", "httpsproxy")
264+
t.Setenv("NO_PROXY", "noproxy")
265+
t.Setenv("REQUEST_METHOD", "")
267266
got := httpproxy.FromEnvironment()
268267
want := httpproxy.Config{
269268
HTTPProxy: "httpproxy",
@@ -276,10 +275,10 @@ func TestFromEnvironment(t *testing.T) {
276275
}
277276

278277
func TestFromEnvironmentWithRequestMethod(t *testing.T) {
279-
os.Setenv("HTTP_PROXY", "httpproxy")
280-
os.Setenv("HTTPS_PROXY", "httpsproxy")
281-
os.Setenv("NO_PROXY", "noproxy")
282-
os.Setenv("REQUEST_METHOD", "PUT")
278+
t.Setenv("HTTP_PROXY", "httpproxy")
279+
t.Setenv("HTTPS_PROXY", "httpsproxy")
280+
t.Setenv("NO_PROXY", "noproxy")
281+
t.Setenv("REQUEST_METHOD", "PUT")
283282
got := httpproxy.FromEnvironment()
284283
want := httpproxy.Config{
285284
HTTPProxy: "httpproxy",
@@ -293,10 +292,10 @@ func TestFromEnvironmentWithRequestMethod(t *testing.T) {
293292
}
294293

295294
func TestFromEnvironmentLowerCase(t *testing.T) {
296-
os.Setenv("http_proxy", "httpproxy")
297-
os.Setenv("https_proxy", "httpsproxy")
298-
os.Setenv("no_proxy", "noproxy")
299-
os.Setenv("REQUEST_METHOD", "")
295+
t.Setenv("http_proxy", "httpproxy")
296+
t.Setenv("https_proxy", "httpsproxy")
297+
t.Setenv("no_proxy", "noproxy")
298+
t.Setenv("REQUEST_METHOD", "")
300299
got := httpproxy.FromEnvironment()
301300
want := httpproxy.Config{
302301
HTTPProxy: "httpproxy",
@@ -308,6 +307,25 @@ func TestFromEnvironmentLowerCase(t *testing.T) {
308307
}
309308
}
310309

310+
func TestFromEnvironmentUpperAndLowerCase(t *testing.T) {
311+
t.Setenv("HTTP_PROXY", "httpproxyupper")
312+
t.Setenv("HTTPS_PROXY", "httpsproxyupper")
313+
t.Setenv("NO_PROXY", "noproxyupper")
314+
t.Setenv("http_proxy", "httpproxylower")
315+
t.Setenv("https_proxy", "httpsproxylower")
316+
t.Setenv("no_proxy", "noproxylower")
317+
t.Setenv("REQUEST_METHOD", "")
318+
got := httpproxy.FromEnvironment()
319+
want := httpproxy.Config{
320+
HTTPProxy: "httpproxylower",
321+
HTTPSProxy: "httpsproxylower",
322+
NoProxy: "noproxylower",
323+
}
324+
if *got != want {
325+
t.Errorf("unexpected proxy config, got %#v want %#v", got, want)
326+
}
327+
}
328+
311329
var UseProxyTests = []struct {
312330
host string
313331
match bool

0 commit comments

Comments
 (0)