Skip to content

The agent that grows with you

fix(agent): reject subdirectory hint files that resolve outside the tree

beardthelion5aca5406f0b6
diff --git a/agent/subdirectory_hints.py b/agent/subdirectory_hints.py
index d1ad92c05bd..edc1d8b4349 100644
--- a/agent/subdirectory_hints.py
+++ b/agent/subdirectory_hints.py
@@ -37,15 +37,48 @@ def _digest(content: str) -> str:
return hashlib.sha256(content.encode("utf-8")).hexdigest()
+def _resolved_hint_target(hint_path: Path, working_dir: Path) -> Optional[Path]:
+ """Resolved hint-file target, or None when the file must not be loaded.
+
+ ``is_file()`` and ``read_text`` follow symlinks, so a checked-in
+ ``sub/AGENTS.md -> ~/.aws/credentials`` would inject an out-of-tree file
+ into the tool result. The resolved target must stay inside the resolved
+ working dir (same containment ``_within_working_dir`` applies to the
+ directory itself) and pass the canonical read deny-list
+ (``context_references`` applies it to explicit @-references), which also
+ catches in-tree targets like a symlink to the project ``.env``.
+ """
+ try:
+ resolved = hint_path.resolve()
+ except (OSError, RuntimeError):
+ return None
+ try:
+ inside = resolved.is_relative_to(working_dir)
+ except (OSError, ValueError, RuntimeError):
+ inside = False
+ if not inside:
+ return None
+ try:
+ from agent.file_safety import get_read_block_error
+ blocked = get_read_block_error(str(resolved)) is not None
+ except Exception:
+ # Mirror context_references: a deny-list lookup that fails re-opens the
+ # exact hole the check closes, so fail closed.
+ return None
+ return None if blocked else resolved
+
+
def _first_hint_file(directory: Path):
"""``(path, stripped content)`` of the first readable non-empty hint file
in *directory* (priority order), or None. Unreadable files are skipped."""
for filename in _HINT_FILENAMES:
candidate = directory / filename
try:
- if not candidate.is_file():
+ if not candidate.is_file() or (target := _resolved_hint_target(candidate, directory)) is None:
continue
- content = candidate.read_text(encoding="utf-8").strip()
+ # Read the resolved target (not the link path) so a symlink swapped
+ # between check and read still lands on the vetted file.
+ content = target.read_text(encoding="utf-8").strip()
except (OSError, UnicodeDecodeError):
continue
return candidate, content
@@ -202,8 +235,10 @@ class SubdirectoryHintTracker:
continue
except OSError:
continue
+ if (target := _resolved_hint_target(hint_path, self.working_dir)) is None:
+ continue
try:
- content = (_read_text_with_timeout(hint_path) or "").strip()
+ content = (_read_text_with_timeout(target) or "").strip()
if not content:
continue
digest = _digest(content)
diff --git a/tests/agent/test_subdirectory_hints.py b/tests/agent/test_subdirectory_hints.py
index eef49f3eee4..9eccd89514a 100644
--- a/tests/agent/test_subdirectory_hints.py
+++ b/tests/agent/test_subdirectory_hints.py
@@ -375,3 +375,97 @@ class TestExcludedDirectories:
assert result is not None
assert "Personal backend override" in result
assert "Committed backend rules" not in result
+
+
+class TestSymlinkedHintTargets:
+ """A hint file's resolved target must stay inside the working tree and off
+ the read deny-list; the link's in-tree name does not sanitize its target."""
+
+ def test_symlinked_hint_outside_tree_not_loaded(self, tmp_path):
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (outside / "secret.md").write_text("OUTSIDE-MARKER-9f3a", encoding="utf-8")
+
+ workspace = tmp_path / "workspace"
+ workspace.mkdir()
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").symlink_to(outside / "secret.md")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ assert tracker.check_tool_call("read_file", {"path": str(sub / "f.py")}) is None
+
+ def test_symlink_to_denied_file_inside_tree_not_loaded(self, tmp_path):
+ """In-tree containment is not enough: a symlink to the project .env
+ still hits the canonical read deny-list."""
+ workspace = tmp_path / "workspace"
+ workspace.mkdir()
+ (workspace / ".env").write_text("API_KEY=SECRET-VALUE-7b2d", encoding="utf-8")
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").symlink_to(workspace / ".env")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ assert tracker.check_tool_call("read_file", {"path": str(sub / "f.py")}) is None
+
+ def test_in_tree_symlink_still_loads(self, tmp_path):
+ """A symlink whose resolved target stays inside the tree keeps working."""
+ workspace = tmp_path / "workspace"
+ docs = workspace / "docs"
+ docs.mkdir(parents=True)
+ (docs / "AGENTS.md").write_text("Shared in-tree instructions", encoding="utf-8")
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").symlink_to(docs / "AGENTS.md")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ result = tracker.check_tool_call("read_file", {"path": str(sub / "f.py")})
+ assert result is not None and "Shared in-tree instructions" in result
+
+ def test_working_dir_seed_skips_outside_symlink(self, tmp_path):
+ """_first_hint_file applies the same policy: an escaping CWD-level
+ symlink seeds no digest."""
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (outside / "shared.md").write_text("OUTSIDE-MARKER-9f3a", encoding="utf-8")
+
+ workspace = tmp_path / "workspace"
+ workspace.mkdir()
+ (workspace / "AGENTS.md").symlink_to(outside / "shared.md")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ assert tracker._loaded_digests == set()
+ # Same content dropped in a subdirectory is not deduped against a file
+ # that was never legitimately loaded.
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").write_text("OUTSIDE-MARKER-9f3a", encoding="utf-8")
+ assert tracker.check_tool_call("read_file", {"path": str(sub / "f.py")}) is not None
+
+ def test_symlink_escape_via_cd_navigation(self, tmp_path):
+ """The realistic route: a terminal cd into a checked-out dir whose
+ planted symlink escapes the tree."""
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (outside / "loot.md").write_text("OUTSIDE-MARKER-9f3a", encoding="utf-8")
+
+ workspace = tmp_path / "workspace"
+ workspace.mkdir()
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").symlink_to(outside / "loot.md")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ assert tracker.check_tool_call("terminal", {"command": f"cd {sub}"}) is None
+
+ def test_real_hint_file_unaffected_by_target_check(self, tmp_path):
+ """A plain (non-symlink) in-tree hint file still loads."""
+ workspace = tmp_path / "workspace"
+ workspace.mkdir()
+ sub = workspace / "sub"
+ sub.mkdir()
+ (sub / "AGENTS.md").write_text("Legit subdirectory rules", encoding="utf-8")
+
+ tracker = SubdirectoryHintTracker(working_dir=str(workspace))
+ result = tracker.check_tool_call("read_file", {"path": str(sub / "f.py")})
+ assert result is not None and "Legit subdirectory rules" in result