diff --git a/CHANGELOG.md b/CHANGELOG.md index 134962d..4bc2cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Requires napalm-device-types 4.36.0. ### Fixed +- `set_hostname()` hands the name to the shell quoted (`shlex.quote`): to `hostname`, to + `postconf -e` and in the paths of the `/etc/pve/nodes` move (#64, NetOrk/netork#2456). They + stood between single quotes of their own, so a name with a single quote ended them and + the rest ran as root (`x';reboot;'`). - `set_hostname()` writes the new `/etc/hosts` to a `mktemp` file beside it (`/etc/.hosts.XXXXXX`), sets its mode and moves it into place; a failed step removes it (#66, NetOrk/netork#2446). It wrote as root to the fixed `/tmp/_netork_hosts` and diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 34ec20c..908a8de 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -23,6 +23,7 @@ from __future__ import annotations import ipaddress import logging import re +import shlex from typing import Any from napalm.base.exceptions import CommandErrorException @@ -452,12 +453,13 @@ class ProxmoxSystemMixin: ) # ── 4. Postfix myhostname ───────────────────────────────────────── + postfix = shlex.quote(f"myhostname = {fqdn}") self._exec_ssh_command( - f"command -v postconf >/dev/null 2>&1 && postconf -e 'myhostname = {fqdn}' || true" + f"command -v postconf >/dev/null 2>&1 && postconf -e {postfix} || true" ) # ── 5. Apply hostname at runtime ───────────────────────────────── - self._exec_ssh_command(f"hostname '{short_hostname}'") + self._exec_ssh_command(f"hostname {shlex.quote(short_hostname)}") # ── 6. Migrate /etc/pve/nodes/ → /etc/pve/nodes/ ────── # Proxmox stores VM and CT configs under /etc/pve/nodes//. @@ -466,16 +468,15 @@ class ProxmoxSystemMixin: # Without this step the node boots under the new hostname, finds an # empty /etc/pve/nodes// and all VMs appear missing. if old_short and old_short != short_hostname: - pve_old = f"/etc/pve/nodes/{old_short}" - pve_new = f"/etc/pve/nodes/{short_hostname}" + pve_old = shlex.quote(f"/etc/pve/nodes/{old_short}") + pve_new = shlex.quote(f"/etc/pve/nodes/{short_hostname}") migrate_out = self._exec_ssh_command( - f"if [ -d '{pve_old}' ] && [ ! -e '{pve_new}' ]; then" - f" mv '{pve_old}' '{pve_new}'" - f" && echo 'migrated {pve_old} -> {pve_new}';" - f"elif [ -d '{pve_new}' ]; then" + f"if [ -d {pve_old} ] && [ ! -e {pve_new} ]; then" + f" mv {pve_old} {pve_new} && echo migrated {pve_old} '->' {pve_new};" + f"elif [ -d {pve_new} ]; then" f" echo 'target already exists, skipping migration';" f"else" - f" echo 'source {pve_old} not found, skipping migration';" + f" echo source {pve_old} 'not found, skipping migration';" f"fi" ) logger.info("Proxmox node dir migration: %s", migrate_out) diff --git a/tests/test_set_hostname.py b/tests/test_set_hostname.py index e245795..bf6dc36 100644 --- a/tests/test_set_hostname.py +++ b/tests/test_set_hostname.py @@ -5,6 +5,7 @@ from __future__ import annotations import base64 import re +import shlex import pytest from napalm.base.exceptions import CommandErrorException @@ -107,6 +108,51 @@ def test_an_unreadable_cluster_check_refuses_too(driver, node): assert [c for c in node.ran if "corosync.conf" not in c] == [] +# ── the name reaches the shell quoted (NetOrk/netork#2456) ────────────────── +# +# The name went into the root commands between single quotes of their own, so a name +# with a single quote ended them: ``x';reboot;'`` ran ``reboot``. Each command now +# hands it on as one shell word, whatever it holds. + +INJECTED = "x';reboot;'" + + +def _run(node: _Node, start: str) -> str: + return next(c for c in node.ran if c.startswith(start)) + + +def test_the_runtime_hostname_is_one_word(driver, node): + driver.set_hostname(f"{INJECTED}.dom.lan") + + assert shlex.split(_run(node, "hostname ")) == ["hostname", INJECTED] + + +def test_postfix_gets_the_fqdn_as_one_word(driver, node): + driver.set_hostname(f"{INJECTED}.dom.lan") + + words = shlex.split(_run(node, "command -v postconf")) + assert words[words.index("-e") + 1] == f"myhostname = {INJECTED}.dom.lan" + + +def test_the_node_directory_is_moved_by_quoted_paths(driver, node): + driver.set_hostname(INJECTED) + + words = shlex.split(_run(node, "if [ -d")) + assert words[words.index("mv") + 1 : words.index("mv") + 3] == [ + "/etc/pve/nodes/pve", + f"/etc/pve/nodes/{INJECTED}", + ] + + +@pytest.mark.parametrize("name", [INJECTED, "$(reboot)", "`reboot`", "a b"]) +def test_no_command_runs_what_the_name_holds(driver, node, name): + driver.set_hostname(name) + + for command in node.ran: + words = list(shlex.shlex(command, posix=True, punctuation_chars=True)) + assert "reboot" not in words, command + + # ── /etc/hosts is written through a private temporary file (#66, NetOrk/netork#2446) ── # # The new file was written as root to the fixed /tmp/_netork_hosts and moved over