bad characters in DomainnameOption could be in warning level
This commit is contained in:
@ -408,23 +408,8 @@ class DomainnameOption(Option):
|
||||
raise ValueError(_('allow_without_dot must be a boolean')) # pragma: optional cover
|
||||
extra['_allow_ip'] = allow_ip
|
||||
extra['_allow_without_dot'] = allow_without_dot
|
||||
end = ''
|
||||
extrachar = ''
|
||||
extrachar_mandatory = ''
|
||||
if extra['_dom_type'] == 'netbios':
|
||||
length = 14 # pragma: optional cover
|
||||
elif extra['_dom_type'] == 'hostname':
|
||||
length = 62 # pragma: optional cover
|
||||
elif extra['_dom_type'] == 'domainname':
|
||||
length = 62
|
||||
if allow_without_dot is False:
|
||||
extrachar_mandatory = '\.'
|
||||
else:
|
||||
extrachar = '\.' # pragma: optional cover
|
||||
end = '+[a-z]*'
|
||||
extra['_domain_re'] = re.compile(r'^(?:[a-z\d][a-z\d\-{0}]{{,{1}}}{2}){3}$'
|
||||
''.format(extrachar, length,
|
||||
extrachar_mandatory, end))
|
||||
extra['_domain_re'] = re.compile(r'^[a-z\d][a-z\d\-]*$')
|
||||
|
||||
super(DomainnameOption, self).__init__(name, doc, default=default,
|
||||
default_multi=default_multi,
|
||||
callback=callback,
|
||||
@ -438,22 +423,52 @@ class DomainnameOption(Option):
|
||||
extra=extra)
|
||||
|
||||
def _validate(self, value, context=undefined):
|
||||
def _valid_length(val):
|
||||
if len(val) < 2:
|
||||
raise ValueError(_("invalid domainname's length (min 2)"))
|
||||
if len(val) > part_name_length:
|
||||
raise ValueError(_("invalid domainname's length (max {0})"
|
||||
"").format(part_name_length))
|
||||
|
||||
if self._get_extra('_allow_ip') is True: # pragma: optional cover
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
return
|
||||
except ValueError:
|
||||
pass
|
||||
if self._get_extra('_dom_type') == 'domainname' and \
|
||||
not self._get_extra('_allow_without_dot') and \
|
||||
'.' not in value: # pragma: optional cover
|
||||
raise ValueError(_("invalid domainname, must have dot"))
|
||||
if len(value) > 255:
|
||||
raise ValueError(_("invalid domainname's length (max 255)")) # pragma: optional cover
|
||||
if len(value) < 2:
|
||||
raise ValueError(_("invalid domainname's length (min 2)")) # pragma: optional cover
|
||||
if not self._get_extra('_domain_re').search(value):
|
||||
raise ValueError(_('invalid domainname')) # pragma: optional cover
|
||||
if self._get_extra('_dom_type') == 'netbios':
|
||||
part_name_length = 15
|
||||
else:
|
||||
part_name_length = 63
|
||||
if self._get_extra('_dom_type') == 'domainname':
|
||||
if not self._get_extra('_allow_without_dot') and not "." in value:
|
||||
raise ValueError(_("invalid domainname, must have dot"))
|
||||
if len(value) > 255:
|
||||
raise ValueError(_("invalid domainname's length (max 255)"))
|
||||
for dom in value.split('.'):
|
||||
_valid_length(dom)
|
||||
else:
|
||||
_valid_length(value)
|
||||
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
def _valid_char(val):
|
||||
if not self._get_extra('_domain_re').search(val):
|
||||
if warnings_only:
|
||||
raise ValueError(_('same characters may cause problems'))
|
||||
else:
|
||||
raise ValueError(_('invalid domainname'))
|
||||
#not for IP
|
||||
if self._get_extra('_allow_ip') is True:
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
return
|
||||
except ValueError:
|
||||
pass
|
||||
if self._get_extra('_dom_type') == 'domainname':
|
||||
for dom in value.split('.'):
|
||||
_valid_char(dom)
|
||||
else:
|
||||
_valid_char(value)
|
||||
|
||||
|
||||
class EmailOption(DomainnameOption):
|
||||
@ -470,6 +485,10 @@ class EmailOption(DomainnameOption):
|
||||
if not self.username_re.search(username):
|
||||
raise ValueError(_('invalid username in email address')) # pragma: optional cover
|
||||
super(EmailOption, self)._validate(domain)
|
||||
super(EmailOption, self)._second_level_validation(domain, False)
|
||||
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
pass
|
||||
|
||||
|
||||
class URLOption(DomainnameOption):
|
||||
@ -503,10 +522,14 @@ class URLOption(DomainnameOption):
|
||||
'65536')) # pragma: optional cover
|
||||
# validate domainname
|
||||
super(URLOption, self)._validate(domain)
|
||||
super(URLOption, self)._second_level_validation(domain, False)
|
||||
# validate file
|
||||
if files is not None and files != '' and not self.path_re.search(files):
|
||||
raise ValueError(_('invalid url, must ends with filename')) # pragma: optional cover
|
||||
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
pass
|
||||
|
||||
|
||||
class UsernameOption(Option):
|
||||
__slots__ = tuple()
|
||||
|
Reference in New Issue
Block a user