From 3d3a9cd5275647b32816c2825ec0d6ef1e3f9ad2 Mon Sep 17 00:00:00 2001 From: Daniele Maglie Date: Sun, 21 Jan 2024 14:19:18 +0100 Subject: [PATCH] Added CIDR syntax validators. --- src/piracyshield_component/config.py | 4 +- .../validation/rules/cidr_syntax_ipv4.py | 46 +++++++++++++++++++ .../validation/rules/cidr_syntax_ipv6.py | 46 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/piracyshield_component/validation/rules/cidr_syntax_ipv4.py create mode 100644 src/piracyshield_component/validation/rules/cidr_syntax_ipv6.py diff --git a/src/piracyshield_component/config.py b/src/piracyshield_component/config.py index 62096d6..7f499cc 100644 --- a/src/piracyshield_component/config.py +++ b/src/piracyshield_component/config.py @@ -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: """ diff --git a/src/piracyshield_component/validation/rules/cidr_syntax_ipv4.py b/src/piracyshield_component/validation/rules/cidr_syntax_ipv4.py new file mode 100644 index 0000000..f71a5e9 --- /dev/null +++ b/src/piracyshield_component/validation/rules/cidr_syntax_ipv4.py @@ -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 diff --git a/src/piracyshield_component/validation/rules/cidr_syntax_ipv6.py b/src/piracyshield_component/validation/rules/cidr_syntax_ipv6.py new file mode 100644 index 0000000..e697b14 --- /dev/null +++ b/src/piracyshield_component/validation/rules/cidr_syntax_ipv6.py @@ -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