diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d975b..a5d1666 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,10 +42,10 @@ All notable changes to this driver. The format follows a session log object, which older releases do not accept. ### Fixed -- `set_vlan()` refuses a name with a line break or a double quote (`ValueError`), over - every transport, before anything is sent (#28, NetOrk/netork#2455). Over SSH the name went - into `name ""` in config mode: a line break ended that line and the switch ran the - rest as commands, then `write memory`. +- `set_vlan()` refuses a name, `set_interface()` a description, with a line break or a + double quote (`ValueError`), over every transport, before anything is sent (#28, + NetOrk/netork#2455). Over SSH each went into `name ""` in config mode: a line + break ended that line and the switch ran the rest as commands, then `write memory`. - The REST API's certificate is verified. A switch's self-signed certificate is checked by its SHA-256 fingerprint, not by a CA, with trust on first use: `optional_args["tls_fingerprints"]` (`{"443": "SHA256:AB:CD:…"}`) holds the fingerprint diff --git a/napalm_procurve/procurve.py b/napalm_procurve/procurve.py index 13f69ae..11a4c03 100644 --- a/napalm_procurve/procurve.py +++ b/napalm_procurve/procurve.py @@ -89,10 +89,10 @@ _CLI_WORD = re.compile(r'^[^\s"#]+$') _BREAKS_CLI_VALUE = re.compile(r'[\r\n"]') -def _check_vlan_name(name: str | None) -> None: - """Refuse a VLAN name that would break out of its ``name "..."`` line (NetOrk/netork#2455).""" - if name is not None and _BREAKS_CLI_VALUE.search(name): - raise ValueError(f"VLAN name {name!r} must be one line without double quotes") +def _check_cli_value(what: str, value: str | None) -> None: + """Refuse a value that would break out of its ``name "..."`` line (NetOrk/netork#2455).""" + if value is not None and _BREAKS_CLI_VALUE.search(value): + raise ValueError(f"{what} {value!r} must be one line without double quotes") class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver): @@ -835,7 +835,7 @@ class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver): sent, over every transport: over SSH it ended its ``name "..."`` line and the switch ran the rest as commands. """ - _check_vlan_name(config.get("name")) + _check_cli_value("VLAN name", config.get("name")) if self._transport == "api": self._api_set_vlan(vlan_id, config) else: @@ -926,7 +926,12 @@ class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver): self._save_config() def set_interface(self, interface: str, config: InterfaceConfigDict) -> None: - """Configure a switch interface (mode, VLAN membership).""" + """Configure a switch interface (mode, VLAN membership). + + A description with a line break or a double quote is refused before anything + is sent, over every transport: over SSH it ended its ``name "..."`` line. + """ + _check_cli_value("description", config.get("description")) if self._transport == "api": self._api_set_interface(interface, config) else: diff --git a/tests/unit/test_vlan_name.py b/tests/unit/test_cli_values.py similarity index 56% rename from tests/unit/test_vlan_name.py rename to tests/unit/test_cli_values.py index 70807ff..e954fb8 100644 --- a/tests/unit/test_vlan_name.py +++ b/tests/unit/test_cli_values.py @@ -1,4 +1,5 @@ -"""set_vlan refuses a name that would break out of its CLI line (NetOrk/netork#2455). +"""set_vlan and set_interface refuse values that would break out of their CLI line +(#28, NetOrk/netork#2455). Over SSH the name goes into ``name ""`` in config mode, one line of a block sent line by line. A line break in the name ended that line and sent @@ -61,3 +62,42 @@ def test_a_vlan_without_a_name_is_sent_without_one(): driver.set_vlan(42, {}) assert driver.applied == ["vlan 42\nexit"] + + +# ── a port's description: set_interface (#28) ─────────────────────────────── +# +# Over SSH the description goes into `` name ""`` under ``interface ``, +# one line of the same kind of block: a line break ran the rest as commands, a double +# quote ended the value. Refused the same way, over every transport. + + +@pytest.mark.parametrize("transport", ["ssh", "api"]) +@pytest.mark.parametrize("description", ["uplink\nexit\nno password manager", "a\rb", 'to "core"']) +def test_a_description_with_a_line_break_or_a_double_quote_is_refused(transport, description): + driver = _driver(transport) + + with pytest.raises(ValueError, match="description"): + driver.set_interface("4", {"description": description}) + + assert driver.applied == [] + driver._enter_config_mode.assert_not_called() + driver._api.post.assert_not_called() + driver._api.put.assert_not_called() + driver._api.patch.assert_not_called() + driver._save_config.assert_not_called() + + +def test_a_one_line_description_is_sent_quoted(): + driver = _driver("ssh") + + driver.set_interface("4", {"description": "to core-sw"}) + + assert driver.applied == ['interface 4\n name "to core-sw"\nexit'] + + +def test_an_empty_description_clears_the_name(): + driver = _driver("ssh") + + driver.set_interface("4", {"description": ""}) + + assert driver.applied == ["interface 4\n no name\nexit"]