-
Notifications
You must be signed in to change notification settings - Fork 399
/
urivalidate_test.go
232 lines (225 loc) · 6.05 KB
/
urivalidate_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
package osin
import (
"fmt"
"testing"
)
func TestURIValidate(t *testing.T) {
valid := []struct {
name string
clientRedirectURI string
inputRedirectURI string
normalized string
}{
{
"Exact match",
"http://localhost:14000/appauth",
"http://localhost:14000/appauth",
"http://localhost:14000/appauth",
},
{
"Only domain, exact match",
"http://google.com",
"http://google.com",
"http://google.com",
},
{
"Only domain, with subpath",
"http://google.com",
"http://google.com/redirect",
"http://google.com/redirect",
},
{
"Trailing slash",
"http://www.google.com/myapp",
"http://www.google.com/myapp/",
"http://www.google.com/myapp/",
},
{
"Exact match with trailing slash",
"http://www.google.com/myapp/",
"http://www.google.com/myapp/",
"http://www.google.com/myapp/",
},
{
"Subpath",
"http://www.google.com/myapp",
"http://www.google.com/myapp/interface/implementation",
"http://www.google.com/myapp/interface/implementation",
},
{
"Subpath with trailing slash",
"http://www.google.com/myapp/",
"http://www.google.com/myapp/interface/implementation",
"http://www.google.com/myapp/interface/implementation",
},
{
"Subpath with things that are close to path traversals, but aren't",
"http://www.google.com/myapp",
"http://www.google.com/myapp/.../..implementation../...",
"http://www.google.com/myapp/.../..implementation../...",
},
{
"If the allowed basepath contains path traversals, allow them?",
"http://www.google.com/traversal/../allowed",
"http://www.google.com/traversal/../allowed/with/subpath",
"http://www.google.com/allowed/with/subpath",
},
{
"Backslashes",
"https://mysafewebsite.com/secure/redirect",
"https://mysafewebsite.com/secure/redirect/\\../\\../\\../evil",
"https://mysafewebsite.com/secure/redirect/%5C../%5C../%5C../evil",
},
{
"Query string must be kept",
"http://www.google.com/myapp/redir",
"http://www.google.com/myapp/redir?a=1&b=2",
"http://www.google.com/myapp/redir?a=1&b=2",
},
{
"IPv4 loopback address",
"http://127.0.0.1/callback",
"http://127.0.0.1:8081/callback",
"http://127.0.0.1:8081/callback",
},
{
"Uncommon IPv4 loopback address",
"http://127.0.0.1/callback",
"http://127.0.0.8:8081/callback",
"http://127.0.0.8:8081/callback",
},
{
"IPv6 loopback address",
"http://127.0.0.1/callback",
"http://[::1]:8081/callback",
"http://[::1]:8081/callback",
},
{
"No port in IPv4 loopback address",
"http://127.0.0.1/callback",
"http://127.0.0.1/callback",
"http://127.0.0.1/callback",
},
{
"No port in IPv6 loopback address",
"http://127.0.0.1/callback",
"http://[0:0:0:0:0:0:0:1]/callback",
"http://[0:0:0:0:0:0:0:1]/callback",
},
}
for _, v := range valid {
t.Run(fmt.Sprintf("valid/%s", v.name), func(t *testing.T) {
if realRedirectUri, err := ValidateUri(v.clientRedirectURI, v.inputRedirectURI); err != nil {
t.Errorf("Expected ValidateUri(%s, %s) to succeed, got %v", v.clientRedirectURI, v.inputRedirectURI, err)
} else if realRedirectUri != v.normalized {
t.Errorf("Expected ValidateUri(%s, %s) to return uri %s, got %s", v.clientRedirectURI, v.inputRedirectURI, v.normalized, realRedirectUri)
}
})
}
invalid := []struct {
name string
clientRedirectURI string
inputRedirectURI string
}{
{
"Doesn't satisfy base path",
"http://localhost:14000/appauth",
"http://localhost:14000/app",
},
{
"Doesn't satisfy base path",
"http://localhost:14000/app/",
"http://localhost:14000/app",
},
{
"Not a subpath of base path",
"http://localhost:14000/appauth",
"http://localhost:14000/appauthmodifiedpath",
},
{
"Host mismatch",
"http://www.google.com/myapp",
"http://www2.google.com/myapp",
},
{
"Scheme mismatch",
"http://www.google.com/myapp",
"https://www.google.com/myapp",
},
{
"Path traversal",
"http://www.google.com/myapp",
"http://www.google.com/myapp/..",
},
{
"Embedded path traversal",
"http://www.google.com/myapp",
"http://www.google.com/myapp/../test",
},
{
"Not a subpath",
"http://www.google.com/myapp",
"http://www.google.com/myapp../test",
},
{
"Backslashes",
"https://mysafewebsite.com/secure/redirect",
"https://mysafewebsite.com/secure%2fredirect/../evil",
},
{
"Mismatching ports",
"https://example.com:8081/redirect",
"https://example.com:8080/redirect",
},
{
"Non loopback address",
"http://127.0.0.1/callback",
"http://localhost:8080/callback",
},
{
"Invalid input redirect URI",
"http://127.0.0.1/callback",
"http://127.0.0.1:abc/callback",
},
{
"Non http scheme in redirect URI",
"custom://127.0.0.1/callback",
"custom://127.0.0.1:8080/callback",
},
{
"Redirect URI is loopback, input is a domain name with port",
"http://127.0.0.1/callback",
"http://example.com:8081/callback",
},
{
"Redirect URI is loopback, input is a domain name without port",
"http://127.0.0.1/callback",
"http://example.com/callback",
},
}
for _, v := range invalid {
t.Run(fmt.Sprintf("invalid/%s", v.name), func(t *testing.T) {
if _, err := ValidateUri(v.clientRedirectURI, v.inputRedirectURI); err == nil {
t.Errorf("Expected ValidateUri(%s, %s) to fail", v.clientRedirectURI, v.inputRedirectURI)
}
})
}
}
func TestURIListValidate(t *testing.T) {
// V1
if _, err := ValidateUriList("http://localhost:14000/appauth", "http://localhost:14000/appauth", ""); err != nil {
t.Errorf("V1: %s", err)
}
// V2
if _, err := ValidateUriList("http://localhost:14000/appauth", "http://localhost:14000/app", ""); err == nil {
t.Error("V2 should have failed")
}
// V3
if _, err := ValidateUriList("http://xxx:14000/appauth;http://localhost:14000/appauth", "http://localhost:14000/appauth", ";"); err != nil {
t.Errorf("V3: %s", err)
}
// V4
if _, err := ValidateUriList("http://xxx:14000/appauth;http://localhost:14000/appauth", "http://localhost:14000/app", ";"); err == nil {
t.Error("V4 should have failed")
}
}