From 17d9bead506c2aed573bcf84ddea9eae69a46e5f Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 19:19:41 +0100 Subject: [PATCH] fix(wireless): refuse an unknown MAC ACL mode before sending anything push_mac_acl wrote macfilter='allow' for "whitelist" and 'deny' for any other mode but "off", so a mode the driver does not know -- a typo, or a new mode from the caller -- became a deny list. It now raises ValueError naming the mode before the first command is sent. Closes #39 --- CHANGELOG.md | 3 +++ napalm_openwrt/wireless_mixin.py | 8 +++++++- tests/unit/test_wireless_writes.py | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aeed11..2cc95d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,9 @@ recorded here; their history is the git log. `manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8). ### Fixed +- `push_mac_acl()` raises `ValueError` for a mode other than `"off"`, `"whitelist"` and + `"blacklist"`, before anything is sent. An unknown mode was written as a deny list + (#39). - `push_mac_acl()` and `push_radio_channel()` raise when the router refuses a step (`CommandErrorException`, naming the `uci` command or the wifi restart and what the router said). They used to return normally after a refused `uci set`, `add_list`, diff --git a/napalm_openwrt/wireless_mixin.py b/napalm_openwrt/wireless_mixin.py index 36e999d..14f5795 100644 --- a/napalm_openwrt/wireless_mixin.py +++ b/napalm_openwrt/wireless_mixin.py @@ -29,6 +29,8 @@ _WIFI_RELOAD = "wifi reload" #: A full down/up of every radio; a channel needs it (see ``push_radio_channel``). _WIFI_RESTART = "wifi" _ACL_OPTIONS = ("macfilter", "maclist") +#: The modes ``push_mac_acl`` takes, and the ``macfilter`` each writes ("off" writes none). +_MACFILTER_BY_MODE = {"whitelist": "allow", "blacklist": "deny", "off": None} def _mac_acl_changes(sec: UciSection, mode: str, macs: list[str]) -> list[str]: @@ -40,7 +42,7 @@ def _mac_acl_changes(sec: UciSection, mode: str, macs: list[str]) -> list[str]: present = [opt for opt in _ACL_OPTIONS if opt in sec.options] if mode == "off": return [uci("delete", f"{sec.ref}.{opt}") for opt in present] - changes = [uci("set", f"{sec.ref}.macfilter={'allow' if mode == 'whitelist' else 'deny'}")] + changes = [uci("set", f"{sec.ref}.macfilter={_MACFILTER_BY_MODE[mode]}")] if "maclist" in present: changes.append(uci("delete", f"{sec.ref}.maclist")) changes += [uci("add_list", f"{sec.ref}.maclist={mac}") for mac in macs] @@ -634,9 +636,13 @@ class OpenWrtWirelessMixin(UciStepsMixin): :param macs: MAC addresses to set as the maclist. Only the entries for the active mode's list are ever passed in — the caller resolves whitelist vs. blacklist before calling. + :raises ValueError: for a mode other than the three, before anything is sent. :raises CommandErrorException: when no wifi-iface carries *ssid_name*, or when a step is refused. """ + if mode not in _MACFILTER_BY_MODE: + known = ", ".join(map(repr, _MACFILTER_BY_MODE)) + raise ValueError(f"unknown MAC ACL mode {mode!r}; expected one of {known}") effective_mode = "off" if (mode == "whitelist" and not macs) else mode sections = [ sec diff --git a/tests/unit/test_wireless_writes.py b/tests/unit/test_wireless_writes.py index c9871b5..553dabf 100644 --- a/tests/unit/test_wireless_writes.py +++ b/tests/unit/test_wireless_writes.py @@ -293,3 +293,13 @@ def test_a_failed_wifi_restart_raises_naming_it(): ap = _Ap({"wifi": CommandResult("", "", 1)}) with pytest.raises(CommandErrorException, match="`wifi`.*exit status 1"): _driver(ap).push_radio_channel("radio0", 6) + + +@pytest.mark.parametrize("mode", ["deny", "Blacklist", "disable", ""]) +def test_an_unknown_mode_raises_before_anything_is_sent(mode): + """A mode the driver does not know was written as a deny list (#39).""" + ap = _Ap() + with pytest.raises(ValueError, match=repr(mode)): + _driver(ap).push_mac_acl("CorpWiFi", mode, ["00:00:5E:00:53:0A"]) + + assert ap.commands == []