2
2
from django .core .validators import ValidationError
3
3
from django .test import TestCase
4
4
5
- from oauth2_provider .validators import AllowedURIValidator , RedirectURIValidator
5
+ from oauth2_provider .validators import AllowedURIValidator , RedirectURIValidator , WildcardSet
6
6
7
7
8
8
@pytest .mark .usefixtures ("oauth2_settings" )
@@ -36,11 +36,6 @@ def test_validate_custom_uri_scheme(self):
36
36
# Check ValidationError not thrown
37
37
validator (uri )
38
38
39
- validator = AllowedURIValidator (["my-scheme" , "https" , "git+ssh" ], "Origin" )
40
- for uri in good_uris :
41
- # Check ValidationError not thrown
42
- validator (uri )
43
-
44
39
def test_validate_bad_uris (self ):
45
40
validator = RedirectURIValidator (allowed_schemes = ["https" ])
46
41
self .oauth2_settings .ALLOWED_REDIRECT_URI_SCHEMES = ["https" , "good" ]
@@ -67,47 +62,73 @@ def test_validate_bad_uris(self):
67
62
with self .assertRaises (ValidationError ):
68
63
validator (uri )
69
64
70
- def test_validate_good_origin_uris (self ):
71
- """
72
- Test AllowedURIValidator validates origin URIs if they match requirements
73
- """
74
- validator = AllowedURIValidator (
75
- ["https" ],
76
- "Origin" ,
77
- allow_path = False ,
78
- allow_query = False ,
79
- allow_fragments = False ,
80
- )
65
+ def test_validate_wildcard_scheme__bad_uris (self ):
66
+ validator = RedirectURIValidator (allowed_schemes = WildcardSet ())
67
+ bad_uris = [
68
+ "http:/example.com#fragment" ,
69
+ "HTTP://localhost#fragment" ,
70
+ "http://example.com/#fragment" ,
71
+ "good://example.com/#fragment" ,
72
+ " " ,
73
+ "" ,
74
+ # Bad IPv6 URL, urlparse behaves differently for these
75
+ 'https://["><script>alert()</script>' ,
76
+ ]
77
+
78
+ for uri in bad_uris :
79
+ with self .assertRaises (ValidationError , msg = uri ):
80
+ validator (uri )
81
+
82
+ def test_validate_wildcard_scheme_good_uris (self ):
83
+ validator = RedirectURIValidator (allowed_schemes = WildcardSet ())
81
84
good_uris = [
85
+ "my-scheme://example.com" ,
86
+ "my-scheme://example" ,
87
+ "my-scheme://localhost" ,
82
88
"https://example.com" ,
83
- "https://example.com:8080" ,
84
- "https://example" ,
85
- "https://localhost" ,
86
- "https://1.1.1.1" ,
87
- "https://127.0.0.1" ,
88
- "https://255.255.255.255" ,
89
+ "HTTPS://example.com" ,
90
+ "HTTPS://example.com." ,
91
+ "git+ssh://example.com" ,
92
+ "ANY://localhost" ,
93
+ "scheme://example.com" ,
94
+ "at://example.com" ,
95
+ "all://example.com" ,
89
96
]
90
97
for uri in good_uris :
91
98
# Check ValidationError not thrown
92
99
validator (uri )
93
100
94
- def test_validate_bad_origin_uris (self ):
95
- """
96
- Test AllowedURIValidator rejects origin URIs if they do not match requirements
97
- """
98
- validator = AllowedURIValidator (
99
- ["https" ],
100
- "Origin" ,
101
- allow_path = False ,
102
- allow_query = False ,
103
- allow_fragments = False ,
104
- )
101
+
102
+ @pytest .mark .usefixtures ("oauth2_settings" )
103
+ class TestAllowedURIValidator (TestCase ):
104
+ # TODO: verify the specifics of the ValidationErrors
105
+ def test_valid_schemes (self ):
106
+ validator = AllowedURIValidator (["my-scheme" , "https" , "git+ssh" ], "test" )
107
+ good_uris = [
108
+ "my-scheme://example.com" ,
109
+ "my-scheme://example" ,
110
+ "my-scheme://localhost" ,
111
+ "https://example.com" ,
112
+ "HTTPS://example.com" ,
113
+ "git+ssh://example.com" ,
114
+ ]
115
+ for uri in good_uris :
116
+ # Check ValidationError not thrown
117
+ validator (uri )
118
+
119
+ def test_invalid_schemes (self ):
120
+ validator = AllowedURIValidator (["https" ], "test" )
105
121
bad_uris = [
106
122
"http:/example.com" ,
107
123
"HTTP://localhost" ,
108
124
"HTTP://example.com" ,
125
+ "https://-exa" , # triggers an exception in the upstream validators
126
+ "HTTP://example.com/path" ,
127
+ "HTTP://example.com/path?query=string" ,
128
+ "HTTP://example.com/path?query=string#fragmemt" ,
109
129
"HTTP://example.com." ,
110
- "http://example.com/#fragment" ,
130
+ "http://example.com/path/#fragment" ,
131
+ "http://example.com?query=string#fragment" ,
111
132
"123://example.com" ,
112
133
"http://fe80::1" ,
113
134
"git+ssh://example.com" ,
@@ -119,12 +140,125 @@ def test_validate_bad_origin_uris(self):
119
140
"" ,
120
141
# Bad IPv6 URL, urlparse behaves differently for these
121
142
'https://["><script>alert()</script>' ,
122
- # Origin uri should not contain path, query of fragment parts
123
- # https://www.rfc-editor.org/rfc/rfc6454#section-7.1
124
- "https://example.com/" ,
125
- "https://example.com/test" ,
126
- "https://example.com/?q=test" ,
127
- "https://example.com/#test" ,
143
+ ]
144
+
145
+ for uri in bad_uris :
146
+ with self .assertRaises (ValidationError ):
147
+ validator (uri )
148
+
149
+ def test_allow_paths_valid_urls (self ):
150
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_path = True )
151
+ good_uris = [
152
+ "https://example.com" ,
153
+ "https://example.com:8080" ,
154
+ "https://example" ,
155
+ "https://example.com/path" ,
156
+ "https://example.com:8080/path" ,
157
+ "https://example/path" ,
158
+ "https://localhost/path" ,
159
+ "myapp://host/path" ,
160
+ ]
161
+ for uri in good_uris :
162
+ # Check ValidationError not thrown
163
+ validator (uri )
164
+
165
+ def test_allow_paths_invalid_urls (self ):
166
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_path = True )
167
+ bad_uris = [
168
+ "https://example.com?query=string" ,
169
+ "https://example.com#fragment" ,
170
+ "https://example.com/path?query=string" ,
171
+ "https://example.com/path#fragment" ,
172
+ "https://example.com/path?query=string#fragment" ,
173
+ "myapp://example.com/path?query=string" ,
174
+ "myapp://example.com/path#fragment" ,
175
+ "myapp://example.com/path?query=string#fragment" ,
176
+ "bad://example.com/path" ,
177
+ ]
178
+
179
+ for uri in bad_uris :
180
+ with self .assertRaises (ValidationError ):
181
+ validator (uri )
182
+
183
+ def test_allow_query_valid_urls (self ):
184
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_query = True )
185
+ good_uris = [
186
+ "https://example.com" ,
187
+ "https://example.com:8080" ,
188
+ "https://example.com?query=string" ,
189
+ "https://example" ,
190
+ "myapp://example.com?query=string" ,
191
+ "myapp://example?query=string" ,
192
+ ]
193
+ for uri in good_uris :
194
+ # Check ValidationError not thrown
195
+ validator (uri )
196
+
197
+ def test_allow_query_invalid_urls (self ):
198
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_query = True )
199
+ bad_uris = [
200
+ "https://example.com/path" ,
201
+ "https://example.com#fragment" ,
202
+ "https://example.com/path?query=string" ,
203
+ "https://example.com/path#fragment" ,
204
+ "https://example.com/path?query=string#fragment" ,
205
+ "https://example.com:8080/path" ,
206
+ "https://example/path" ,
207
+ "https://localhost/path" ,
208
+ "myapp://example.com/path?query=string" ,
209
+ "myapp://example.com/path#fragment" ,
210
+ "myapp://example.com/path?query=string#fragment" ,
211
+ "bad://example.com/path" ,
212
+ ]
213
+
214
+ for uri in bad_uris :
215
+ with self .assertRaises (ValidationError ):
216
+ validator (uri )
217
+
218
+ def test_allow_fragment_valid_urls (self ):
219
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_fragments = True )
220
+ good_uris = [
221
+ "https://example.com" ,
222
+ "https://example.com#fragment" ,
223
+ "https://example.com:8080" ,
224
+ "https://example.com:8080#fragment" ,
225
+ "https://example" ,
226
+ "https://example#fragment" ,
227
+ "myapp://example" ,
228
+ "myapp://example#fragment" ,
229
+ "myapp://example.com" ,
230
+ "myapp://example.com#fragment" ,
231
+ ]
232
+ for uri in good_uris :
233
+ # Check ValidationError not thrown
234
+ validator (uri )
235
+
236
+ def test_allow_fragment_invalid_urls (self ):
237
+ validator = AllowedURIValidator (["https" , "myapp" ], "test" , allow_fragments = True )
238
+ bad_uris = [
239
+ "https://example.com?query=string" ,
240
+ "https://example.com?query=string#fragment" ,
241
+ "https://example.com/path" ,
242
+ "https://example.com/path?query=string" ,
243
+ "https://example.com/path#fragment" ,
244
+ "https://example.com/path?query=string#fragment" ,
245
+ "https://example.com:8080/path" ,
246
+ "https://example?query=string" ,
247
+ "https://example?query=string#fragment" ,
248
+ "https://example/path" ,
249
+ "https://example/path?query=string" ,
250
+ "https://example/path#fragment" ,
251
+ "https://example/path?query=string#fragment" ,
252
+ "myapp://example?query=string" ,
253
+ "myapp://example?query=string#fragment" ,
254
+ "myapp://example/path" ,
255
+ "myapp://example/path?query=string" ,
256
+ "myapp://example/path#fragment" ,
257
+ "myapp://example.com/path?query=string" ,
258
+ "myapp://example.com/path#fragment" ,
259
+ "myapp://example.com/path?query=string#fragment" ,
260
+ "myapp://example.com?query=string" ,
261
+ "bad://example.com" ,
128
262
]
129
263
130
264
for uri in bad_uris :
0 commit comments