Skip to content

The agent that grows with you

fix(files): the gateway refuses a file: URL that encodes a path separator

emozilla1edbe513addc
diff --git a/hermes_cli/web_server_files.py b/hermes_cli/web_server_files.py
index ac8e3010060..25d14262fc5 100644
--- a/hermes_cli/web_server_files.py
+++ b/hermes_cli/web_server_files.py
@@ -52,6 +52,28 @@ def _decoded_candidate(raw: str, *, cwd: str | None = None) -> Path | None:
return _resolve_fs_candidate(decoded, cwd=cwd)
+def _file_url_path(url: str) -> str:
+ """The native path a ``file:`` URL names, percent-decoded once.
+
+ A URL that encodes a separator is refused (ValueError) as ``apps/shared/src/file-url.ts``
+ refuses it in the renderer and Electron: ``%2f``, and ``%5c`` on a Windows gateway or in a
+ Windows URL (a UNC host or a drive letter). Decoding it would add a segment, so ``..%2f`` would
+ name a file outside the folder the URL spells.
+ """
+ parsed = urllib.parse.urlparse(url)
+ uri_path = parsed.path
+ host = "" if parsed.netloc.lower() == "localhost" else parsed.netloc
+ drive = re.match(r"/[a-z]:", urllib.parse.unquote(uri_path), re.IGNORECASE)
+ windows = os.name == "nt" or bool(host) or drive is not None
+ if re.search(r"%2f", uri_path, re.IGNORECASE) or (windows and re.search(r"%5c", uri_path, re.IGNORECASE)):
+ raise ValueError("file: URL encodes a path separator")
+ if host:
+ if os.name != "nt":
+ raise ValueError("file: URL names another host")
+ uri_path = f"//{host}{uri_path}"
+ return urllib.request.url2pathname(uri_path)
+
+
def _fs_path(raw_path: str, *, cwd: str | None = None, decode_fallback: bool = True) -> Path:
raw = str(raw_path or "").strip()
if not raw:
@@ -61,13 +83,7 @@ def _fs_path(raw_path: str, *, cwd: str | None = None, decode_fallback: bool = T
try:
from_url = raw.lower().startswith("file:")
if from_url:
- parsed = urllib.parse.urlparse(raw)
- uri_path = parsed.path
- if parsed.netloc and parsed.netloc.lower() != "localhost":
- if os.name != "nt":
- raise ValueError
- uri_path = f"//{parsed.netloc}{uri_path}"
- raw = urllib.request.url2pathname(uri_path)
+ raw = _file_url_path(raw)
elif os.name == "nt":
# MEDIA links can reuse Git Bash paths; native Path would read /c/ as C:\c\.
from tools.environments.local import _msys_to_windows_path
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index d84a9127290..2d757f16776 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -571,6 +571,23 @@ def test_a_file_url_is_decoded_once_by_every_read(client, tmp_path, endpoint):
assert body == "the file the URL names"
[email protected]("name", ["..%2fsecret.md", "..%2Fsecret.md", "..%5csecret.md"])
[email protected]("endpoint", ["read-text", "read-data-url", "download"])
+def test_a_file_url_that_encodes_a_separator_is_refused(client, tmp_path, endpoint, name):
+ """A ``file:`` URL that percent-encodes a separator (``%2f``; ``%5c`` where it decodes to one)
+ is refused, as the shared parser in ``apps/shared/src/file-url.ts`` refuses it in the renderer
+ and Electron: decoding it would add a segment, and ``..%2fsecret.md`` would read a file outside
+ the folder the URL names."""
+ folder = tmp_path / "docs"
+ folder.mkdir()
+ (tmp_path / "secret.md").write_text("outside the named folder")
+
+ response = client.get(f"/api/fs/{endpoint}", params={"path": f"{folder.as_uri()}/{name}"})
+
+ assert response.status_code in {400, 404}, (endpoint, name, response.status_code)
+ assert "outside the named folder" not in response.text
+
+
@pytest.mark.parametrize("name", ["..%2fsecret.md", "%2e%2e/secret.md", "%2E%2E%2Fsecret.md"])
def test_fs_read_text_decode_fallback_stays_in_the_named_folder(client, tmp_path, name):
"""The one-decode rescue for a doubly encoded path (issue #103425) never leaves the folder the