1
- package network_test
1
+ package network
2
2
3
3
import (
4
4
"context"
5
5
"net/http"
6
6
"net/http/httptest"
7
+ "net/url"
8
+ "strconv"
7
9
"testing"
8
10
"time"
9
11
12
+ "github.com/doyensec/safeurl"
10
13
"github.com/stretchr/testify/require"
11
14
12
15
"github.com/smartcontractkit/chainlink-common/pkg/logger"
13
- "github.com/smartcontractkit/chainlink/v2/core/services/gateway/network"
14
16
)
15
17
16
18
func TestHTTPClient_Send (t * testing.T ) {
17
19
t .Parallel ()
18
20
19
21
// Setup the test environment
20
22
lggr := logger .Test (t )
21
- config := network. HTTPClientConfig {
23
+ config := HTTPClientConfig {
22
24
MaxResponseBytes : 1024 ,
23
25
DefaultTimeout : 5 * time .Second ,
24
26
}
25
- client , err := network .NewHTTPClient (config , lggr )
26
- require .NoError (t , err )
27
27
28
28
// Define test cases
29
29
tests := []struct {
30
30
name string
31
31
setupServer func () * httptest.Server
32
- request network. HTTPRequest
32
+ request HTTPRequest
33
33
expectedError error
34
- expectedResp * network. HTTPResponse
34
+ expectedResp * HTTPResponse
35
35
}{
36
36
{
37
37
name : "successful request" ,
@@ -42,15 +42,15 @@ func TestHTTPClient_Send(t *testing.T) {
42
42
require .NoError (t , err2 )
43
43
}))
44
44
},
45
- request : network. HTTPRequest {
45
+ request : HTTPRequest {
46
46
Method : "GET" ,
47
47
URL : "/" ,
48
48
Headers : map [string ]string {},
49
49
Body : nil ,
50
50
Timeout : 2 * time .Second ,
51
51
},
52
52
expectedError : nil ,
53
- expectedResp : & network. HTTPResponse {
53
+ expectedResp : & HTTPResponse {
54
54
StatusCode : http .StatusOK ,
55
55
Headers : map [string ]string {"Content-Length" : "7" },
56
56
Body : []byte ("success" ),
@@ -66,7 +66,7 @@ func TestHTTPClient_Send(t *testing.T) {
66
66
require .NoError (t , err2 )
67
67
}))
68
68
},
69
- request : network. HTTPRequest {
69
+ request : HTTPRequest {
70
70
Method : "GET" ,
71
71
URL : "/" ,
72
72
Headers : map [string ]string {},
@@ -85,15 +85,15 @@ func TestHTTPClient_Send(t *testing.T) {
85
85
require .NoError (t , err2 )
86
86
}))
87
87
},
88
- request : network. HTTPRequest {
88
+ request : HTTPRequest {
89
89
Method : "GET" ,
90
90
URL : "/" ,
91
91
Headers : map [string ]string {},
92
92
Body : nil ,
93
93
Timeout : 2 * time .Second ,
94
94
},
95
95
expectedError : nil ,
96
- expectedResp : & network. HTTPResponse {
96
+ expectedResp : & HTTPResponse {
97
97
StatusCode : http .StatusInternalServerError ,
98
98
Headers : map [string ]string {"Content-Length" : "5" },
99
99
Body : []byte ("error" ),
@@ -108,7 +108,7 @@ func TestHTTPClient_Send(t *testing.T) {
108
108
require .NoError (t , err2 )
109
109
}))
110
110
},
111
- request : network. HTTPRequest {
111
+ request : HTTPRequest {
112
112
Method : "GET" ,
113
113
URL : "/" ,
114
114
Headers : map [string ]string {},
@@ -126,6 +126,26 @@ func TestHTTPClient_Send(t *testing.T) {
126
126
server := tt .setupServer ()
127
127
defer server .Close ()
128
128
129
+ u , err := url .Parse (server .URL )
130
+ require .NoError (t , err )
131
+
132
+ hostname , port := u .Hostname (), u .Port ()
133
+ portInt , err := strconv .ParseInt (port , 10 , 32 )
134
+ require .NoError (t , err )
135
+
136
+ safeConfig := safeurl .
137
+ GetConfigBuilder ().
138
+ SetTimeout (config .DefaultTimeout ).
139
+ SetAllowedIPs (hostname ).
140
+ SetAllowedPorts (int (portInt )).
141
+ Build ()
142
+
143
+ client := & httpClient {
144
+ config : config ,
145
+ client : safeurl .Client (safeConfig ),
146
+ lggr : lggr ,
147
+ }
148
+
129
149
tt .request .URL = server .URL + tt .request .URL
130
150
131
151
resp , err := client .Send (context .Background (), tt .request )
@@ -145,3 +165,100 @@ func TestHTTPClient_Send(t *testing.T) {
145
165
})
146
166
}
147
167
}
168
+
169
+ func TestHTTPClient_BlocksUnallowed (t * testing.T ) {
170
+ t .Parallel ()
171
+
172
+ // Setup the test environment
173
+ lggr := logger .Test (t )
174
+ config := HTTPClientConfig {
175
+ MaxResponseBytes : 1024 ,
176
+ DefaultTimeout : 5 * time .Second ,
177
+ }
178
+
179
+ client , err := NewHTTPClient (config , lggr )
180
+ require .NoError (t , err )
181
+
182
+ // Define test cases
183
+ tests := []struct {
184
+ name string
185
+ request HTTPRequest
186
+ expectedError string
187
+ }{
188
+ {
189
+ name : "blocked port" ,
190
+ request : HTTPRequest {
191
+ Method : "GET" ,
192
+ URL : "http://127.0.0.1:8080" ,
193
+ Headers : map [string ]string {},
194
+ Body : nil ,
195
+ Timeout : 2 * time .Second ,
196
+ },
197
+ expectedError : "port: 8080 not found in allowlist" ,
198
+ },
199
+ {
200
+ name : "blocked scheme" ,
201
+ request : HTTPRequest {
202
+ Method : "GET" ,
203
+ URL : "file://127.0.0.1" ,
204
+ Headers : map [string ]string {},
205
+ Body : nil ,
206
+ Timeout : 2 * time .Second ,
207
+ },
208
+ expectedError : "scheme: file not found in allowlist" ,
209
+ },
210
+ {
211
+ name : "explicitly blocked IP" ,
212
+ request : HTTPRequest {
213
+ Method : "GET" ,
214
+ URL : "http://169.254.0.1" ,
215
+ Headers : map [string ]string {},
216
+ Body : nil ,
217
+ Timeout : 2 * time .Second ,
218
+ },
219
+ expectedError : "ip: 169.254.0.1 not found in allowlist" ,
220
+ },
221
+ {
222
+ name : "explicitly blocked IP - internal network" ,
223
+ request : HTTPRequest {
224
+ Method : "GET" ,
225
+ URL : "http://169.254.0.1/endpoint" ,
226
+ Headers : map [string ]string {},
227
+ Body : nil ,
228
+ Timeout : 2 * time .Second ,
229
+ },
230
+ expectedError : "ip: 169.254.0.1 not found in allowlist" ,
231
+ },
232
+ {
233
+ name : "explicitly blocked IP - localhost" ,
234
+ request : HTTPRequest {
235
+ Method : "GET" ,
236
+ URL : "http://127.0.0.1/endpoint" ,
237
+ Headers : map [string ]string {},
238
+ Body : nil ,
239
+ Timeout : 2 * time .Second ,
240
+ },
241
+ expectedError : "ip: 127.0.0.1 not found in allowlist" ,
242
+ },
243
+ {
244
+ name : "explicitly blocked IP - current network" ,
245
+ request : HTTPRequest {
246
+ Method : "GET" ,
247
+ URL : "http://0.0.0.0/endpoint" ,
248
+ Headers : map [string ]string {},
249
+ Body : nil ,
250
+ Timeout : 2 * time .Second ,
251
+ },
252
+ expectedError : "ip: 0.0.0.0 not found in allowlist" ,
253
+ },
254
+ }
255
+
256
+ // Execute test cases
257
+ for _ , tt := range tests {
258
+ t .Run (tt .name , func (t * testing.T ) {
259
+ _ , err := client .Send (context .Background (), tt .request )
260
+ require .Error (t , err )
261
+ require .ErrorContains (t , err , tt .expectedError )
262
+ })
263
+ }
264
+ }
0 commit comments