fix(system): set_hostname quotes the name in its root commands (#64)
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Failing after 1m6s
driver / test (3.13) (pull_request) Failing after 1m6s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m58s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Failing after 1m6s
driver / test (3.13) (pull_request) Failing after 1m6s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m58s
driver / publish (pull_request) Skipped
`postconf -e 'myhostname = <fqdn>'`, `hostname '<name>'` and the /etc/pve/nodes move put the name between single quotes of their own, so a name with a single quote ended them and the rest ran as root (NetOrk/netork#2456). Each value is now passed through shlex.quote. /etc/hostname, /etc/hosts and /etc/mailname were already written through base64.
This commit is contained in:
1 parent
042b21d3d9
commit
a13cd0202f
3 files changed
+60
-9
No files matched your search
@@ -48,6 +48,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Requires napalm-device-types 4.35.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;'`).
|
||||
- `get_ntp_servers()` read `GET /nodes/{node}/ntp`, a path Proxmox VE does not have, and so
|
||||
always reported no servers. It now reports chrony's configured sources, the ones
|
||||
`get_time_sync()` reads, and the backup server has it too.
|
||||
|
||||
@@ -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
|
||||
@@ -453,12 +454,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/<old> → /etc/pve/nodes/<new> ──────
|
||||
# Proxmox stores VM and CT configs under /etc/pve/nodes/<nodename>/.
|
||||
@@ -467,16 +469,15 @@ class ProxmoxSystemMixin:
|
||||
# Without this step the node boots under the new hostname, finds an
|
||||
# empty /etc/pve/nodes/<new>/ 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)
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import re
|
||||
import shlex
|
||||
|
||||
import pytest
|
||||
from napalm.base.exceptions import CommandErrorException
|
||||
@@ -105,3 +106,48 @@ def test_an_unreadable_cluster_check_refuses_too(driver, node):
|
||||
driver.set_hostname("new")
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user