22from django .core .validators import ValidationError
33from django .test import TestCase
44
5- from oauth2_provider .validators import RedirectURIValidator
5+ from oauth2_provider .validators import RedirectURIValidator , AllowedURIValidator
66
77
88@pytest .mark .usefixtures ("oauth2_settings" )
@@ -36,6 +36,11 @@ def test_validate_custom_uri_scheme(self):
3636 # Check ValidationError not thrown
3737 validator (uri )
3838
39+ validator = AllowedURIValidator (["my-scheme" , "https" , "git+ssh" ], "Origin" )
40+ for uri in good_uris :
41+ # Check ValidationError not thrown
42+ validator (uri )
43+
3944 def test_validate_bad_uris (self ):
4045 validator = RedirectURIValidator (allowed_schemes = ["https" ])
4146 self .oauth2_settings .ALLOWED_REDIRECT_URI_SCHEMES = ["https" , "good" ]
@@ -61,3 +66,67 @@ def test_validate_bad_uris(self):
6166 for uri in bad_uris :
6267 with self .assertRaises (ValidationError ):
6368 validator (uri )
69+
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+ )
81+ good_uris = [
82+ "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+ ]
90+ for uri in good_uris :
91+ # Check ValidationError not thrown
92+ validator (uri )
93+
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+ )
105+ bad_uris = [
106+ "http:/example.com" ,
107+ "HTTP://localhost" ,
108+ "HTTP://example.com" ,
109+ "HTTP://example.com." ,
110+ "http://example.com/#fragment" ,
111+ "123://example.com" ,
112+ "http://fe80::1" ,
113+ "git+ssh://example.com" ,
114+ "my-scheme://example.com" ,
115+ "uri-without-a-scheme" ,
116+ "https://example.com/#fragment" ,
117+ "good://example.com/#fragment" ,
118+ " " ,
119+ "" ,
120+ # Bad IPv6 URL, urlparse behaves differently for these
121+ '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" ,
128+ ]
129+
130+ for uri in bad_uris :
131+ with self .assertRaises (ValidationError ):
132+ validator (uri )
0 commit comments