better validation for Ip/Network/Netmask Option

This commit is contained in:
Emmanuel Garette 2016-05-17 14:10:30 +02:00
parent ef5bcbf98b
commit d1c9250e34

View File

@ -192,6 +192,8 @@ class IPOption(Option):
err = self._impl_valid_unicode(value)
if err:
return err
if value.count('.') != 3:
return ValueError(_('invalid IP'))
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
return ValueError(_('invalid IP')) # pragma: optional cover
@ -330,6 +332,11 @@ class NetworkOption(Option):
err = self._impl_valid_unicode(value)
if err:
return err
if value.count('.') != 3:
return ValueError(_('invalid network address'))
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
return ValueError(_('invalid network address'))
try:
IP(value)
except ValueError: # pragma: optional cover
@ -354,6 +361,11 @@ class NetmaskOption(Option):
err = self._impl_valid_unicode(value)
if err:
return err
if value.count('.') != 3:
return ValueError(_('invalid netmask address'))
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
return ValueError(_('invalid netmask address'))
try:
IP('0.0.0.0/{0}'.format(value))
except ValueError: # pragma: optional cover