diff --git a/CHANGELOG.md b/CHANGELOG.md index deeb4a1..e204b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,6 +139,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Only what a command prints on stdout is its output; stderr no longer stands in when stdout is empty. Each command goes straight to SSH: the driver first posted it to `/nodes/{node}/execute`, which runs API calls, not a shell, and always failed (#17). +- 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 diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 2aae130..421ba43 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -369,10 +369,13 @@ class ProxmoxDriver( import paramiko if self._ssh_client is None: - # A key is tried first; paramiko falls back to the password. + # A key is tried first; paramiko falls back to the password. Only + # these: not the keys in the process's own ~/.ssh, nor an agent. connect_kwargs: _JsonDict = { "username": self._ssh_username or self.username, "timeout": self.timeout, + "look_for_keys": False, + "allow_agent": False, } password = self._ssh_password or self.password if password: diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 051b082..ecaf236 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -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, ) diff --git a/tests/test_ssh_login_key.py b/tests/test_ssh_login_key.py index d9397d4..207edcc 100644 --- a/tests/test_ssh_login_key.py +++ b/tests/test_ssh_login_key.py @@ -77,3 +77,16 @@ def test_a_token_only_login_sends_no_empty_password(client): # noqa: F811 )._node_ssh() assert "password" not in client.connected_with + + +@pytest.mark.parametrize( + "args", + [{"ssh_private_key_str": _ed25519_pem()}, {"ssh_password": "ssh-pw"}, {}], + ids=["key", "ssh-password", "api-password"], +) +def test_only_the_given_credentials_log_in(client, args): # noqa: F811 + """Neither the keys in the worker's own ~/.ssh nor an SSH agent it reaches (#48).""" + ProxmoxDriver("10.0.0.9", "root", "api-s3cret", optional_args=args)._node_ssh() + + assert client.connected_with["look_for_keys"] is False + assert client.connected_with["allow_agent"] is False diff --git a/tests/test_vm_provision_mixin.py b/tests/test_vm_provision_mixin.py index ec5c56c..bda0b4a 100644 --- a/tests/test_vm_provision_mixin.py +++ b/tests/test_vm_provision_mixin.py @@ -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()