Skip to content

The agent that grows with you

fix(web): resolve Git Bash paths in Windows file APIs

Ryan Hill3251a180f01a
diff --git a/hermes_cli/web_server_files.py b/hermes_cli/web_server_files.py
index a36b086a323..2268db3602a 100644
--- a/hermes_cli/web_server_files.py
+++ b/hermes_cli/web_server_files.py
@@ -46,6 +46,11 @@ def _fs_path(raw_path: str, *, cwd: str | None = None, decode_fallback: bool = T
raise ValueError
uri_path = f"//{parsed.netloc}{uri_path}"
raw = urllib.request.url2pathname(uri_path)
+ 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
+
+ raw = _msys_to_windows_path(raw)
candidate = _resolve_fs_candidate(raw, cwd=cwd)
# A remote client hop may percent-encode a path on top of HTTP's own
# decoding, so a non-ASCII name can arrive as a literal "%E5%8D%8A..."
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index ec725779db3..24cfc5ddca3 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -266,6 +266,48 @@ def test_fs_download_rejects_sensitive_files(client, tmp_path):
assert response.status_code == 403
[email protected]("endpoint", ["/api/fs/download", "/api/fs/read-data-url"])
[email protected]("prefix", ["/", "/cygdrive/", "/mnt/"])
+def test_fs_media_shell_paths_deliver_files_and_preserve_access_checks(client, tmp_path, endpoint, prefix):
+ """MEDIA may reuse the path a successful Git Bash command used."""
+ target = tmp_path / "checklist with spaces #1.txt"
+ target.write_bytes(b"Weekend checklist\n[ ] Take a walk\n")
+ sensitive = tmp_path / ".env"
+ sensitive.write_bytes(b"SECRET=must-not-be-delivered")
+
+ def shell_path(path):
+ native = path.as_posix()
+ return f"{prefix}{native[0].lower()}{native[2:]}"
+
+ for path in (shell_path(target), str(target), target.as_uri()):
+ response = client.get(endpoint, params={"path": path})
+ assert response.status_code == 200, response.text
+ data = (base64.b64decode(response.json()["dataUrl"].split(",", 1)[1])
+ if endpoint.endswith("read-data-url") else response.content)
+ assert data == target.read_bytes()
+
+ for path, expected in ((sensitive, 403), (tmp_path / "missing.txt", 404)):
+ response = client.get(endpoint, params={"path": shell_path(path)})
+ assert response.status_code == expected, response.text
+ assert "must-not-be-delivered" not in response.text
+
+ invalid_session = client.get(endpoint, params={
+ "path": shell_path(target), "profile": "default", "session_id": "missing-session",
+ })
+ assert invalid_session.status_code == 404, invalid_session.text
+ client.headers.pop(web_server._SESSION_HEADER_NAME)
+ assert client.get(endpoint, params={"path": shell_path(target)}).status_code == 401
+
+
+def test_fs_media_posix_paths_are_not_interpreted_as_windows_drives():
+ from hermes_cli.web_server_files import _fs_path
+
+ for path in ("/c/Users/example/checklist.txt", "/cygdrive/c/checklist.txt", "/mnt/c/checklist.txt"):
+ assert _fs_path(path) == Path(path).resolve()
+
+
@pytest.mark.parametrize("endpoint", ["/api/fs/read-text", "/api/fs/read-data-url", "/api/fs/download"])
@pytest.mark.parametrize("relative", [".env", "auth.json", "mcp-tokens/github.json"])
def test_fs_readers_reject_sensitive_paths(client, tmp_path, endpoint, relative):