fix(vm-provision): quote the cloud-init snippet path in its root command
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m4s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m21s
CI / driver (pull_request) Successful in 3m1s
driver / publish (pull_request) Skipped

_write_snippet put the snippet storage's path and the snippet's file
name unquoted into the command that writes the snippet as root on the
node; a space, ; or $(...) in either was acted on by the shell. Both now
go through shlex.quote, and a file name that is not one plain name
(a path, a shell character) is refused before anything runs.

Closes #47
This commit is contained in:
Christian Manivong committed 2026-11-02 09:25:45 +01:00
1 parent b3fe5009c8
commit dffb19e3df
3 files changed
+40 -2

No files matched your search

+3
View File
@@ -132,6 +132,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The SSH login to the node uses only the credentials it is given
(`ssh_private_key_str`, `ssh_password` or the API password): paramiko no longer also
tries the keys in the running process's `~/.ssh` or an SSH agent it can reach (#48).
- `create_vm_from_cloud_init()` quotes the snippet storage's path and the snippet's
file name in the command that writes the cloud-init snippets as root on the node,
and refuses a snippet name that is not one plain file name (#47).
## [0.8.0] - 2026-10-23
+10 -2
View File
@@ -91,6 +91,9 @@ _GUEST_VM_SETTINGS: dict[str, dict[str, str]] = {
"openbsd": {"ostype": "other", "agent": "1,type=isa", "serial0": "socket"},
}
# A snippet's file name: one plain name in the snippets directory, no path.
_SNIPPET_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9._+=-]*")
# A NIC as Proxmox reports it: "virtio=BC:24:11:AA:BB:02,bridge=vmbr0".
_NIC_MAC = re.compile(r"^[a-z0-9]+=([0-9A-Fa-f:]{17})")
@@ -248,12 +251,17 @@ class ProxmoxVMProvisionMixin:
{iso, vztmpl, import} — "snippets" is rejected outright ("does not
have a value in the enumeration"). Snippets can only be written
directly to the filesystem, so this goes over SSH.
:raises ValueError: for a *filename* that is not one plain file name.
"""
if not _SNIPPET_NAME.fullmatch(filename):
raise ValueError(f"not a snippet file name: {filename!r}")
_logger.debug(f"Writing Cloud-Init snippet {filename} to {storage_path}/snippets")
encoded = base64.b64encode(content.encode("utf-8")).decode("ascii")
snippets = f"{storage_path}/snippets"
self._run_node_command(
f"mkdir -p {storage_path}/snippets && "
f"echo {encoded} | base64 -d > {storage_path}/snippets/{filename}",
f"mkdir -p {shlex.quote(snippets)} && echo {encoded} | base64 -d > "
f"{shlex.quote(f'{snippets}/{filename}')}",
timeout=30,
)
+27
View File
@@ -1501,3 +1501,30 @@ def test_a_checksum_algorithm_outside_the_known_ones_is_refused():
)
assert not any("reboot" in c.args[0] for c in mixin._run_node_command.call_args_list)
# ---------------------------------------------------------------------------
# The cloud-init snippet's path in a root shell command (#47)
# ---------------------------------------------------------------------------
def test_a_snippet_storage_path_stays_one_argument(tmp_path, monkeypatch):
storage = tmp_path / "a b;reboot;$(reboot)"
mixin = _hostile_shell(tmp_path, monkeypatch)
mixin._write_snippet(str(storage), "101-user-data.yaml", "#cloud-config\n")
assert not (tmp_path / "injected").exists()
assert (storage / "snippets" / "101-user-data.yaml").read_text() == "#cloud-config\n"
@pytest.mark.parametrize(
"filename", ["x;reboot;.yaml", "../../etc/cron.d/x", "a b.yaml", "$(reboot).yaml", ""]
)
def test_a_snippet_name_that_is_not_a_plain_file_name_is_refused(filename):
mixin = ProxmoxVMProvisionMixin()
mixin._run_node_command = MagicMock(return_value="")
with pytest.raises(ValueError, match="snippet"):
mixin._write_snippet("/var/lib/vz", filename, "#cloud-config\n")
mixin._run_node_command.assert_not_called()