From 1312bd6c4ac89449ed004feb88e10e44a1af3457 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 6 Jan 2024 19:19:48 +0100 Subject: [PATCH] implement stricter validation of ip and port --- owrx/source/afedri.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/owrx/source/afedri.py b/owrx/source/afedri.py index b9e6a2e5..79c9fc14 100644 --- a/owrx/source/afedri.py +++ b/owrx/source/afedri.py @@ -1,4 +1,5 @@ import re +from ipaddress import IPv4Address, AddressValueError from owrx.source.soapy import SoapyConnectorSource, SoapyConnectorDeviceDescription from owrx.form.input import Input, CheckboxInput, DropdownInput, Option from owrx.form.input.device import TextInput @@ -12,9 +13,21 @@ AFEDRI_PROFILE_KEYS = ["r820t_lna_agc", "r820t_mixer_agc"] class IPv4AndPortValidator(Validator): def validate(self, key, value) -> None: - m = re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}$", value) - if not m: - raise ValidationError(key, "Wrong format. IPv4:Port expected") + parts = value.split(":") + if len(parts) != 2: + raise ValidationError(key, "Wrong format. Expected IPv4:port") + + try: + IPv4Address(parts[0]) + except AddressValueError as e: + raise ValidationError(key, "IP address error: {}".format(str(e))) + + try: + port = int(parts[1]) + except ValueError: + raise ValidationError(key, "Port number invalid") + if not 0 <= port <= 65535: + raise ValidationError(key, "Port number out of range") class AfedriAddressPortInput(TextInput):