From f968d22867beb2ea44a6e6716e2fb40e73a68b39 Mon Sep 17 00:00:00 2001 From: Ruben Herold Date: Fri, 7 Aug 2026 22:56:21 +0200 Subject: utils: T9177: fix _are_same_ip() using wrong address family _are_same_ip() computed the address family of both arguments but only ever passed the first one's family to inet_pton() for both, via a computed-but-unused "s_two" local. Comparing addresses of different families (e.g. one IPv4, one IPv6) raised instead of returning False. Found via ruff flagging s_two as an unused local; the function currently has no callers. --- python/vyos/utils/network.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 5b75de1cc..ef8205b97 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -25,8 +25,10 @@ def _are_same_ip(one, two): from vyos.template import is_ipv4 # compare the binary representation of the IP f_one = AF_INET if is_ipv4(one) else AF_INET6 - s_two = AF_INET if is_ipv4(two) else AF_INET6 - return inet_pton(f_one, one) == inet_pton(f_one, two) + f_two = AF_INET if is_ipv4(two) else AF_INET6 + if f_one != f_two: + return False + return inet_pton(f_one, one) == inet_pton(f_two, two) def get_protocol_by_name(protocol_name): """Get protocol number by protocol name -- cgit v1.2.3 From 20022b3470eab0c7c474917a82a7453d655a9714 Mon Sep 17 00:00:00 2001 From: Ruben Herold Date: Fri, 7 Aug 2026 22:57:37 +0200 Subject: utils: T9177: fix ruff findings in touched files Same class of pre-existing whole-file ruff findings as vyos-1x#5374's 477dd4d6b: bare except:, one unused except-clause variable, one multi-statement if, and one unused loop variable in python/vyos/utils/network.py. Also renames a duplicate test_is_ipv6_link_local() in test_utils_network.py to test_is_loopback_addr() (its actual subject) - the duplicate name silently shadowed the first test method, so is_ipv6_link_local() was never actually exercised. No behavior changes to application code. --- python/vyos/utils/network.py | 16 ++++++++-------- src/tests/test_utils_network.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'python') diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index ef8205b97..9eecf97f4 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -131,7 +131,7 @@ def get_vrf_members(vrf: str) -> list: # Skip PIM interfaces which appears in VRF if 'pim' not in data.get('ifname'): interfaces.append(data.get('ifname')) - except: + except Exception: pass return interfaces @@ -219,7 +219,8 @@ def get_interface_namespace(interface: str): """ # Bail out early if netns does not exist tmp = cmdl(['ip', '--json', 'netns', 'ls']) - if not tmp: return None + if not tmp: + return None for ns in loads(tmp): netns = f'{ns["name"]}' @@ -339,7 +340,7 @@ def mac2eui64(mac, prefix=None): net = ip_network(prefix, strict=False) euil = int('0x{0}'.format(eui64), 16) return str(net[euil]) - except: # pylint: disable=bare-except + except Exception: return @@ -381,7 +382,7 @@ def check_port_availability(address: str = None, port: int = 0, protocol = socket.SOCK_STREAM if protocol == 'tcp' else socket.SOCK_DGRAM try: addr_info = socket.getaddrinfo(address, port, socket.AF_UNSPEC, protocol) - except socket.gaierror as e: + except socket.gaierror: print(f'Invalid address: {address}') return False @@ -500,7 +501,6 @@ def is_intf_addr_assigned(ifname: str, addr: str, netns: str=None) -> bool: json_out = loads(out) addresses = jmespath.search("[].addr_info[].{family: family, address: local, prefixlen: prefixlen}", json_out) for address_info in addresses: - family = address_info['family'] address = address_info['address'] prefixlen = address_info['prefixlen'] # Remove the interface name if present in the given address @@ -688,7 +688,7 @@ def ipv6_prefix_length(low, high): try: lo = bytearray(socket.inet_pton(socket.AF_INET6, low)) hi = bytearray(socket.inet_pton(socket.AF_INET6, high)) - except: + except Exception: return None xor = bytearray(a ^ b for a, b in zip(lo, hi)) @@ -752,7 +752,7 @@ def is_valid_ipv4_address_or_range(addr: str) -> bool: return is_valid_ipv4_address_or_range(split[0]) and is_valid_ipv4_address_or_range(split[1]) else: return ip_network(addr).version == 4 - except: + except Exception: return False def is_valid_ipv6_address_or_range(addr: str) -> bool: @@ -768,7 +768,7 @@ def is_valid_ipv6_address_or_range(addr: str) -> bool: return is_valid_ipv6_address_or_range(split[0]) and is_valid_ipv6_address_or_range(split[1]) else: return ip_network(addr).version == 6 - except: + except Exception: return False diff --git a/src/tests/test_utils_network.py b/src/tests/test_utils_network.py index 2e12aea7f..5c2f708ff 100644 --- a/src/tests/test_utils_network.py +++ b/src/tests/test_utils_network.py @@ -35,7 +35,7 @@ class TestVyOSUtilsNetwork(TestCase): self.assertFalse(vyos.utils.network.is_ipv6_link_local('::1')) self.assertFalse(vyos.utils.network.is_ipv6_link_local('::1%lo')) - def test_is_ipv6_link_local(self): + def test_is_loopback_addr(self): self.assertTrue(vyos.utils.network.is_loopback_addr('127.0.0.1')) self.assertTrue(vyos.utils.network.is_loopback_addr('127.0.1.1')) self.assertTrue(vyos.utils.network.is_loopback_addr('127.1.1.1')) -- cgit v1.2.3 From 29a7078648abace564621a36a87eaad46528684d Mon Sep 17 00:00:00 2001 From: Ruben Herold Date: Tue, 11 Aug 2026 22:18:43 +0200 Subject: utils: T9177: remove unreachable raise in get_vrf_members() The ValueError for a non-existent VRF was always immediately swallowed by the surrounding bare except, so the raise never propagated and only obscured the actual behavior (returning an empty list). Check existence up front and return early instead. Flagged by Copilot review on PR #5383. --- python/vyos/utils/network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 9eecf97f4..3a26fe193 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -121,9 +121,9 @@ def get_vrf_members(vrf: str) -> list: :return: list """ interfaces = [] + if not interface_exists(vrf): + return interfaces try: - if not interface_exists(vrf): - raise ValueError(f'VRF "{vrf}" does not exist!') output = cmdl(['ip', '--json', '--brief', 'link', 'show', 'vrf', vrf]) answer = loads(output) for data in answer: -- cgit v1.2.3