driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Failing after 17s
driver / types (pull_request) Successful in 1m2s
driver / test (3.13) (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m8s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped
Over SSH the description went into ` name "<desc>"` under `interface <port>`, one line of a block sent line by line before `write memory`: a line break ran the rest as commands, a double quote ended the value. It is the same injection as set_vlan's name (NetOrk/netork#2455). set_interface() now raises ValueError for a description containing \r, \n or ", before anything is sent, over every transport, through the same check as set_vlan (_check_cli_value). The tests move to test_cli_values.py, which covers both.
104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
"""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 "<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"]
|
|
|
|
|
|
# ── a port's description: set_interface (#28) ───────────────────────────────
|
|
#
|
|
# Over SSH the description goes into `` name "<desc>"`` under ``interface <port>``,
|
|
# 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"]
|