Skip to content

The agent that grows with you

fix(cli): decode percent-encoded fs paths that arrive double-encoded

liuhao1024a557bebb8e66
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.py
index 6d498410519..4dce3878ca4 100644
--- a/hermes_cli/web_routers/files.py
+++ b/hermes_cli/web_routers/files.py
@@ -846,7 +846,7 @@ async def fs_write_text(payload: FsWriteText, profile: Optional[str] = None):
_raise_fs_backend_error(exc)
return {"ok": True, "path": target, "byteSize": byte_size}
- target = _fs_path(payload.path)
+ target = _fs_path(payload.path, decode_fallback=False)
if len(text.encode("utf-8")) > _FS_TEXT_WRITE_MAX_BYTES:
raise HTTPException(status_code=413, detail="Content too large")
diff --git a/hermes_cli/web_server_files.py b/hermes_cli/web_server_files.py
index dce34644b63..a36b086a323 100644
--- a/hermes_cli/web_server_files.py
+++ b/hermes_cli/web_server_files.py
@@ -21,7 +21,17 @@ class ManagedFilesPolicy:
can_change_path: bool
-def _fs_path(raw_path: str, *, cwd: str | None = None) -> Path:
+def _resolve_fs_candidate(raw: str, *, cwd: str | None = None) -> Path:
+ candidate = Path(raw).expanduser()
+ if not candidate.is_absolute():
+ base = Path(cwd).expanduser() if cwd is not None else Path.cwd()
+ if not base.is_absolute():
+ raise HTTPException(status_code=400, detail="Session working directory is unavailable")
+ candidate = base / candidate
+ return candidate.resolve(strict=False)
+
+
+def _fs_path(raw_path: str, *, cwd: str | None = None, decode_fallback: bool = True) -> Path:
raw = str(raw_path or "").strip()
if not raw:
raise HTTPException(status_code=400, detail="Path is required")
@@ -36,13 +46,17 @@ def _fs_path(raw_path: str, *, cwd: str | None = None) -> Path:
raise ValueError
uri_path = f"//{parsed.netloc}{uri_path}"
raw = urllib.request.url2pathname(uri_path)
- candidate = Path(raw).expanduser()
- if not candidate.is_absolute():
- base = Path(cwd).expanduser() if cwd is not None else Path.cwd()
- if not base.is_absolute():
- raise HTTPException(status_code=400, detail="Session working directory is unavailable")
- candidate = base / candidate
- return candidate.resolve(strict=False)
+ 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..."
+ # string that stats as missing (issue #103425). The verbatim path wins
+ # whenever it exists, so filenames that genuinely contain "%XX" keep
+ # resolving as-is; the unquoted form only rescues the lookup.
+ if decode_fallback and "%" in raw and not candidate.exists():
+ decoded = _resolve_fs_candidate(urllib.parse.unquote(raw), cwd=cwd)
+ if decoded.exists():
+ candidate = decoded
+ return candidate
except (OSError, RuntimeError, ValueError):
raise HTTPException(status_code=400, detail="Invalid path")
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index d5f72a7f21e..ec725779db3 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -1,4 +1,5 @@
import base64
+import urllib.parse
from contextlib import nullcontext
from pathlib import Path
@@ -304,3 +305,43 @@ def test_fs_endpoints_require_auth(tmp_path):
assert list_response.status_code == 401
assert read_response.status_code == 401
assert default_response.status_code == 401
+
+
+def test_fs_read_text_decodes_double_encoded_non_ascii_path(client, tmp_path):
+ target = tmp_path / "半导体行业运营分享会纪要.md"
+ target.write_text("hello")
+
+ # One percent-encoding on top of HTTP's own query decoding: the server
+ # receives the literal "%E5%8D%8A..." string (issue #103425).
+ double_encoded = urllib.parse.quote(str(target))
+
+ response = client.get("/api/fs/read-text", params={"path": double_encoded})
+
+ assert response.status_code == 200
+ body = response.json()
+ assert body["path"] == str(target)
+ assert body["text"] == "hello"
+
+
+def test_fs_read_text_keeps_literal_percent_filename_verbatim(client, tmp_path):
+ target = tmp_path / "50%25off.md"
+ target.write_text("literal percent name")
+
+ response = client.get("/api/fs/read-text", params={"path": str(target)})
+
+ assert response.status_code == 200
+ assert response.json()["text"] == "literal percent name"
+
+
+def test_fs_write_text_never_decodes_percent_path(client, tmp_path):
+ sibling = tmp_path / "report v2.md"
+ sibling.write_text("existing")
+
+ response = client.post(
+ "/api/fs/write-text",
+ json={"path": str(tmp_path / "report%20v2.md"), "content": "new"},
+ )
+
+ assert response.status_code == 200
+ assert sibling.read_text() == "existing"
+ assert (tmp_path / "report%20v2.md").read_text() == "new"