Skip to content

Commit fa622d3

Browse files
committed
check that when used, proxy url starts with http and do proxy hostname dns lookup to confirm connectivity
1 parent bbcffa9 commit fa622d3

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pkg/cli/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"net/http"
78
"net/url"
89
"os"
@@ -410,6 +411,15 @@ func (config *BackplaneConfiguration) testHTTPRequestToBackplaneAPI() (bool, err
410411
if err != nil {
411412
return false, err
412413
}
414+
scheme := proxyURL.Scheme
415+
if scheme != "http" && scheme != "https" {
416+
return false, fmt.Errorf("proxy URL scheme must be http or https, got: %s", scheme)
417+
}
418+
hostname := proxyURL.Hostname()
419+
_, err = net.LookupHost(hostname)
420+
if err != nil {
421+
return false, fmt.Errorf("DNS lookup failed for proxy hostname '%s': %v (check network connectivity or VPN if required)", hostname, err)
422+
}
413423
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
414424
}
415425

pkg/cli/config/config_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http/httptest"
88
"net/url"
99
"os"
10+
"strings"
1011
"testing"
1112

1213
"github.com/spf13/viper"
@@ -85,8 +86,55 @@ func TestGetBackplaneConnection(t *testing.T) {
8586
t.Failed()
8687
}
8788
})
89+
90+
t.Run("should pass for valid http proxy URL", func(t *testing.T) {
91+
proxyURL := "http://www.example.com"
92+
config := BackplaneConfiguration{URL: "https://api.example.com", ProxyURL: &proxyURL}
93+
_, err := config.testHTTPRequestToBackplaneAPI()
94+
95+
// Should not get scheme validation error (DNS lookup error is expected)
96+
if err != nil && strings.Contains(err.Error(), "proxy URL scheme must be http or https") {
97+
t.Errorf("unexpected scheme validation error: %v", err)
98+
}
99+
})
100+
101+
t.Run("should pass for valid https proxy URL", func(t *testing.T) {
102+
proxyURL := "https://www.example.com"
103+
config := BackplaneConfiguration{URL: "https://api.example.com", ProxyURL: &proxyURL}
104+
_, err := config.testHTTPRequestToBackplaneAPI()
105+
106+
// Should not get scheme validation error (DNS lookup error is expected)
107+
if err != nil && strings.Contains(err.Error(), "proxy URL scheme must be http or https") {
108+
t.Errorf("unexpected scheme validation error: %v", err)
109+
}
110+
})
111+
112+
t.Run("should fail for proxy URL without scheme", func(t *testing.T) {
113+
proxyURL := "www.example.com"
114+
config := BackplaneConfiguration{URL: "https://api.example.com", ProxyURL: &proxyURL}
115+
_, err := config.testHTTPRequestToBackplaneAPI()
116+
117+
if err == nil {
118+
t.Errorf("expected error but got none")
119+
} else if !strings.Contains(err.Error(), "proxy URL scheme must be http or https, got:") {
120+
t.Errorf("expected scheme validation error, got: %s", err.Error())
121+
}
122+
})
123+
124+
t.Run("should fail for ftp proxy URL", func(t *testing.T) {
125+
proxyURL := "ftp://www.example.com"
126+
config := BackplaneConfiguration{URL: "https://api.example.com", ProxyURL: &proxyURL}
127+
_, err := config.testHTTPRequestToBackplaneAPI()
128+
129+
if err == nil {
130+
t.Errorf("expected error but got none")
131+
} else if !strings.Contains(err.Error(), "proxy URL scheme must be http or https, got: ftp") {
132+
t.Errorf("expected scheme validation error for ftp, got: %s", err.Error())
133+
}
134+
})
88135
}
89136

137+
90138
func TestBackplaneConfiguration_getFirstWorkingProxyURL(t *testing.T) {
91139
tests := []struct {
92140
name string

0 commit comments

Comments
 (0)