-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathrequest_test.go
More file actions
338 lines (301 loc) · 8.39 KB
/
request_test.go
File metadata and controls
338 lines (301 loc) · 8.39 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package utilities
import (
"net/http"
"net/http/httptest"
tst "testing"
"github.com/stretchr/testify/require"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/sbff"
)
func TestIsRedirectURLValidSameOrigin(t *tst.T) {
cases := []struct {
desc string
siteURL string
redirectURL string
want bool
}{
{
desc: "exact match",
siteURL: "https://example.com",
redirectURL: "https://example.com/path",
want: true,
},
{
desc: "scheme downgrade https→http rejected",
siteURL: "https://example.com",
redirectURL: "http://example.com/path",
want: false,
},
{
desc: "scheme upgrade http→https rejected",
siteURL: "http://example.com",
redirectURL: "https://example.com/path",
want: false,
},
{
desc: "different port rejected",
siteURL: "https://example.com",
redirectURL: "https://example.com:8443/path",
want: false,
},
{
desc: "explicit port matches SiteURL explicit port",
siteURL: "https://example.com:9000",
redirectURL: "https://example.com:9000/path",
want: true,
},
{
desc: "no port vs explicit port rejected",
siteURL: "https://example.com:9000",
redirectURL: "https://example.com/path",
want: false,
},
{
desc: "different explicit ports rejected",
siteURL: "https://example.com:9000",
redirectURL: "https://example.com:9001/path",
want: false,
},
// RFC 8252 Section 7.3: variable ports must be allowed for localhost
{
desc: "localhost with different port allowed (RFC 8252 Section 7.3)",
siteURL: "http://localhost:3000",
redirectURL: "http://localhost:8080/callback",
want: true,
},
{
desc: "127.0.0.1 with different port allowed (RFC 8252 Section 7.3)",
siteURL: "http://127.0.0.1:3000",
redirectURL: "http://127.0.0.1:8080/callback",
want: true,
},
{
desc: "localhost without port in redirect allowed (RFC 8252 Section 7.3)",
siteURL: "http://localhost:3000",
redirectURL: "http://localhost/callback",
want: true,
},
{
desc: "localhost scheme downgrade still rejected despite RFC 8252",
siteURL: "https://localhost:3000",
redirectURL: "http://localhost:8080/callback",
want: false,
},
{
desc: "non-localhost variable port still rejected",
siteURL: "https://example.com:9000",
redirectURL: "https://example.com:9001/path",
want: false,
},
}
for _, c := range cases {
t.Run(c.desc, func(t *tst.T) {
config := conf.GlobalConfiguration{
SiteURL: c.siteURL,
JWT: conf.JWTConfiguration{Secret: "testsecret"},
}
require.NoError(t, config.ApplyDefaults())
require.Equal(t, c.want, IsRedirectURLValid(&config, c.redirectURL))
})
}
}
func TestGetIPAddressWithSBFF(t *tst.T) {
testCases := []struct {
name string
remoteAddr string
headerVal string
expAddr string
}{
{
name: "ValidSBFF",
remoteAddr: "60.60.60.60",
headerVal: "192.168.1.100",
expAddr: "192.168.1.100",
},
{
name: "MissingSBFF",
remoteAddr: "60.60.60.60",
headerVal: "",
expAddr: "60.60.60.60",
},
{
name: "InvalidSBFF",
remoteAddr: "60.60.60.60",
headerVal: "invalid",
expAddr: "60.60.60.60",
},
}
config := conf.SecurityConfiguration{
SbForwardedForEnabled: true,
}
for _, tc := range testCases {
t.Run(tc.name, func(t *tst.T) {
var handler http.HandlerFunc = func(rw http.ResponseWriter, r *http.Request) {
obsAddr := GetIPAddress(r)
require.Equal(t, tc.expAddr, obsAddr)
}
errCallback := func(r *http.Request, err error) {
}
middleware := sbff.Middleware(&config, errCallback)
wrappedHandler := middleware(handler)
r := httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
r.RemoteAddr = tc.remoteAddr
if tc.headerVal != "" {
r.Header.Set(sbff.HeaderName, tc.headerVal)
}
wrappedHandler.ServeHTTP(nil, r)
})
}
}
func TestGetIPAddressWithXFF(t *tst.T) {
examples := []func(r *http.Request) string{
func(r *http.Request) string {
r.Header = nil
r.RemoteAddr = "127.0.0.1:8080"
return "127.0.0.1"
},
func(r *http.Request) string {
r.Header = nil
r.RemoteAddr = "incorrect"
return "incorrect"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
return "127.0.0.1"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "[::1]:8080"
return "::1"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
r.Header.Add("X-Forwarded-For", "127.0.0.2")
return "127.0.0.2"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
r.Header.Add("X-Forwarded-For", "127.0.0.2")
return "127.0.0.2"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
r.Header.Add("X-Forwarded-For", "127.0.0.2,")
return "127.0.0.2"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
r.Header.Add("X-Forwarded-For", "127.0.0.2,127.0.0.3")
return "127.0.0.2"
},
func(r *http.Request) string {
r.Header = make(http.Header)
r.RemoteAddr = "127.0.0.1:8080"
r.Header.Add("X-Forwarded-For", "::1,127.0.0.2")
return "::1"
},
}
for _, example := range examples {
req := &http.Request{}
expected := example(req)
require.Equal(t, GetIPAddress(req), expected)
}
}
func TestGetReferrer(t *tst.T) {
config := conf.GlobalConfiguration{
SiteURL: "https://example.com",
URIAllowList: []string{"http://localhost:8000/*", "http://*.localhost:8000/*", "http://*:12345/*", "http://**:12345/*"},
JWT: conf.JWTConfiguration{
Secret: "testsecret",
},
}
require.NoError(t, config.ApplyDefaults())
cases := []struct {
desc string
redirectURL string
expected string
}{
{
desc: "valid redirect url",
redirectURL: "http://localhost:8000/path",
expected: "http://localhost:8000/path",
},
{
desc: "invalid redirect url",
redirectURL: "http://localhost:3000",
expected: config.SiteURL,
},
{
desc: "no / separator",
redirectURL: "http://localhost:8000",
expected: config.SiteURL,
},
{
desc: "* respects separator",
redirectURL: "http://localhost:8000/path/to/page",
expected: config.SiteURL,
},
{
desc: "* respects parameters",
redirectURL: "http://localhost:8000/path?param=1",
expected: "http://localhost:8000/path?param=1",
},
{
desc: "invalid redirect url due to decimal IP address",
redirectURL: "http://123?.localhost:8000/path",
expected: config.SiteURL,
},
{
desc: "invalid redirect url due to IPv4 address",
redirectURL: "http://123.123.123.123?localhost:8000/path",
expected: config.SiteURL,
},
{
desc: "invalid redirect url due to IPv6 address",
redirectURL: "http://[65e7:9410:d8b6:e227:58cd:e55b:8fc0:206d]?localhost:8000/path",
expected: config.SiteURL,
},
{
desc: "invalid redirect url due to bad URL",
redirectURL: "http://65e7:9410:d8b6:e227:58cd:e55b:8fc0:206d?localhost:8000/path",
expected: config.SiteURL,
},
{
desc: "valid loopback IPv4 address",
redirectURL: "http://127.0.0.1:12345/path",
expected: "http://127.0.0.1:12345/path",
},
{
desc: "valid loopback IPv6 address",
redirectURL: "http://[0:0:0:0:0:0:0:1]:12345/path",
expected: "http://[0:0:0:0:0:0:0:1]:12345/path",
},
{
desc: "same origin allowed",
redirectURL: "https://example.com/dashboard",
expected: "https://example.com/dashboard",
},
{
desc: "same hostname but http scheme rejected (scheme downgrade)",
redirectURL: "http://example.com/dashboard",
expected: config.SiteURL,
},
{
desc: "same hostname and scheme but explicit non-default port rejected",
redirectURL: "https://example.com:8443/dashboard",
expected: config.SiteURL,
},
}
for _, c := range cases {
t.Run(c.desc, func(t *tst.T) {
r := httptest.NewRequest("GET", "http://localhost?redirect_to="+c.redirectURL, nil)
referrer := GetReferrer(r, &config)
require.Equal(t, c.expected, referrer)
})
}
}