forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_http_test.go
executable file
·241 lines (192 loc) · 5.99 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
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("GET", "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("GET", "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("GET", "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("GET", "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 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("GET", "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("GET", "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestShouldRestrictOrigin(t *testing.T) {
plainOrigins := []*url.URL{
createURL("https://example.org", t),
}
wildCardOrigins := []*url.URL{
createURL("https://localhost", t),
createURL("https://*.example.org", t),
createURL("https://some.s3.bucket.on.aws.org", t),
}
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, 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)
}
})
}
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
}