-
Notifications
You must be signed in to change notification settings - Fork 415
/
httptrace_test.go
347 lines (329 loc) · 9.52 KB
/
httptrace_test.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package httptrace
import (
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"inet.af/netaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
)
func TestStartRequestSpan(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
r := httptest.NewRequest(http.MethodGet, "/somePath", nil)
s, _ := StartRequestSpan(r)
s.Finish()
spans := mt.FinishedSpans()
require.Len(t, spans, 1)
assert.Equal(t, "example.com", spans[0].Tag("http.host"))
}
type IPTestCase struct {
name string
remoteAddr string
headers map[string]string
expectedIP netaddr.IP
multiHeaders string
clientIPHeader string
}
func genIPTestCases() []IPTestCase {
ipv4Global := randGlobalIPv4().String()
ipv6Global := randGlobalIPv6().String()
ipv4Private := randPrivateIPv4().String()
ipv6Private := randPrivateIPv6().String()
tcs := []IPTestCase{}
// Simple ipv4 test cases over all headers
for _, header := range defaultIPHeaders {
tcs = append(tcs, IPTestCase{
name: "ipv4-global." + header,
headers: map[string]string{header: ipv4Global},
expectedIP: netaddr.MustParseIP(ipv4Global),
})
tcs = append(tcs, IPTestCase{
name: "ipv4-private." + header,
headers: map[string]string{header: ipv4Private},
expectedIP: netaddr.IP{},
})
}
// Simple ipv6 test cases over all headers
for _, header := range defaultIPHeaders {
tcs = append(tcs, IPTestCase{
name: "ipv6-global." + header,
headers: map[string]string{header: ipv6Global},
expectedIP: netaddr.MustParseIP(ipv6Global),
})
tcs = append(tcs, IPTestCase{
name: "ipv6-private." + header,
headers: map[string]string{header: ipv6Private},
expectedIP: netaddr.IP{},
})
}
// private and global in same header
tcs = append([]IPTestCase{
{
name: "ipv4-private+global",
headers: map[string]string{"x-forwarded-for": ipv4Private + "," + ipv4Global},
expectedIP: netaddr.MustParseIP(ipv4Global),
},
{
name: "ipv4-global+private",
headers: map[string]string{"x-forwarded-for": ipv4Global + "," + ipv4Private},
expectedIP: netaddr.MustParseIP(ipv4Global),
},
{
name: "ipv6-private+global",
headers: map[string]string{"x-forwarded-for": ipv6Private + "," + ipv6Global},
expectedIP: netaddr.MustParseIP(ipv6Global),
},
{
name: "ipv6-global+private",
headers: map[string]string{"x-forwarded-for": ipv6Global + "," + ipv6Private},
expectedIP: netaddr.MustParseIP(ipv6Global),
},
}, tcs...)
// Invalid IPs (or a mix of valid/invalid over a single or multiple headers)
tcs = append([]IPTestCase{
{
name: "invalid-ipv4",
headers: map[string]string{"x-forwarded-for": "127..0.0.1"},
expectedIP: netaddr.IP{},
},
{
name: "invalid-ipv4-recover",
headers: map[string]string{"x-forwarded-for": "127..0.0.1, " + ipv4Global},
expectedIP: netaddr.MustParseIP(ipv4Global),
},
{
name: "ipv4-multi-header-1",
headers: map[string]string{"x-forwarded-for": "127.0.0.1", "forwarded-for": ipv4Global},
expectedIP: netaddr.IP{},
multiHeaders: "x-forwarded-for,forwarded-for",
},
{
name: "ipv4-multi-header-2",
headers: map[string]string{"forwarded-for": ipv4Global, "x-forwarded-for": "127.0.0.1"},
expectedIP: netaddr.IP{},
multiHeaders: "x-forwarded-for,forwarded-for",
},
{
name: "invalid-ipv6",
headers: map[string]string{"x-forwarded-for": "2001:0db8:2001:zzzz::"},
expectedIP: netaddr.IP{},
},
{
name: "invalid-ipv6-recover",
headers: map[string]string{"x-forwarded-for": "2001:0db8:2001:zzzz::, " + ipv6Global},
expectedIP: netaddr.MustParseIP(ipv6Global),
},
{
name: "ipv6-multi-header-1",
headers: map[string]string{"x-forwarded-for": "2001:0db8:2001:zzzz::", "forwarded-for": ipv6Global},
expectedIP: netaddr.IP{},
multiHeaders: "x-forwarded-for,forwarded-for",
},
{
name: "ipv6-multi-header-2",
headers: map[string]string{"forwarded-for": ipv6Global, "x-forwarded-for": "2001:0db8:2001:zzzz::"},
expectedIP: netaddr.IP{},
multiHeaders: "x-forwarded-for,forwarded-for",
},
}, tcs...)
tcs = append([]IPTestCase{
{
name: "no-headers",
expectedIP: netaddr.IP{},
},
{
name: "header-case",
expectedIP: netaddr.MustParseIP(ipv4Global),
headers: map[string]string{"X-fOrWaRdEd-FoR": ipv4Global},
},
{
name: "user-header",
expectedIP: netaddr.MustParseIP(ipv4Global),
headers: map[string]string{"x-forwarded-for": ipv6Global, "custom-header": ipv4Global},
clientIPHeader: "custom-header",
},
{
name: "user-header-not-found",
expectedIP: netaddr.IP{},
headers: map[string]string{"x-forwarded-for": ipv4Global},
clientIPHeader: "custom-header",
},
}, tcs...)
return tcs
}
func TestIPHeaders(t *testing.T) {
// Make sure to restore the real value of cfg.clientIPHeader at the end of the test
defer func(s string) { cfg.clientIPHeader = s }(cfg.clientIPHeader)
for _, tc := range genIPTestCases() {
t.Run(tc.name, func(t *testing.T) {
header := http.Header{}
for k, v := range tc.headers {
header.Add(k, v)
}
r := http.Request{Header: header, RemoteAddr: tc.remoteAddr}
cfg.clientIPHeader = tc.clientIPHeader
spanCfg := ddtrace.StartSpanConfig{}
for _, opt := range genClientIPSpanTags(&r) {
opt(&spanCfg)
}
if tc.expectedIP.IsValid() {
require.Equal(t, tc.expectedIP.String(), spanCfg.Tags[ext.HTTPClientIP])
require.Nil(t, spanCfg.Tags[multipleIPHeaders])
} else {
require.Nil(t, spanCfg.Tags[ext.HTTPClientIP])
if tc.multiHeaders != "" {
require.Equal(t, tc.multiHeaders, spanCfg.Tags[multipleIPHeaders])
for hdr, ip := range tc.headers {
require.Equal(t, ip, spanCfg.Tags[ext.HTTPRequestHeaders+"."+hdr])
}
}
}
})
}
}
func TestURLTag(t *testing.T) {
type URLTestCase struct {
name, expectedURL, host, port, path, query, fragment string
}
for _, tc := range []URLTestCase{
{
name: "no-host",
expectedURL: "/test",
path: "/test",
},
{
name: "basic",
expectedURL: "http://example.com",
host: "example.com",
},
{
name: "basic-path",
expectedURL: "http://example.com/test",
host: "example.com",
path: "/test",
},
{
name: "basic-port",
expectedURL: "http://example.com:8080",
host: "example.com",
port: "8080",
},
{
name: "basic-fragment",
expectedURL: "http://example.com#test",
host: "example.com",
fragment: "test",
},
{
name: "query1",
expectedURL: "http://example.com?test1=test2",
host: "example.com",
query: "test1=test2",
},
{
name: "query2",
expectedURL: "http://example.com?test1=test2&test3=test4",
host: "example.com",
query: "test1=test2&test3=test4",
},
{
name: "combined",
expectedURL: "http://example.com:7777/test?test1=test2&test3=test4#test",
host: "example.com",
path: "/test",
query: "test1=test2&test3=test4",
port: "7777",
fragment: "test",
},
{
name: "basic-obfuscation1",
expectedURL: "http://example.com?<redacted>",
host: "example.com",
query: "token=value",
},
{
name: "basic-obfuscation2",
expectedURL: "http://example.com?test0=test1&<redacted>&test1=test2",
host: "example.com",
query: "test0=test1&token=value&test1=test2",
},
{
name: "combined-obfuscation",
expectedURL: "http://example.com:7777/test?test1=test2&<redacted>&test3=test4#test",
host: "example.com",
path: "/test",
query: "test1=test2&token=value&test3=test4",
port: "7777",
fragment: "test",
},
} {
t.Run(tc.name, func(t *testing.T) {
r := http.Request{
URL: &url.URL{
Path: tc.path,
RawQuery: tc.query,
Fragment: tc.fragment,
},
Host: tc.host,
}
if tc.port != "" {
r.Host += ":" + tc.port
}
url := urlFromRequest(&r)
require.Equal(t, tc.expectedURL, url)
})
}
}
func randIPv4() netaddr.IP {
return netaddr.IPv4(uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()))
}
func randIPv6() netaddr.IP {
return netaddr.IPv6Raw([16]byte{
uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()),
uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()),
uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()),
uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()), uint8(rand.Uint32()),
})
}
func randGlobalIPv4() netaddr.IP {
for {
ip := randIPv4()
if isGlobal(ip) {
return ip
}
}
}
func randGlobalIPv6() netaddr.IP {
for {
ip := randIPv6()
if isGlobal(ip) {
return ip
}
}
}
func randPrivateIPv4() netaddr.IP {
for {
ip := randIPv4()
if !isGlobal(ip) && ip.IsPrivate() {
return ip
}
}
}
func randPrivateIPv6() netaddr.IP {
for {
ip := randIPv6()
if !isGlobal(ip) && ip.IsPrivate() {
return ip
}
}
}