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
|
import pytest
from django.core.validators import ValidationError
from oauth2_provider.validators import AllowedURIValidator
from .common_testing import OAuth2ProviderTestCase as TestCase
@pytest.mark.usefixtures("oauth2_settings")
class TestAllowedURIValidator(TestCase):
# TODO: verify the specifics of the ValidationErrors
def test_valid_schemes(self):
validator = AllowedURIValidator(["my-scheme", "https", "git+ssh"], "test")
good_uris = [
"my-scheme://example.com",
"my-scheme://example",
"my-scheme://localhost",
"https://example.com",
"HTTPS://example.com",
"git+ssh://example.com",
]
for uri in good_uris:
# Check ValidationError not thrown
validator(uri)
def test_invalid_schemes(self):
validator = AllowedURIValidator(["https"], "test")
bad_uris = [
"http:/example.com",
"HTTP://localhost",
"HTTP://example.com",
"https://-exa", # triggers an exception in the upstream validators
"HTTP://example.com/path",
"HTTP://example.com/path?query=string",
"HTTP://example.com/path?query=string#fragmemt",
"HTTP://example.com.",
"http://example.com/path/#fragment",
"http://example.com?query=string#fragment",
"123://example.com",
"http://fe80::1",
"git+ssh://example.com",
"my-scheme://example.com",
"uri-without-a-scheme",
"https://example.com/#fragment",
"good://example.com/#fragment",
" ",
"",
# Bad IPv6 URL, urlparse behaves differently for these
'https://["><script>alert()</script>',
]
for uri in bad_uris:
with self.assertRaises(ValidationError):
validator(uri)
def test_allow_paths_valid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_path=True)
good_uris = [
"https://example.com",
"https://example.com:8080",
"https://example",
"https://example.com/path",
"https://example.com:8080/path",
"https://example/path",
"https://localhost/path",
"myapp://host/path",
]
for uri in good_uris:
# Check ValidationError not thrown
validator(uri)
def test_allow_paths_invalid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_path=True)
bad_uris = [
"https://example.com?query=string",
"https://example.com#fragment",
"https://example.com/path?query=string",
"https://example.com/path#fragment",
"https://example.com/path?query=string#fragment",
"myapp://example.com/path?query=string",
"myapp://example.com/path#fragment",
"myapp://example.com/path?query=string#fragment",
"bad://example.com/path",
]
for uri in bad_uris:
with self.assertRaises(ValidationError):
validator(uri)
def test_allow_query_valid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_query=True)
good_uris = [
"https://example.com",
"https://example.com:8080",
"https://example.com?query=string",
"https://example",
"myapp://example.com?query=string",
"myapp://example?query=string",
]
for uri in good_uris:
# Check ValidationError not thrown
validator(uri)
def test_allow_query_invalid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_query=True)
bad_uris = [
"https://example.com/path",
"https://example.com#fragment",
"https://example.com/path?query=string",
"https://example.com/path#fragment",
"https://example.com/path?query=string#fragment",
"https://example.com:8080/path",
"https://example/path",
"https://localhost/path",
"myapp://example.com/path?query=string",
"myapp://example.com/path#fragment",
"myapp://example.com/path?query=string#fragment",
"bad://example.com/path",
]
for uri in bad_uris:
with self.assertRaises(ValidationError):
validator(uri)
def test_allow_fragment_valid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_fragments=True)
good_uris = [
"https://example.com",
"https://example.com#fragment",
"https://example.com:8080",
"https://example.com:8080#fragment",
"https://example",
"https://example#fragment",
"myapp://example",
"myapp://example#fragment",
"myapp://example.com",
"myapp://example.com#fragment",
]
for uri in good_uris:
# Check ValidationError not thrown
validator(uri)
def test_allow_fragment_invalid_urls(self):
validator = AllowedURIValidator(["https", "myapp"], "test", allow_fragments=True)
bad_uris = [
"https://example.com?query=string",
"https://example.com?query=string#fragment",
"https://example.com/path",
"https://example.com/path?query=string",
"https://example.com/path#fragment",
"https://example.com/path?query=string#fragment",
"https://example.com:8080/path",
"https://example?query=string",
"https://example?query=string#fragment",
"https://example/path",
"https://example/path?query=string",
"https://example/path#fragment",
"https://example/path?query=string#fragment",
"myapp://example?query=string",
"myapp://example?query=string#fragment",
"myapp://example/path",
"myapp://example/path?query=string",
"myapp://example/path#fragment",
"myapp://example.com/path?query=string",
"myapp://example.com/path#fragment",
"myapp://example.com/path?query=string#fragment",
"myapp://example.com?query=string",
"bad://example.com",
]
for uri in bad_uris:
with self.assertRaises(ValidationError):
validator(uri)
def test_allow_hostname_wildcard(self):
validator = AllowedURIValidator(["https"], "test", allow_hostname_wildcard=True)
good_uris = [
"https://*.example.com",
"https://*-partial.example.com",
"https://*.partial.example.com",
"https://*-partial.valid.example.com",
]
for uri in good_uris:
# Check ValidationError not thrown
validator(uri)
bad_uris = [
"https://*/",
"https://*-partial",
"https://*.com",
"https://*-partial.com",
"https://*.*.example.com",
"https://invalid.*.example.com",
]
for uri in bad_uris:
with self.assertRaises(ValidationError):
validator(uri)
|