Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): resolve fallback paths against a checked run directory

Haozhe Wubce60123a0d2
diff --git a/agent/src/tools/path_utils.py b/agent/src/tools/path_utils.py
index 3906b4296..ca4d93ad0 100644
--- a/agent/src/tools/path_utils.py
+++ b/agent/src/tools/path_utils.py
@@ -205,6 +205,12 @@ def allowed_write_roots() -> list[Path]:
return roots
+def _fallback_candidate(file_path: str, base: Path) -> Path:
+ """Resolve a fallback against the supplied run directory, not the CWD."""
+ candidate = Path(file_path).expanduser()
+ return (candidate if candidate.is_absolute() else base / candidate).resolve()
+
+
def resolve_safe_path(
file_path: str,
run_dir: str | None,
@@ -233,11 +239,13 @@ def resolve_safe_path(
# Try resolving against run_dir if provided
if run_dir:
+ # A rejected network path must never enter the fallback resolver.
+ _rejects_unc(run_dir)
try:
run_root = safe_run_dir(run_dir)
except ValueError as exc:
# If safe_run_dir fails, check if the path is in allowed_roots first
- candidate = Path(file_path).expanduser().resolve()
+ candidate = _fallback_candidate(file_path, Path(run_dir).expanduser())
for root in allowed_roots:
contained = _contained_path(candidate, root)
if contained is not None:
@@ -248,7 +256,7 @@ def resolve_safe_path(
return safe_path(file_path, run_root)
except ValueError as exc:
# Fallback to allowed roots if safe_path containment fails
- candidate = Path(file_path).expanduser().resolve()
+ candidate = _fallback_candidate(file_path, run_root)
for root in allowed_roots:
contained = _contained_path(candidate, root)
if contained is not None:
diff --git a/agent/tests/test_file_tool_sandbox_security.py b/agent/tests/test_file_tool_sandbox_security.py
index 01eef9d4b..a8cf86ef0 100644
--- a/agent/tests/test_file_tool_sandbox_security.py
+++ b/agent/tests/test_file_tool_sandbox_security.py
@@ -231,3 +231,41 @@ def test_read_purpose_rejection_names_the_file_roots_env(tmp_path: Path, monkeyp
assert "VIBE_TRADING_ALLOWED_FILE_ROOTS" in str(excinfo.value)
assert "VIBE_TRADING_ALLOWED_WRITE_ROOTS" not in str(excinfo.value)
+
+
[email protected]("allowed_run", [True, False])
+def test_relative_fallback_uses_run_dir_not_cwd(tmp_path, monkeypatch, allowed_run):
+ run_dir = tmp_path / "runs" / "one"
+ run_dir.mkdir(parents=True)
+ allowed = tmp_path / "allowed"
+ cwd = allowed / "nested" / "cwd"
+ cwd.mkdir(parents=True)
+ monkeypatch.chdir(cwd)
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(run_dir if allowed_run else tmp_path / "other"))
+ with pytest.raises(ValueError):
+ resolve_safe_path("../../outside.txt", str(run_dir), [allowed])
+ # An explicitly allowed sibling remains reachable through the same base.
+ assert resolve_safe_path("../../allowed/ok.txt", str(run_dir), [allowed]) == allowed / "ok.txt"
+
+
[email protected]("run_dir", ["//server/share", r"\\server\share"])
+def test_unc_run_dir_is_rejected_before_any_resolution(run_dir, monkeypatch):
+ def forbidden(*args, **kwargs):
+ pytest.fail("UNC rejection must precede filesystem resolution")
+
+ monkeypatch.setattr(Path, "resolve", forbidden)
+ with pytest.raises(ValueError, match="UNC"):
+ resolve_safe_path("result.txt", run_dir, [])
+
+
+def test_relative_fallback_cannot_escape_through_symlink(tmp_path, monkeypatch):
+ run_dir = tmp_path / "runs" / "one"
+ run_dir.mkdir(parents=True)
+ allowed = tmp_path / "allowed"
+ allowed.mkdir()
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (allowed / "link").symlink_to(outside, target_is_directory=True)
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(run_dir))
+ with pytest.raises(ValueError):
+ resolve_safe_path("../../allowed/link/out.txt", str(run_dir), [allowed])