-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
233 lines (220 loc) · 7.37 KB
/
Copy pathproxy_test.go
File metadata and controls
233 lines (220 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package netquality
import (
"bufio"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/json"
"io"
"math/big"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/korya/netquality/server"
)
func testCert(t *testing.T, issuerOrg string, withSCT bool) *x509.Certificate {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
// Self-signed: the issuer is the subject.
Subject: pkix.Name{CommonName: "example.test", Organization: []string{issuerOrg}},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
}
if withSCT {
tmpl.ExtraExtensions = []pkix.Extension{{Id: asn1.ObjectIdentifier(sctExtensionOID), Value: []byte{0x04, 0x00}}}
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
c, err := x509.ParseCertificate(der)
if err != nil {
t.Fatal(err)
}
return c
}
func TestInspectChain(t *testing.T) {
public := testCert(t, "Public CA", true)
private := testCert(t, "Corp Root", false)
vendor := testCert(t, "Zscaler Inc.", false)
tests := []struct {
name string
cs tls.ConnectionState
want bool
contains string
}{
{"public leaf with SCT", tls.ConnectionState{PeerCertificates: []*x509.Certificate{public}, VerifiedChains: [][]*x509.Certificate{{public}}}, false, ""},
{"private CA, verified", tls.ConnectionState{PeerCertificates: []*x509.Certificate{private}, VerifiedChains: [][]*x509.Certificate{{private}}}, true, "private CA"},
{"inspection vendor", tls.ConnectionState{PeerCertificates: []*x509.Certificate{vendor}, VerifiedChains: [][]*x509.Certificate{{vendor}}}, true, "TLS-inspection product"},
{"verification skipped (insecure)", tls.ConnectionState{PeerCertificates: []*x509.Certificate{private}}, false, ""},
{"SCTs via TLS extension", tls.ConnectionState{PeerCertificates: []*x509.Certificate{private}, VerifiedChains: [][]*x509.Certificate{{private}}, SignedCertificateTimestamps: [][]byte{{1}}}, false, ""},
{"empty", tls.ConnectionState{}, false, ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := inspectChain(tc.cs)
if (got != nil) != tc.want {
t.Fatalf("got %+v, want detection=%v", got, tc.want)
}
if got != nil && (!got.TLSInterception || !strings.Contains(got.Reason, tc.contains) || got.Issuer == "") {
t.Errorf("%+v", got)
}
})
}
}
// startConnectProxy runs a minimal HTTP CONNECT proxy and returns its URL and a
// counter of tunnelled connections.
func startConnectProxy(t *testing.T) (string, *atomic.Int32) {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ln.Close() })
hits := new(atomic.Int32)
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer func() { _ = c.Close() }()
req, err := http.ReadRequest(bufio.NewReader(c))
if err != nil || req.Method != http.MethodConnect {
return
}
up, err := net.Dial("tcp", req.Host)
if err != nil {
return
}
defer func() { _ = up.Close() }()
hits.Add(1)
_, _ = io.WriteString(c, "HTTP/1.1 200 Connection established\r\n\r\n")
go func() { _, _ = io.Copy(up, c) }()
_, _ = io.Copy(c, up)
}()
}
}()
return "http://user:secret@" + ln.Addr().String(), hits
}
func quickOpts(client *http.Client) Options {
return Options{HTTPClient: client, Directions: Download, IdleProbes: 2,
MaxDuration: 400 * time.Millisecond, MaxBytes: 1 << 40, Stability: fastStability()}
}
func TestExplicitProxyDetected(t *testing.T) {
srv := httptest.NewUnstartedServer(server.Handler(server.Options{TestEndpoint: "nq.example.test"}))
srv.EnableHTTP2 = true
srv.StartTLS()
defer srv.Close()
proxyURL, hits := startConnectProxy(t)
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.Proxy = http.ProxyURL(mustParse(t, proxyURL))
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // test server
res, err := Run(context.Background(), Target{ConfigURL: srv.URL + server.ConfigPath}, quickOpts(&http.Client{Transport: tr}))
if err != nil {
t.Fatal(err)
}
p := res.Target.Proxy
if p == nil || !p.Explicit || p.TLSInterception {
t.Fatalf("proxy = %+v", p)
}
if strings.Contains(p.URL, "secret") || !strings.HasSuffix(p.URL, strings.TrimPrefix(proxyURL, "http://user:secret@")) {
t.Errorf("url = %q", p.URL)
}
if hits.Load() == 0 {
t.Error("proxy was not used")
}
if res.Download.HTTPVersion != "HTTP/2.0" {
t.Errorf("http version through proxy = %q", res.Download.HTTPVersion)
}
if len(res.Target.LocalIPs) != 1 || res.Target.LocalIPs[0] != "127.0.0.1" {
t.Errorf("local_ips via proxy = %v", res.Target.LocalIPs)
}
var sawProxy, sawEndpoint bool
for _, w := range res.Warnings {
sawProxy = sawProxy || strings.Contains(w, "explicit proxy")
sawEndpoint = sawEndpoint || strings.Contains(w, "test_endpoint")
}
if !sawProxy || !sawEndpoint {
t.Errorf("warnings = %v", res.Warnings)
}
}
func TestTLSInterceptionDetected(t *testing.T) {
srv := httptest.NewUnstartedServer(server.Handler(server.Options{}))
srv.EnableHTTP2 = true
srv.StartTLS()
defer srv.Close()
// Trust the server's self-signed cert like a corporate root: the chain
// verifies, but the leaf carries no SCTs.
pool := x509.NewCertPool()
pool.AddCert(srv.Certificate())
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}
res, err := Run(context.Background(), Target{ConfigURL: srv.URL + server.ConfigPath}, quickOpts(&http.Client{Transport: tr}))
if err != nil {
t.Fatal(err)
}
p := res.Target.Proxy
if p == nil || !p.TLSInterception || p.Explicit || !strings.Contains(p.Issuer, "Acme") {
t.Fatalf("proxy = %+v", p)
}
if len(res.Warnings) == 0 || !strings.Contains(res.Warnings[0], "TLS interception") {
t.Errorf("warnings = %v", res.Warnings)
}
}
func TestInsecureDoesNotFlagProxy(t *testing.T) {
target, client := newTestServer(t, server.Options{})
res, err := Run(context.Background(), target, quickOpts(client))
if err != nil {
t.Fatal(err)
}
if res.Target.Proxy != nil {
t.Errorf("proxy = %+v", res.Target.Proxy)
}
data, _ := json.Marshal(res)
if strings.Contains(string(data), `"proxy"`) {
t.Error("proxy key must be omitted when nil")
}
data, _ = json.Marshal(ResolvedTarget{Proxy: &ProxyInfo{Explicit: true, URL: "http://p:1"}})
if !strings.Contains(string(data), `"proxy":{"explicit":true,"url":"http://p:1","tls_interception":false}`) {
t.Errorf("json = %s", data)
}
}
func TestExplicitProxyNilWithoutProxy(t *testing.T) {
f := &transportFactory{base: &http.Transport{Proxy: nil}}
if f.explicitProxy("https://example.test/") != nil {
t.Error("nil Proxy func must yield nil")
}
f = &transportFactory{custom: http.DefaultTransport}
if f.explicitProxy("https://example.test/") != nil {
t.Error("custom RoundTripper must yield nil")
}
f = &transportFactory{base: &http.Transport{Proxy: func(*http.Request) (*url.URL, error) { return nil, nil }}}
if f.explicitProxy("https://example.test/") != nil {
t.Error("DIRECT must yield nil")
}
}
func mustParse(t *testing.T, s string) *url.URL {
t.Helper()
u, err := url.Parse(s)
if err != nil {
t.Fatal(err)
}
return u
}