forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotected_fetch_client_test.go
More file actions
317 lines (281 loc) · 9.25 KB
/
Copy pathprotected_fetch_client_test.go
File metadata and controls
317 lines (281 loc) · 9.25 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
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
package service
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/setting/system_setting"
"github.com/stretchr/testify/require"
)
type staticSSRFResolver map[string][]net.IPAddr
func (r staticSSRFResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
if ips, ok := r[host]; ok {
return ips, nil
}
return nil, fmt.Errorf("unexpected lookup for %s", host)
}
func staticProtection(protection *common.SSRFProtection) func() (*common.SSRFProtection, bool, error) {
return func() (*common.SSRFProtection, bool, error) {
return protection, true, nil
}
}
func testConn(t *testing.T) net.Conn {
t.Helper()
clientConn, serverConn := net.Pipe()
t.Cleanup(func() {
clientConn.Close()
serverConn.Close()
})
return clientConn
}
func configureSSRFTestFetchSetting(t *testing.T) {
t.Helper()
fetchSetting := system_setting.GetFetchSetting()
original := *fetchSetting
t.Cleanup(func() {
*fetchSetting = original
})
fetchSetting.EnableSSRFProtection = true
fetchSetting.AllowPrivateIp = false
fetchSetting.DomainFilterMode = false
fetchSetting.IpFilterMode = false
fetchSetting.DomainList = nil
fetchSetting.IpList = nil
fetchSetting.AllowedPorts = []string{"80", "443"}
fetchSetting.ApplyIPFilterForDomain = true
}
func mustParseURL(t *testing.T, rawURL string) *url.URL {
t.Helper()
parsedURL, err := url.Parse(rawURL)
require.NoError(t, err)
return parsedURL
}
func TestProtectedFetchDialerRejectsPrivateReboundAddress(t *testing.T) {
dialer := &protectedFetchDialer{
resolver: staticSSRFResolver{
"safe.example": {{IP: net.ParseIP("127.0.0.1")}},
},
dialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
t.Fatalf("dialContext should not be called for blocked address %s", address)
return nil, nil
},
getProtection: staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "safe.example:80")
require.Error(t, err)
require.Nil(t, conn)
require.Contains(t, err.Error(), "private IP address not allowed")
}
func TestProtectedFetchDialerRejectsMixedResolvedIPs(t *testing.T) {
var dialed []string
dialer := &protectedFetchDialer{
resolver: staticSSRFResolver{
"safe.example": {
{IP: net.ParseIP("10.0.0.1")},
{IP: net.ParseIP("8.8.8.8")},
},
},
dialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return testConn(t), nil
},
getProtection: staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "safe.example:443")
require.Error(t, err)
require.Nil(t, conn)
require.Empty(t, dialed)
require.Contains(t, err.Error(), "private IP address not allowed")
}
func TestProtectedFetchDialerDialsWhenAllResolvedIPsAllowed(t *testing.T) {
var dialed []string
dialer := &protectedFetchDialer{
resolver: staticSSRFResolver{
"safe.example": {
{IP: net.ParseIP("8.8.8.8")},
{IP: net.ParseIP("1.1.1.1")},
},
},
dialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return testConn(t), nil
},
getProtection: staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "safe.example:443")
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, []string{"8.8.8.8:443"}, dialed)
}
func TestProtectedFetchDialerAllowsPrivateIPWhenWhitelisted(t *testing.T) {
var dialed []string
dialer := &protectedFetchDialer{
resolver: staticSSRFResolver{
"internal.example": {{IP: net.ParseIP("10.1.2.3")}},
},
dialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return testConn(t), nil
},
getProtection: staticProtection(&common.SSRFProtection{
AllowPrivateIp: true,
DomainFilterMode: false,
IpFilterMode: true,
IpList: []string{"10.0.0.0/8"},
ApplyIPFilterForDomain: true,
}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "internal.example:80")
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, []string{"10.1.2.3:80"}, dialed)
}
func TestProtectedFetchDialerSkipsResolvedIPCheckWhenDisabled(t *testing.T) {
var dialed []string
dialer := &protectedFetchDialer{
resolver: staticSSRFResolver{},
dialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return testConn(t), nil
},
getProtection: staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: false,
}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "safe.example:80")
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, []string{"safe.example:80"}, dialed)
}
func TestGetSSRFProtectedHTTPClientFallsBackToDefaultClientWhenProtectionDisabled(t *testing.T) {
fetchSetting := system_setting.GetFetchSetting()
originalFetchSetting := *fetchSetting
originalHTTPClient := httpClient
originalProtectedClient := ssrfProtectedHTTPClient
t.Cleanup(func() {
*fetchSetting = originalFetchSetting
httpClient = originalHTTPClient
ssrfProtectedHTTPClient = originalProtectedClient
})
fetchSetting.EnableSSRFProtection = false
expected := &http.Client{}
httpClient = expected
ssrfProtectedHTTPClient = &http.Client{}
require.Same(t, expected, GetSSRFProtectedHTTPClient())
}
func TestProtectedFetchRoundTripperUsesConfiguredProxy(t *testing.T) {
configureSSRFTestFetchSetting(t)
proxyURL := mustParseURL(t, "http://127.0.0.1:3128")
var dialed []string
client := newProtectedFetchHTTPClientWithProxy(
staticSSRFResolver{},
func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return nil, errors.New("stop after proxy dial")
},
staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
func(req *http.Request) (*url.URL, error) {
return proxyURL, nil
},
)
req, err := http.NewRequest(http.MethodGet, "http://93.184.216.34/resource", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, []string{"127.0.0.1:3128"}, dialed)
}
func TestProtectedFetchRoundTripperRejectsPrivateTargetBeforeProxy(t *testing.T) {
configureSSRFTestFetchSetting(t)
proxyURL := mustParseURL(t, "http://127.0.0.1:3128")
var dialed []string
client := newProtectedFetchHTTPClientWithProxy(
staticSSRFResolver{},
func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return nil, errors.New("proxy should not be dialed")
},
staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
func(req *http.Request) (*url.URL, error) {
return proxyURL, nil
},
)
req, err := http.NewRequest(http.MethodGet, "http://localhost/resource", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.Error(t, err)
require.Nil(t, resp)
require.Contains(t, err.Error(), "private IP address not allowed")
require.Empty(t, dialed)
}
func TestProtectedFetchRoundTripperNoProxyUsesProtectedDialer(t *testing.T) {
configureSSRFTestFetchSetting(t)
var dialed []string
client := newProtectedFetchHTTPClientWithProxy(
staticSSRFResolver{},
func(ctx context.Context, network, address string) (net.Conn, error) {
dialed = append(dialed, address)
return nil, errors.New("unexpected direct dial")
},
staticProtection(&common.SSRFProtection{
AllowPrivateIp: false,
DomainFilterMode: false,
IpFilterMode: false,
ApplyIPFilterForDomain: true,
}),
func(req *http.Request) (*url.URL, error) {
return nil, nil
},
)
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1/resource", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.Error(t, err)
require.Nil(t, resp)
require.Contains(t, err.Error(), "private IP address not allowed")
require.Empty(t, dialed)
}
func TestProtectedFetchRoundTripperReusesTransportPerProxy(t *testing.T) {
client := newProtectedFetchHTTPClientWithDialer(nil, nil, nil)
roundTripper, ok := client.Transport.(*ssrfProtectedRoundTripper)
require.True(t, ok)
direct := roundTripper.transportFor(nil)
directAgain := roundTripper.transportFor(nil)
proxied := roundTripper.transportFor(mustParseURL(t, "http://127.0.0.1:3128"))
require.Same(t, direct, directAgain)
require.NotSame(t, direct, proxied)
require.True(t, direct.ForceAttemptHTTP2)
require.False(t, direct.DisableKeepAlives)
}