mirror of
https://github.com/fuckpiracyshield/component.git
synced 2024-11-22 13:29:47 +01:00
Added CIDR syntax validators.
This commit is contained in:
parent
ca324d66c7
commit
3d3a9cd527
3 changed files with 94 additions and 2 deletions
|
@ -38,7 +38,7 @@ class Config:
|
|||
return toml.load(file_path)
|
||||
|
||||
except FileNotFoundError:
|
||||
raise ConfigNotFound(f'Impossibile trovare file {file_path}')
|
||||
raise ConfigNotFound(f'Could not find path `{file_path}`')
|
||||
|
||||
def get(self, key: str) -> str | Exception:
|
||||
"""
|
||||
|
@ -52,7 +52,7 @@ class Config:
|
|||
return self.config_content[key]
|
||||
|
||||
except KeyError:
|
||||
raise ConfigKeyNotFound(f'Impossibile trovare chiave {key}')
|
||||
raise ConfigKeyNotFound(f'Could not find key `{key}`')
|
||||
|
||||
def get_all(self, key: str = None) -> any:
|
||||
"""
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
from piracyshield_component.validation.rule import Rule
|
||||
|
||||
import ipaddress
|
||||
|
||||
class CIDRSyntaxIPv4(Rule):
|
||||
|
||||
"""
|
||||
Rule that checks for a valid IPv4 CIDR syntax.
|
||||
"""
|
||||
|
||||
message = 'IPv4 CIDR syntax not valid'
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize parent __init__.
|
||||
"""
|
||||
|
||||
super().__init__()
|
||||
|
||||
def __call__(self, value: str) -> None:
|
||||
"""
|
||||
Checks the validity of the passed string.
|
||||
|
||||
:param value: a valid CIDR syntax string.
|
||||
"""
|
||||
|
||||
try:
|
||||
# doesn't seem solid enough, but we're not too paranoid
|
||||
if '/' not in value:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
||||
|
||||
network = ipaddress.ip_network(value, strict = True)
|
||||
|
||||
# make sure we have at least one address
|
||||
if network.num_addresses < 1:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
||||
|
||||
# non valid at all
|
||||
except ValueError:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
|
@ -0,0 +1,46 @@
|
|||
from piracyshield_component.validation.rule import Rule
|
||||
|
||||
import ipaddress
|
||||
|
||||
class CIDRSyntaxIPv6(Rule):
|
||||
|
||||
"""
|
||||
Rule that checks for a valid IPv6 CIDR syntax.
|
||||
"""
|
||||
|
||||
message = 'IPv6 CIDR syntax not valid'
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize parent __init__.
|
||||
"""
|
||||
|
||||
super().__init__()
|
||||
|
||||
def __call__(self, value: str) -> None:
|
||||
"""
|
||||
Checks the validity of the passed string.
|
||||
|
||||
:param value: a valid CIDR syntax string.
|
||||
"""
|
||||
|
||||
try:
|
||||
# doesn't seem solid enough, but we're not too paranoid
|
||||
if '/' not in value:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
||||
|
||||
network = ipaddress.ip_network(value, strict = True)
|
||||
|
||||
# check for a single IPv6 address
|
||||
if network.prefixlen > 128:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
||||
|
||||
# non valid at all
|
||||
except ValueError:
|
||||
self.register_error(self.message)
|
||||
|
||||
return False
|
Loading…
Reference in a new issue