fix(vm-provision): quote the image URL in the node's root commands

The cloud-image download put image_url, hand-wrapped in single quotes,
and the cache path built from its basename unquoted into commands that
run as root on the node: a quote, $(...), ; or space in a URL from a
release feed was acted on by the shell. The URL and every path built
from it now go through shlex.quote (exists check, wget, mv, checksum,
rm, unpack, qm importdisk), and the cache file name keeps only the
characters Proxmox keeps in a content file name, as the import volume
name already did. The checksum algorithm is checked against the ones
Proxmox knows, since it is part of the command name.

The file-name helpers move to image_names.py; vm_provision_mixin.py
would otherwise cross the 1000-line limit. _download_cloud_image
drops below 50 lines, so its baseline entry goes.

Closes #43
This commit is contained in:
Christian Manivong committed 2026-11-02 08:46:44 +01:00
1 parent 6a8132e234
commit bfba804ec2
6 files changed
+192 -69

No files matched your search

+84
View File
@@ -1417,3 +1417,87 @@ def test_download_cloud_image_does_not_silence_wget():
download = mixin._run_node_command.call_args_list[1][0][0]
assert download.startswith("wget -nv ")
# ---------------------------------------------------------------------------
# The image URL in root shell commands (#43)
# ---------------------------------------------------------------------------
# A URL a tampered release feed could publish: a quote that ends hand-written
# quoting, a command substitution and a space, each of which the node's shell
# would act on if the URL or the file name built from it went in unquoted.
_HOSTILE_URL = "https://example.org/$(reboot)/a b/x';reboot;'.img"
# reboot() records that the shell ran it; wget() saves the URL it was given as
# the "image", so the file's content shows whether the URL arrived as one argument.
_HOSTILE_PRELUDE = (
'reboot() { echo injected >> "$MARK"; }; '
"wget() { "
"while [ $# -gt 0 ]; do "
'[ "$1" = -O ] && o=$2; [ $# -eq 1 ] && u=$1; shift; done; '
'printf \'%s\' "$u" > "$o"; }; '
# The unpack step of a packed image: "xz -dc <file>" as a plain copy.
'xz() { cat "$2"; }; '
)
def _hostile_shell(tmp_path, monkeypatch) -> ProxmoxVMProvisionMixin:
import napalm_proxmox.vm_provision_mixin as module
cache = tmp_path / "cache"
monkeypatch.setattr(module, "_IMAGE_CACHE_DIR", str(cache))
monkeypatch.setenv("MARK", str(tmp_path / "injected"))
mixin = ProxmoxVMProvisionMixin()
mixin._node_ssh = MagicMock(return_value=_LocalShell(_HOSTILE_PRELUDE))
return mixin
def test_a_hostile_image_url_stays_one_argument_in_every_command(tmp_path, monkeypatch):
import hashlib
from pathlib import Path
mixin = _hostile_shell(tmp_path, monkeypatch)
checksum = "sha256:" + hashlib.sha256(_HOSTILE_URL.encode()).hexdigest()
path = mixin._download_cloud_image(_HOSTILE_URL, checksum, timeout=10)
assert not (tmp_path / "injected").exists()
assert Path(path).read_text() == _HOSTILE_URL # wget got the whole URL
assert Path(path).parent == tmp_path / "cache"
def test_the_cache_file_name_keeps_only_safe_characters(tmp_path, monkeypatch):
import re
from pathlib import Path
mixin = _hostile_shell(tmp_path, monkeypatch)
path = mixin._download_cloud_image(_HOSTILE_URL, None, timeout=10)
assert re.fullmatch(r"[A-Za-z0-9._+=-]+", Path(path).name)
assert Path(path).name.endswith("-x__reboot__.img")
def test_a_packed_hostile_image_is_unpacked_without_running_anything(tmp_path, monkeypatch):
from pathlib import Path
mixin = _hostile_shell(tmp_path, monkeypatch)
url = _HOSTILE_URL + ".xz"
path = mixin._cloud_image_on_node(url, None, timeout=10)
assert not (tmp_path / "injected").exists()
assert Path(path).read_text() == url
assert not path.endswith(".xz")
def test_a_checksum_algorithm_outside_the_known_ones_is_refused():
mixin = ProxmoxVMProvisionMixin()
mixin._run_node_command = MagicMock(return_value="EXISTS")
with pytest.raises(ValueError, match="checksum algorithm"):
mixin._download_cloud_image(
"https://example.org/img/debian.qcow2", "sha256;reboot;:abc", timeout=10
)
assert not any("reboot" in c.args[0] for c in mixin._run_node_command.call_args_list)