|
|
@@ -1,31 +1,24 @@
|
|
|
import re
|
|
|
|
|
|
+from django.conf import settings
|
|
|
from django.core.validators import _lazy_re_compile, BaseValidator, URLValidator
|
|
|
|
|
|
|
|
|
class EnhancedURLValidator(URLValidator):
|
|
|
"""
|
|
|
- Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension.
|
|
|
+ Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension and enforce allowed
|
|
|
+ schemes specified in the configuration.
|
|
|
"""
|
|
|
- class AnyURLScheme(object):
|
|
|
- """
|
|
|
- A fake URL list which "contains" all scheme names abiding by the syntax defined in RFC 3986 section 3.1
|
|
|
- """
|
|
|
- def __contains__(self, item):
|
|
|
- if not item or not re.match(r'^[a-z][0-9a-z+\-.]*$', item.lower()):
|
|
|
- return False
|
|
|
- return True
|
|
|
-
|
|
|
fqdn_re = URLValidator.hostname_re + URLValidator.domain_re + URLValidator.tld_re
|
|
|
host_res = [URLValidator.ipv4_re, URLValidator.ipv6_re, fqdn_re, URLValidator.hostname_re]
|
|
|
regex = _lazy_re_compile(
|
|
|
- r'^(?:[a-z0-9\.\-\+]*)://' # Scheme (previously enforced by AnyURLScheme or schemes kwarg)
|
|
|
+ r'^(?:[a-z0-9\.\-\+]*)://' # Scheme (enforced separately)
|
|
|
r'(?:\S+(?::\S*)?@)?' # HTTP basic authentication
|
|
|
r'(?:' + '|'.join(host_res) + ')' # IPv4, IPv6, FQDN, or hostname
|
|
|
r'(?::\d{2,5})?' # Port number
|
|
|
r'(?:[/?#][^\s]*)?' # Path
|
|
|
r'\Z', re.IGNORECASE)
|
|
|
- schemes = AnyURLScheme()
|
|
|
+ schemes = settings.ALLOWED_URL_SCHEMES
|
|
|
|
|
|
|
|
|
class ExclusionValidator(BaseValidator):
|