Merge remote-tracking branch 'origin/main' into fix/config-tempfile-2446
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 53s
driver / test (3.12) (pull_request) Successful in 55s
driver / test (3.13) (pull_request) Successful in 51s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m14s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 53s
driver / test (3.12) (pull_request) Successful in 55s
driver / test (3.13) (pull_request) Successful in 51s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m14s
driver / publish (pull_request) Skipped
This commit is contained in:
commit
27ca0667d8
3 files changed
+61
-2
No files matched your search
@@ -70,6 +70,9 @@ recorded here; their history is the git log.
|
||||
`manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8).
|
||||
|
||||
### Fixed
|
||||
- `set_hostname()` hands the name to `uci` as one shell word (the `uci()` helper of
|
||||
`uci_steps`, #43, NetOrk/netork#2456). It stood between single quotes of its own, so a name
|
||||
with a single quote ended them and the rest ran as root (`x';reboot;'`).
|
||||
- A replace `commit_config()` and `rollback()` import their config through a `mktemp`
|
||||
file made under umask 077, written, imported and removed in one command (#45,
|
||||
NetOrk/netork#2446). They wrote it as root to a fixed name in /tmp
|
||||
|
||||
@@ -15,6 +15,8 @@ from __future__ import annotations
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from napalm_openwrt.uci_steps import uci
|
||||
|
||||
|
||||
def _split_uci_list(value: str) -> list[str]:
|
||||
"""Split a ``uci show`` option value into its items.
|
||||
@@ -280,9 +282,14 @@ class OpenWrtVLANMixin:
|
||||
return instances
|
||||
|
||||
def set_hostname(self, new_hostname: str) -> None:
|
||||
"""Set the system hostname via UCI and reload the system service."""
|
||||
"""Set the system hostname via UCI and reload the system service.
|
||||
|
||||
The name reaches ``uci`` as one shell word, whatever it holds
|
||||
(NetOrk/netork#2456): between single quotes of its own, a name with a
|
||||
single quote ended them and the rest ran as root.
|
||||
"""
|
||||
self._send_command(
|
||||
f"uci set system.@system[0].hostname='{new_hostname}' && "
|
||||
f"{uci('set', f'system.@system[0].hostname={new_hostname}')} && "
|
||||
f"uci commit system && "
|
||||
f"/etc/init.d/system reload"
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
"""set_hostname quotes the name for the shell (NetOrk/netork#2456).
|
||||
|
||||
The name went into ``uci set system.@system[0].hostname='<name>'`` between single
|
||||
quotes of its own, so a name with a single quote ended them: ``x';reboot;'`` ran
|
||||
``reboot`` as root. The name now reaches ``uci`` as one word, whatever it holds.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shlex
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_openwrt.openwrt import OpenWrtDriver
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def driver():
|
||||
with patch("napalm_openwrt.openwrt.ConnectHandler"):
|
||||
drv = OpenWrtDriver(hostname="192.0.2.1", username="root", password="")
|
||||
drv._send_command = MagicMock(return_value="")
|
||||
return drv
|
||||
|
||||
|
||||
def _uci_set(command: str) -> list[str]:
|
||||
"""The words of the ``uci set`` the command runs, as the shell splits them."""
|
||||
first = command.split(" && ")[0]
|
||||
return shlex.split(first)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["ap-garden", "x';reboot;'", "a b", "$(reboot)", "`reboot`"])
|
||||
def test_the_name_reaches_uci_as_one_word(driver, name):
|
||||
driver.set_hostname(name)
|
||||
|
||||
[command] = [call.args[0] for call in driver._send_command.call_args_list]
|
||||
assert _uci_set(command) == ["uci", "set", f"system.@system[0].hostname={name}"]
|
||||
assert command.endswith(" && uci commit system && /etc/init.d/system reload")
|
||||
|
||||
|
||||
def test_a_quote_in_the_name_runs_nothing(driver):
|
||||
driver.set_hostname("x';reboot;'")
|
||||
|
||||
[command] = [call.args[0] for call in driver._send_command.call_args_list]
|
||||
# Split as the shell does, the commands are uci's and the reload, nothing else.
|
||||
lexer = shlex.shlex(command, posix=True, punctuation_chars=True)
|
||||
words = list(lexer)
|
||||
assert "reboot" not in words
|
||||
assert words.count(";") == 0
|
||||
Reference in new issue
Block a user