Skip to content

The agent that grows with you

fix(plugins): desktop plugin names cannot reach outside the plugins folder on Windows

kshitijk4poor792ebd2df46e
diff --git a/hermes_cli/desktop_plugin_install.py b/hermes_cli/desktop_plugin_install.py
index 90cfa1c90d4..377337ad1e2 100644
--- a/hermes_cli/desktop_plugin_install.py
+++ b/hermes_cli/desktop_plugin_install.py
@@ -25,6 +25,8 @@ from utils import rmtree_readonly
DESKTOP_PLUGINS_DIR = "desktop-plugins"
_COMMIT = re.compile(r"[0-9a-f]{40}")
+#: Windows device names: a folder called one of these (whatever its extension) opens the device instead.
+_WINDOWS_DEVICE = re.compile(r"(?:CON|PRN|AUX|NUL|CONIN\$|CONOUT\$|CLOCK\$|COM[0-9¹²³]|LPT[0-9¹²³])", re.IGNORECASE)
def desktop_plugins_root() -> Path:
@@ -49,11 +51,13 @@ def _failure(error: str) -> dict[str, Any]:
def _checked_name(name: Optional[str]) -> str:
"""*name* when it can be one folder under the root, else raise: the name comes from a repository
(a subdirectory's last segment, a manifest's ``name``) and must never reach a path unresolved. A
- leading dot is refused too — the root's staging folders use that prefix."""
+ leading dot is refused too — the root's staging folders use that prefix. So are the names Windows
+ reads as something else: a colon makes ``C:..`` a drive-relative path, a device name opens the
+ device, and a trailing dot is dropped."""
candidate = (name or "").strip()
- if (not candidate or candidate in (".", "..") or candidate.startswith(".")
- or "/" in candidate or "\\" in candidate or "\0" in candidate
- or any(ord(char) < 32 for char in candidate) or len(candidate) > 128):
+ if (not candidate or candidate.startswith(".") or candidate.endswith(".")
+ or any(char in candidate for char in "/\\:\0") or any(ord(char) < 32 for char in candidate)
+ or len(candidate) > 128 or _WINDOWS_DEVICE.fullmatch(candidate.split(".", 1)[0].rstrip(" "))):
raise _pc().PluginOperationError(
f"'{name}' is not a desktop plugin folder name: it must be a plain folder name, not a path."
)
diff --git a/tests/hermes_cli/test_desktop_plugin_install.py b/tests/hermes_cli/test_desktop_plugin_install.py
index 29d47a91426..5d478766cda 100644
--- a/tests/hermes_cli/test_desktop_plugin_install.py
+++ b/tests/hermes_cli/test_desktop_plugin_install.py
@@ -206,6 +206,32 @@ def test_remove_leaves_a_linked_plugin_alone_and_names_nothing_outside_the_root(
assert "No desktop plugin named 'ghost'" in install.remove_desktop_plugin("ghost")["error"]
[email protected]("name", ["C:", "C:..", "D:outside", "nul .js", "CON", "nul.txt", "Com1", "LPT¹.js", "CONIN$", "clock$.js", "gizmo."])
+def test_remove_refuses_a_name_windows_reads_as_a_drive_or_a_device(home, name):
+ # On Windows, root / "C:" is the root itself and root / "C:.." its parent: a delete would wipe them.
+ root = _root(home)
+ (root / "keep").mkdir(parents=True)
+ if os.name != "nt":
+ (root / name).mkdir()
+
+ result = install.remove_desktop_plugin(name, force=True)
+
+ assert result["ok"] is False and "folder name" in result["error"]
+ assert (root / "keep").is_dir()
+ assert os.name == "nt" or (root / name).is_dir()
+
+
+def test_a_package_named_like_a_windows_drive_path_is_refused(home, repo):
+ sha = _commit(repo, {"plugin.yaml": "name: 'D:outside'\n", "__init__.py": "",
+ "desktop/plugin.js": "export default {}\n"})
+
+ result = install.install_desktop_plugin(URL, ref=sha, force=True)
+
+ assert result["ok"] is False
+ assert "folder name" in result["error"]
+ assert not any(_root(home).iterdir())
+
+
def _catalog(monkeypatch, sha: str) -> None:
from hermes_cli import plugins_cmd_catalog
entry = SimpleNamespace(install_identifier=URL, sha=sha)