fix(vlans): set_vlan and set_interface refuse values with line breaks (#17)
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 53s
driver / lowest (pull_request) Successful in 27s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m53s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 53s
driver / lowest (pull_request) Successful in 27s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m53s
driver / publish (pull_request) Skipped
The VLAN name went into `name "<name>"`, a port's description into `description <text>`, each one line of a block sent line by line in config mode and then saved: a line break ended the line and the switch ran the rest as commands, a double quote ended the VLAN name. netOrk replays VLAN names other devices reported here (NetOrk/netork#2455). set_vlan() now raises ValueError for a name containing \r, \n or ", set_interface() for a description containing \r or \n, before anything is sent.
This commit is contained in:
1 parent
7c5e6815f9
commit
ea001751cb
3 files changed
+100
-2
No files matched your search
@@ -20,6 +20,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from typing import Any, ClassVar
|
||||
|
||||
import napalm.base.constants as C
|
||||
@@ -47,6 +48,24 @@ logger = logging.getLogger(__name__)
|
||||
#: get_users' password field: the switch never shows a password, so it stays empty.
|
||||
_PASSWORD_NOT_SHOWN = "" # nosec B105
|
||||
|
||||
#: What ends a CLI value early: a line break ends its line, a double quote a quoted value.
|
||||
_LINE_BREAK = re.compile(r"[\r\n]")
|
||||
_BREAKS_QUOTED_VALUE = re.compile(r'[\r\n"]')
|
||||
|
||||
|
||||
def _check_cli_values(config: Mapping[str, Any]) -> None:
|
||||
"""Refuse a VLAN name or a description that would break out of its CLI line.
|
||||
|
||||
Sent line by line in config mode, a line break ran the rest as commands of
|
||||
their own; a double quote ended the quoted VLAN name (NetOrk/netork#2455).
|
||||
"""
|
||||
name = config.get("name")
|
||||
if isinstance(name, str) and _BREAKS_QUOTED_VALUE.search(name):
|
||||
raise ValueError(f"VLAN name {name!r} must be one line without double quotes")
|
||||
description = config.get("description")
|
||||
if isinstance(description, str) and _LINE_BREAK.search(description):
|
||||
raise ValueError(f"description {description!r} must be one line")
|
||||
|
||||
|
||||
class TPLinkJetstreamDriver(
|
||||
JetstreamTimeSyncMixin, ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver
|
||||
@@ -1569,7 +1588,11 @@ class TPLinkJetstreamDriver(
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
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.
|
||||
"""
|
||||
_check_cli_values(config)
|
||||
self._enter_config_mode()
|
||||
try:
|
||||
lines = [f"vlan {vlan_id}"]
|
||||
@@ -1585,7 +1608,11 @@ class TPLinkJetstreamDriver(
|
||||
self._save_config()
|
||||
|
||||
def set_interface(self, interface: str, config: InterfaceConfigDict) -> None:
|
||||
"""Configure a switch interface (mode, VLAN membership, admin state)."""
|
||||
"""Configure a switch interface (mode, VLAN membership, admin state).
|
||||
|
||||
A description with a line break is refused before anything is sent.
|
||||
"""
|
||||
_check_cli_values(config)
|
||||
mode = config.get("mode")
|
||||
iface = self._to_cli_interface(interface)
|
||||
|
||||
|
||||
Reference in new issue
Block a user