Skip to content

The agent that grows with you

fix(webapp): check the opened plugin file is the one at its real path

unsupportedpastelsf9ef116cfb64
diff --git a/hermes_cli/web_routers/desktop_plugins.py b/hermes_cli/web_routers/desktop_plugins.py
index ee463fce7a1..519040cc08c 100644
--- a/hermes_cli/web_routers/desktop_plugins.py
+++ b/hermes_cli/web_routers/desktop_plugins.py
@@ -29,6 +29,8 @@ DESKTOP_PLUGINS_DIR = "desktop-plugins"
_READABLE_FILES = frozenset({"plugin.js", ".hermes-package.json"})
#: Electron's ``PLUGIN_SOURCE_MAX_BYTES``: a plugin loads whole or not at all, never truncated.
PLUGIN_SOURCE_MAX_BYTES = 16 * 1024 * 1024
+#: Never follow a final link where the OS can refuse one, and never block on a FIFO.
+_OPEN_FLAGS = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_BINARY", 0)
def desktop_plugins_root() -> Path:
@@ -72,18 +74,24 @@ def _read(raw: str) -> dict:
not_found = HTTPException(status_code=404, detail="Not a desktop plugin file")
if len(parts) != 2 or parts[1] not in _READABLE_FILES:
raise not_found
- # Follow a linked plugin folder, never a linked file in it: open the entry inside the folder's
- # real path without following a final link, so a check-then-swap can't slip one in either.
+ # Follow a linked plugin folder, never a linked file in it. The file actually opened must be the
+ # regular file at the entry's real, non-sensitive path, checked again after the open, so swapping
+ # the entry or its folder for a link mid-read fails closed, with or without O_NOFOLLOW.
try:
entry = target.parent.resolve(strict=True) / parts[1]
- if entry.is_symlink() or _is_sensitive_path(entry):
+ if _is_sensitive_path(entry) or not stat.S_ISREG(os.lstat(entry).st_mode):
raise not_found
- fd = os.open(entry, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_BINARY", 0))
+ fd = os.open(entry, _OPEN_FLAGS)
except OSError:
raise not_found from None
with os.fdopen(fd, "rb") as handle:
st = os.fstat(handle.fileno())
- if not stat.S_ISREG(st.st_mode):
+ try:
+ here = os.lstat(entry)
+ unchanged = entry.resolve(strict=True) == entry
+ except OSError:
+ raise not_found from None
+ if not (unchanged and stat.S_ISREG(st.st_mode) and (st.st_dev, st.st_ino) == (here.st_dev, here.st_ino)):
raise not_found
if st.st_size > PLUGIN_SOURCE_MAX_BYTES:
raise HTTPException(status_code=413, detail="Plugin source exceeds 16 MiB")
diff --git a/tests/hermes_cli/test_webapp_desktop_plugins.py b/tests/hermes_cli/test_webapp_desktop_plugins.py
index 3ea9e35b8ab..7959eab8d62 100644
--- a/tests/hermes_cli/test_webapp_desktop_plugins.py
+++ b/tests/hermes_cli/test_webapp_desktop_plugins.py
@@ -5,12 +5,15 @@ server, so ``/api/desktop-plugins/*`` answers the same three reads the loader ma
folders, a folder's ``plugin.js`` and package marker), whole, and nothing else. The renderer half is
pinned in ``apps/desktop/src/lib/browser-desktop-plugins.test.ts``.
"""
+import concurrent.futures
import json
+import os
import pytest
from starlette.testclient import TestClient
from hermes_cli import web_server
+from hermes_cli.web_routers import desktop_plugins
def _client(monkeypatch, surface):
@@ -128,6 +131,55 @@ def test_a_linked_entry_file_is_never_followed(webapp, hermes_root, entry):
assert "sk-test" not in read.text
+def _swap_folder_for_link(folder, hermes_root):
+ secrets = hermes_root / "pairing"
+ secrets.mkdir()
+ (secrets / "plugin.js").write_text("sk-test", encoding="utf-8")
+ folder.rename(folder.with_name("evil.old"))
+ folder.symlink_to(secrets, target_is_directory=True)
+
+
+def _swap_entry_for_link(folder, hermes_root):
+ (folder / "plugin.js").unlink()
+ (folder / "plugin.js").symlink_to(hermes_root / ".env")
+
+
+def _swap_entry_for_fifo(folder, hermes_root):
+ (folder / "plugin.js").unlink()
+ os.mkfifo(folder / "plugin.js")
+
+
[email protected](not hasattr(os, "mkfifo"), reason="POSIX links and FIFOs")
[email protected]("swap", [_swap_folder_for_link, _swap_entry_for_link, _swap_entry_for_fifo])
[email protected]("nofollow", [True, False])
+def test_a_swap_between_the_checks_and_the_open_fails_closed(webapp, hermes_root, monkeypatch, swap, nofollow):
+ # The checks pass on a real plugin; the swap lands at the open. Without O_NOFOLLOW (Windows)
+ # the open follows a final link too, so only the identity check after it may stop the read.
+ (hermes_root / ".env").write_text("OPENAI_API_KEY=sk-test", encoding="utf-8")
+ folder = _install(hermes_root, "evil", "export default {}").resolve()
+ if not nofollow:
+ monkeypatch.setattr(desktop_plugins, "_OPEN_FLAGS", desktop_plugins._OPEN_FLAGS & ~getattr(os, "O_NOFOLLOW", 0))
+ real_open, pending = os.open, [True]
+
+ def open_after_swap(path, flags, *args, **kwargs):
+ if pending and str(path) == str(folder / "plugin.js"):
+ pending.clear()
+ swap(folder, hermes_root)
+ return real_open(path, flags, *args, **kwargs)
+
+ monkeypatch.setattr(desktop_plugins.os, "open", open_after_swap)
+ with concurrent.futures.ThreadPoolExecutor(1) as pool:
+ read = pool.submit(webapp.get, "/api/desktop-plugins/read", params={"path": str(folder / "plugin.js")})
+ try:
+ response = read.result(timeout=10)
+ finally:
+ if not read.done() and (folder / "plugin.js").is_fifo():
+ os.close(real_open(folder / "plugin.js", os.O_WRONLY | os.O_NONBLOCK))
+ assert not pending
+ assert response.status_code == 404
+ assert "sk-test" not in response.text
+
+
@pytest.mark.parametrize("surface", ["gateway", "dashboard", "serve"])
def test_only_the_webapp_serves_desktop_plugins(monkeypatch, hermes_root, surface):
_install(hermes_root, "notes", "export default {}")