forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
source_http_test.go
executable file
·485 lines (377 loc) · 13.6 KB
/
source_http_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const fixtureImage = "testdata/large.jpg"
const fixture1024Bytes = "testdata/1024bytes"
func TestHttpImageSource(t *testing.T) {
var body []byte
var err error
buf, _ := ioutil.ReadFile(fixtureImage)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(buf)
}))
defer ts.Close()
source := NewHTTPImageSource(&SourceConfig{})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err = source.GetImage(r)
if err != nil {
t.Fatalf("Error while reading the body: %s", err)
}
_, _ = w.Write(body)
}
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
if len(body) != len(buf) {
t.Error("Invalid response body")
}
}
func TestHttpImageSourceAllowedOrigin(t *testing.T) {
buf, _ := ioutil.ReadFile(fixtureImage)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(buf)
}))
defer ts.Close()
origin, _ := url.Parse(ts.URL)
origins := []*url.URL{origin}
source := NewHTTPImageSource(&SourceConfig{AllowedOrigins: origins})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err := source.GetImage(r)
if err != nil {
t.Fatalf("Error while reading the body: %s", err)
}
_, _ = w.Write(body)
if len(body) != len(buf) {
t.Error("Invalid response body length")
}
}
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestHttpImageSourceNotAllowedOrigin(t *testing.T) {
origin, _ := url.Parse("http://foo")
origins := []*url.URL{origin}
source := NewHTTPImageSource(&SourceConfig{AllowedOrigins: origins})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
_, err := source.GetImage(r)
if err == nil {
t.Fatal("Error cannot be empty")
}
if err.Error() != "not allowed remote URL origin: bar.com" {
t.Fatalf("Invalid error message: %s", err)
}
}
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url=http://bar.com", nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestHttpImageSourceForwardAuthHeader(t *testing.T) {
cases := []string{
"X-Forward-Authorization",
"Authorization",
}
for _, header := range cases {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url=http://bar.com", nil)
r.Header.Set(header, "foobar")
source := &HTTPImageSource{&SourceConfig{AuthForwarding: true}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := &http.Request{Header: make(http.Header)}
source.setAuthorizationHeader(oreq, r)
if oreq.Header.Get("Authorization") != "foobar" {
t.Fatal("Missmatch Authorization header")
}
}
}
func TestHttpImageSourceForwardHeaders(t *testing.T) {
cases := []string{
"X-Custom",
"X-Token",
}
for _, header := range cases {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url=http://bar.com", nil)
r.Header.Set(header, "foobar")
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := &http.Request{Header: make(http.Header)}
source.setForwardHeaders(oreq, r)
if oreq.Header.Get(header) != "foobar" {
t.Fatal("Missmatch custom header")
}
}
}
func TestHttpImageSourceNotForwardHeaders(t *testing.T) {
cases := []string{
"X-Custom",
"X-Token",
}
testURL := createURL("http://bar.com", t)
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+testURL.String(), nil)
r.Header.Set("Not-Forward", "foobar")
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := newHTTPRequest(source, r, http.MethodGet, testURL)
if oreq.Header.Get("Not-Forward") != "" {
t.Fatal("Forwarded unspecified header")
}
}
func TestHttpImageSourceForwardedHeadersNotOverride(t *testing.T) {
cases := []string{
"Authorization",
"X-Custom",
}
testURL := createURL("http://bar.com", t)
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+testURL.String(), nil)
r.Header.Set("Authorization", "foobar")
source := &HTTPImageSource{&SourceConfig{Authorization: "ValidAPIKey", ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := newHTTPRequest(source, r, http.MethodGet, testURL)
if oreq.Header.Get("Authorization") != "ValidAPIKey" {
t.Fatal("Authorization header override")
}
}
func TestHttpImageSourceCaseSensitivityInForwardedHeaders(t *testing.T) {
cases := []string{
"X-Custom",
"X-Token",
}
testURL := createURL("http://bar.com", t)
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+testURL.String(), nil)
r.Header.Set("x-custom", "foobar")
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := newHTTPRequest(source, r, http.MethodGet, testURL)
if oreq.Header.Get("X-Custom") == "" {
t.Fatal("Case sensitive not working on forwarded headers")
}
}
func TestHttpImageSourceEmptyForwardedHeaders(t *testing.T) {
var cases []string
testURL := createURL("http://bar.com", t)
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+testURL.String(), nil)
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
if len(source.Config.ForwardHeaders) != 0 {
t.Log(source.Config.ForwardHeaders)
t.Fatal("Setted empty custom header")
}
oreq := newHTTPRequest(source, r, http.MethodGet, testURL)
if oreq == nil {
t.Fatal("Error creating request using empty custom headers")
}
}
func TestHttpImageSourceError(t *testing.T) {
var err error
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
_, _ = w.Write([]byte("Not found"))
}))
defer ts.Close()
source := NewHTTPImageSource(&SourceConfig{})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
_, err = source.GetImage(r)
if err == nil {
t.Fatalf("Server response should not be valid: %s", err)
}
}
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestHttpImageSourceExceedsMaximumAllowedLength(t *testing.T) {
var body []byte
var err error
buf, _ := ioutil.ReadFile(fixture1024Bytes)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(buf)
}))
defer ts.Close()
source := NewHTTPImageSource(&SourceConfig{
MaxAllowedSize: 1023,
})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err = source.GetImage(r)
if err == nil {
t.Fatalf("It should not allow a request to image exceeding maximum allowed size: %s", err)
}
_, _ = w.Write(body)
}
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestShouldRestrictOrigin(t *testing.T) {
plainOrigins := parseOrigins(
"https://example.org",
)
wildCardOrigins := parseOrigins(
"https://localhost,https://*.example.org,https://some.s3.bucket.on.aws.org,https://*.s3.bucket.on.aws.org",
)
withPathOrigins := parseOrigins(
"https://localhost/foo/bar/,https://*.example.org/foo/,https://some.s3.bucket.on.aws.org/my/bucket/," +
"https://*.s3.bucket.on.aws.org/my/bucket/,https://no-leading-path-slash.example.org/assets",
)
with2Buckets := parseOrigins(
"https://some.s3.bucket.on.aws.org/my/bucket1/,https://some.s3.bucket.on.aws.org/my/bucket2/",
)
pathWildCard := parseOrigins(
"https://some.s3.bucket.on.aws.org/my-bucket-name*",
)
t.Run("Plain origin", func(t *testing.T) {
testURL := createURL("https://example.org/logo.jpg", t)
if shouldRestrictOrigin(testURL, plainOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, plainOrigins)
}
})
t.Run("Wildcard origin, plain URL", func(t *testing.T) {
testURL := createURL("https://example.org/logo.jpg", t)
if shouldRestrictOrigin(testURL, wildCardOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, wildCardOrigins)
}
})
t.Run("Wildcard origin, sub domain URL", func(t *testing.T) {
testURL := createURL("https://node-42.example.org/logo.jpg", t)
if shouldRestrictOrigin(testURL, wildCardOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, wildCardOrigins)
}
})
t.Run("Wildcard origin, sub-sub domain URL", func(t *testing.T) {
testURL := createURL("https://n.s3.bucket.on.aws.org/our/bucket/logo.jpg", t)
if shouldRestrictOrigin(testURL, wildCardOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, wildCardOrigins)
}
})
t.Run("Wildcard origin, incorrect domain URL", func(t *testing.T) {
testURL := createURL("https://myexample.org/logo.jpg", t)
if !shouldRestrictOrigin(testURL, plainOrigins) {
t.Errorf("Expected '%s' to not be allowed with plain origins: %+v", testURL, plainOrigins)
}
if !shouldRestrictOrigin(testURL, wildCardOrigins) {
t.Errorf("Expected '%s' to not be allowed with wildcard origins: %+v", testURL, wildCardOrigins)
}
})
t.Run("Loopback origin with path, correct URL", func(t *testing.T) {
testURL := createURL("https://localhost/foo/bar/logo.png", t)
if shouldRestrictOrigin(testURL, withPathOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("Wildcard origin with path, correct URL", func(t *testing.T) {
testURL := createURL("https://our.company.s3.bucket.on.aws.org/my/bucket/logo.gif", t)
if shouldRestrictOrigin(testURL, withPathOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("Wildcard origin with partial path, correct URL", func(t *testing.T) {
testURL := createURL("https://our.company.s3.bucket.on.aws.org/my/bucket/a/b/c/d/e/logo.gif", t)
if shouldRestrictOrigin(testURL, withPathOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("Wildcard origin with partial path, correct URL double slashes", func(t *testing.T) {
testURL := createURL("https://static.example.org/foo//a//b//c/d/e/logo.webp", t)
if shouldRestrictOrigin(testURL, withPathOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("Wildcard origin with path missing trailing slash, correct URL", func(t *testing.T) {
testURL := createURL("https://no-leading-path-slash.example.org/assets/logo.webp", t)
if shouldRestrictOrigin(testURL, parseOrigins("https://*.example.org/assets")) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("Loopback origin with path, incorrect URL", func(t *testing.T) {
testURL := createURL("https://localhost/wrong/logo.png", t)
if !shouldRestrictOrigin(testURL, withPathOrigins) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, withPathOrigins)
}
})
t.Run("2 buckets, bucket1", func(t *testing.T) {
testURL := createURL("https://some.s3.bucket.on.aws.org/my/bucket1/logo.jpg", t)
if shouldRestrictOrigin(testURL, with2Buckets) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, with2Buckets)
}
})
t.Run("2 buckets, bucket2", func(t *testing.T) {
testURL := createURL("https://some.s3.bucket.on.aws.org/my/bucket2/logo.jpg", t)
if shouldRestrictOrigin(testURL, with2Buckets) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, with2Buckets)
}
})
t.Run("Path wildcard", func(t *testing.T) {
testURL := createURL("https://some.s3.bucket.on.aws.org/my-bucket-name/logo.jpg", t)
testURLFail := createURL("https://some.s3.bucket.on.aws.org/my-other-bucket-name/logo.jpg", t)
if shouldRestrictOrigin(testURL, pathWildCard) {
t.Errorf("Expected '%s' to be allowed with origins: %+v", testURL, pathWildCard)
}
if !shouldRestrictOrigin(testURLFail, pathWildCard) {
t.Errorf("Expected '%s' to be restricted with origins: %+v", testURLFail, pathWildCard)
}
})
}
func TestParseOrigins(t *testing.T) {
t.Run("Appending a trailing slash on paths", func(t *testing.T) {
origins := parseOrigins("http://foo.example.org/assets")
if origins[0].Path != "/assets/" {
t.Errorf("Expected the path to have a trailing /, instead it was: %q", origins[0].Path)
}
})
t.Run("Paths should not receive multiple trailing slashes", func(t *testing.T) {
origins := parseOrigins("http://foo.example.org/assets/")
if origins[0].Path != "/assets/" {
t.Errorf("Expected the path to have a single trailing /, instead it was: %q", origins[0].Path)
}
})
t.Run("Empty paths are fine", func(t *testing.T) {
origins := parseOrigins("http://foo.example.org")
if origins[0].Path != "" {
t.Errorf("Expected the path to remain empty, instead it was: %q", origins[0].Path)
}
})
t.Run("Root paths are fine", func(t *testing.T) {
origins := parseOrigins("http://foo.example.org/")
if origins[0].Path != "/" {
t.Errorf("Expected the path to remain a slash, instead it was: %q", origins[0].Path)
}
})
}
func createURL(urlStr string, t *testing.T) *url.URL {
t.Helper()
result, err := url.Parse(urlStr)
if err != nil {
t.Error("Test setup failed, unable to parse test URL")
}
return result
}