fix(vlans): set_vlan refuses a name with line breaks or double quotes (#28)
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 57s
driver / test (3.13) (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m4s
CI / driver (pull_request) Failing after 2m32s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 57s
driver / test (3.13) (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m4s
CI / driver (pull_request) Failing after 2m32s
driver / publish (pull_request) Skipped
Over SSH the name went into `name "<name>"` in config mode, one line of a block sent line by line before `write memory`: a line break ended the line and the switch ran the rest as commands, a double quote ended the name. netOrk replays VLAN names other devices reported here (NetOrk/netork#2455). set_vlan() now raises ValueError for a name containing \r, \n or ", before anything is sent, over every transport.
This commit is contained in:
1 parent
03a7cb5210
commit
c4cd0bbdf3
3 files changed
+83
-1
No files matched your search
@@ -42,6 +42,10 @@ All notable changes to this driver. The format follows
|
|||||||
a session log object, which older releases do not accept.
|
a session log object, which older releases do not accept.
|
||||||
|
|
||||||
### Fixed
|
### 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 "<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
|
- 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:
|
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
|
`optional_args["tls_fingerprints"]` (`{"443": "SHA256:AB:CD:…"}`) holds the fingerprint
|
||||||
|
|||||||
@@ -85,6 +85,15 @@ _SSH_PORT_BESIDE_API = 22 # SSH beside the REST API: ``port`` then is the API's
|
|||||||
#: What may stand in a quoted CLI value: no whitespace, quote or #.
|
#: What may stand in a quoted CLI value: no whitespace, quote or #.
|
||||||
_CLI_WORD = re.compile(r'^[^\s"#]+$')
|
_CLI_WORD = re.compile(r'^[^\s"#]+$')
|
||||||
|
|
||||||
|
#: What ends a quoted CLI value early: a line break ends its line, a double quote the value.
|
||||||
|
_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")
|
||||||
|
|
||||||
|
|
||||||
class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver):
|
class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver):
|
||||||
"""NAPALM driver for HPE/Aruba ProCurve switches.
|
"""NAPALM driver for HPE/Aruba ProCurve switches.
|
||||||
@@ -820,7 +829,13 @@ class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver):
|
|||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def set_vlan(self, vlan_id: int, config: VlanConfigDict) -> None:
|
def set_vlan(self, vlan_id: int, config: VlanConfigDict) -> None:
|
||||||
"""Create or update a VLAN on the switch."""
|
"""Create or update a VLAN on the switch.
|
||||||
|
|
||||||
|
A name with a line break or a double quote is refused before anything is
|
||||||
|
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"))
|
||||||
if self._transport == "api":
|
if self._transport == "api":
|
||||||
self._api_set_vlan(vlan_id, config)
|
self._api_set_vlan(vlan_id, config)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
"""set_vlan refuses a name that would break out of its CLI line (NetOrk/netork#2455).
|
||||||
|
|
||||||
|
Over SSH the name goes into ``name "<name>"`` in config mode, one line of a
|
||||||
|
block sent line by line. A line break in the name ended that line and sent
|
||||||
|
the rest to the switch as commands of its own; a double quote ended the name.
|
||||||
|
A name with ``\\r``, ``\\n`` or ``"`` is refused before anything is sent, over
|
||||||
|
REST as well, so both transports take the same names.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from napalm_procurve.procurve import ProcurveDriver
|
||||||
|
|
||||||
|
#: The name from the security report: a quote ends the name, a line break the line.
|
||||||
|
INJECTED = 'guest"\npassword manager user-name x plaintext y\nexit\nvlan 42\n name "guest'
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(transport: str) -> ProcurveDriver:
|
||||||
|
d = ProcurveDriver("10.0.0.40", "admin", "pw")
|
||||||
|
d._transport = transport
|
||||||
|
d._api = MagicMock()
|
||||||
|
d._device = MagicMock()
|
||||||
|
d.applied: list[str] = []
|
||||||
|
d._enter_config_mode = MagicMock()
|
||||||
|
d._exit_config_mode = MagicMock()
|
||||||
|
d._save_config = MagicMock()
|
||||||
|
d._apply_config_lines = lambda text: d.applied.append(text) or []
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("transport", ["ssh", "api"])
|
||||||
|
@pytest.mark.parametrize("name", [INJECTED, "a\nb", "a\rb", 'say "hi"'])
|
||||||
|
def test_a_name_with_a_line_break_or_a_double_quote_is_refused(transport, name):
|
||||||
|
driver = _driver(transport)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError, match="VLAN name"):
|
||||||
|
driver.set_vlan(42, {"name": name})
|
||||||
|
|
||||||
|
assert driver.applied == []
|
||||||
|
driver._enter_config_mode.assert_not_called()
|
||||||
|
driver._api.post.assert_not_called()
|
||||||
|
driver._save_config.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_plain_name_is_sent_quoted():
|
||||||
|
driver = _driver("ssh")
|
||||||
|
|
||||||
|
driver.set_vlan(42, {"name": "guest wifi"})
|
||||||
|
|
||||||
|
assert driver.applied == ['vlan 42\n name "guest wifi"\nexit']
|
||||||
|
driver._save_config.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_vlan_without_a_name_is_sent_without_one():
|
||||||
|
driver = _driver("ssh")
|
||||||
|
|
||||||
|
driver.set_vlan(42, {})
|
||||||
|
|
||||||
|
assert driver.applied == ["vlan 42\nexit"]
|
||||||
Reference in new issue
Block a user