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

+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,
)