fix: SSH only with the given credentials, quote the snippet path (#48, #47) #49

Merged
christianmanivong merged 2 commits from fix/bugs-2026-11-02b into main 2026-11-02 09:40:50 +00:00
3 changed files with 40 additions and 2 deletions
Showing only changes of commit dffb19e3df - Show all commits

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()