Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure proxy passed in X-Upstream-Https-Proxy is parsable #224

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/smokescreen/smokescreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,9 @@ func checkACLsForRequest(config *Config, req *http.Request, destination hostport

if connectProxyHost != "" {
connectProxyUrl, err := url.Parse(connectProxyHost)
if err == nil && connectProxyUrl.Hostname() == "" {
err = errors.New("proxy header contains invalid URL. The correct format is https://[username:password@]my.proxy.srv:12345")
}

if err != nil {
config.Log.WithFields(logrus.Fields{
Expand Down
32 changes: 30 additions & 2 deletions pkg/smokescreen/smokescreen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,34 @@ func TestCONNECTProxyACLs(t *testing.T) {
r.Equal(false, entry.Data["allow"])
})

t.Run("Blocks if proxy can't be parsed when the X-Upstream-Https-Proxy header is set", func(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
r := require.New(t)
l, err := net.Listen("tcp", "localhost:0")
r.NoError(err)
cfg, err := testConfig("test-external-connect-proxy-blocked-srv")
r.NoError(err)
cfg.Listener = l

err = cfg.SetAllowAddresses([]string{"127.0.0.1"})
r.NoError(err)

internalToStripeProxy := proxyServer(cfg)
remote := httptest.NewTLSServer(h)

client, err := proxyClientWithConnectHeaders(internalToStripeProxy.URL, http.Header{"X-Upstream-Https-Proxy": []string{"google.com"}})
r.NoError(err)

req, err := http.NewRequest("GET", remote.URL, nil)
r.NoError(err)

_, err = client.Do(req)
r.Error(err)
r.Contains(err.Error(), "Request rejected by proxy")
})

t.Run("Allows an approved proxy when the X-Upstream-Https-Proxy header is set", func(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
Expand Down Expand Up @@ -1334,15 +1362,15 @@ func TestCONNECTProxyACLs(t *testing.T) {
externalProxy.StartTLS()

remote := httptest.NewTLSServer(h)
first_client, err := proxyClientWithConnectHeaders(proxy.URL, http.Header{"X-Upstream-Https-Proxy": []string{"myproxy.com"}})
first_client, err := proxyClientWithConnectHeaders(proxy.URL, http.Header{"X-Upstream-Https-Proxy": []string{"https://myproxy.com"}})
r.NoError(err)

first_req, err := http.NewRequest("GET", remote.URL, nil)
r.NoError(err)

first_client.Do(first_req)

second_client, err := proxyClientWithConnectHeaders(proxy.URL, http.Header{"X-Upstream-Https-Proxy": []string{"myproxy2.com"}})
second_client, err := proxyClientWithConnectHeaders(proxy.URL, http.Header{"X-Upstream-Https-Proxy": []string{"https://myproxy2.com"}})
r.NoError(err)

second_req, err := http.NewRequest("GET", remote.URL, nil)
Expand Down
Loading