This repository has been archived by the owner on Sep 20, 2018. It is now read-only.
forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 14
/
urivalidate_test.go
152 lines (145 loc) · 4.11 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
package osin
import (
"testing"
)
func TestURIValidate(t *testing.T) {
valid := [][]string{
{
// Exact match
"http://localhost:14000/appauth",
"http://localhost:14000/appauth",
"http://localhost:14000/appauth",
},
{
// 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",
},
{
// 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",
},
}
for _, v := range valid {
if realRedirectUri, err := ValidateUri(v[0], v[1]); err != nil {
t.Errorf("Expected ValidateUri(%s, %s) to succeed, got %v", v[0], v[1], err)
} else if len(v) == 3 && realRedirectUri != v[2] {
t.Errorf("Expected ValidateUri(%s, %s) to return uri %s, got %s", v[0], v[1], v[2], realRedirectUri)
}
}
invalid := [][]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",
},
}
for _, v := range invalid {
if _, err := ValidateUri(v[0], v[1]); err == nil {
t.Errorf("Expected ValidateUri(%s, %s) to fail", v[0], v[1])
}
}
}
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")
}
}