fix(system): set_hostname hands the name to uci as one shell word (#43) #44

Merged
christianmanivong merged 1 commits from fix/hostname-quote-2456 into main 2026-11-13 08:55:11 +00:00
3 changed files with 61 additions and 2 deletions

No files matched your search

+3
View File
@@ -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;'`).
- `push_mac_acl()` raises `ValueError` for a mode other than `"off"`, `"whitelist"` and
`"blacklist"`, before anything is sent. An unknown mode was written as a deny list
(#39).
+9 -2
View File
@@ -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"
)
+49
View File
@@ -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