Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor(security): remove unused PathValidator.confine_to_base (#4868)

LearningCircuit9c07263e8d82
diff --git a/changelog.d/+remove-orphaned-confine-to-base.removal.md b/changelog.d/+remove-orphaned-confine-to-base.removal.md
new file mode 100644
index 0000000000..1976238e86
--- /dev/null
+++ b/changelog.d/+remove-orphaned-confine-to-base.removal.md
@@ -0,0 +1 @@
+Removed the now-unused `PathValidator.confine_to_base` helper (and its tests). It was added only to confine the local-folder RAG indexing route, which has been removed, leaving it with no callers.
diff --git a/src/local_deep_research/security/path_validator.py b/src/local_deep_research/security/path_validator.py
index b738c00a64..1035e7fb4b 100644
--- a/src/local_deep_research/security/path_validator.py
+++ b/src/local_deep_research/security/path_validator.py
@@ -329,55 +329,9 @@ class PathValidator:
return Path(safe_path_str)
- @staticmethod
- def confine_to_base(paths: list[str], base: Path) -> list[str]:
- """Filter ``paths`` to those whose real location stays within ``base``.
-
- Globbing and directory walks follow symlinks, so a symlink planted
- inside ``base`` (a linked file, or a linked directory that is descended
- into) can yield a path that physically lives outside ``base``. Resolving
- each candidate and confining it to the resolved base drops that escape,
- while still allowing symlinks that resolve to a location *inside*
- ``base``. The basename of each dropped entry is logged — never the full
- path, which can contain a username.
-
- A candidate that cannot be resolved is dropped. Resolution must use
- ``resolve(strict=True)``: a symlink loop then raises (``RuntimeError``
- through 3.12, ``OSError``/ELOOP on 3.13+, both caught) so the entry is
- skipped instead of propagating. The default ``resolve(strict=False)``
- cannot be relied on — 3.13+ reimplements it on
- ``os.path.realpath(strict=False)``, which returns the loop path
- unchanged, silently keeping a planted in-base loop. A *broken* symlink
- also raises under ``strict=True`` (its target is missing) and is
- dropped, the same end result as the caller's own existence check.
-
- Known limits — both require write access to ``base``, the same
- precondition as the symlink escape this closes, so they don't widen the
- threat model; ``resolve()`` cannot close either: a *hard* link to an
- out-of-base inode resolves to its own in-base path (resolve is
- name-based, not inode-based), and a TOCTOU swap between this check and a
- later ``open()`` by the caller is not prevented.
- """
- resolved_base = base.resolve()
- confined = []
- for p in paths:
- # Log the basename only — the full path can contain a username.
- name = Path(p).name
- try:
- real = Path(p).resolve(strict=True)
- except (OSError, RuntimeError):
- logger.warning(
- f"Skipped a path that could not be resolved: {name}"
- )
- continue
- if real.is_relative_to(resolved_base):
- confined.append(p)
- else:
- logger.warning(
- f"Skipped a path resolving outside the base directory: "
- f"{name}"
- )
- return confined
+ # A `confine_to_base()` helper once lived here — see #4868. It filtered a list
+ # of paths down to those whose real (symlink-resolved) location stayed inside a
+ # base directory, dropping symlink escapes and loops.
@staticmethod
def validate_model_path(
diff --git a/tests/security/test_path_validator.py b/tests/security/test_path_validator.py
index f43aabadf6..c44a6ed030 100644
--- a/tests/security/test_path_validator.py
+++ b/tests/security/test_path_validator.py
@@ -502,81 +502,3 @@ class TestSecurityScenarios:
assert str(temp_base_dir) in str(result)
except OSError:
pytest.skip("Symlink creation not supported")
-
-
-class TestConfineToBase:
- """Tests for PathValidator.confine_to_base() — drops paths whose real
- location escapes the base directory (symlink-follow protection) while
- keeping in-base files and symlinks that resolve inside the base."""
-
- def test_regular_file_inside_base_is_kept(self, tmp_path):
- base = tmp_path / "docs"
- base.mkdir()
- f = base / "a.txt"
- f.write_text("x")
- assert PathValidator.confine_to_base([str(f)], base) == [str(f)]
-
- def test_symlinked_file_escaping_base_is_dropped(self, tmp_path):
- base = tmp_path / "docs"
- base.mkdir()
- secret = tmp_path / "secret.txt"
- secret.write_text("top")
- link = base / "evil.txt"
- link.symlink_to(secret)
- assert PathValidator.confine_to_base([str(link)], base) == []
-
- def test_symlinked_dir_escaping_base_is_dropped(self, tmp_path):
- base = tmp_path / "docs"
- base.mkdir()
- outside = tmp_path / "outside"
- outside.mkdir()
- (outside / "buried.txt").write_text("x")
- (base / "evildir").symlink_to(outside, target_is_directory=True)
- # the path a recursive glob would return through the dir symlink
- matched = base / "evildir" / "buried.txt"
- assert PathValidator.confine_to_base([str(matched)], base) == []
-
- def test_symlink_resolving_inside_base_is_kept(self, tmp_path):
- base = tmp_path / "docs"
- base.mkdir()
- real = base / "real.txt"
- real.write_text("x")
- link = base / "link.txt"
- link.symlink_to(real)
- assert PathValidator.confine_to_base([str(link)], base) == [str(link)]
-
- def test_symlink_escaping_to_sibling_is_dropped(self, tmp_path):
- base = tmp_path / "docs"
- base.mkdir()
- sibling = tmp_path / "other"
- sibling.mkdir()
- target = sibling / "x.txt"
- target.write_text("x")
- link = base / "x.txt"
- link.symlink_to(target)
- assert PathValidator.confine_to_base([str(link)], base) == []
-
- def test_prefix_sibling_is_not_confused_with_base(self, tmp_path):
- """``docs-evil`` must not be treated as inside ``docs`` — confinement
- is path-component aware, not a string-prefix check."""
- base = tmp_path / "docs"
- base.mkdir()
- evil = tmp_path / "docs-evil"
- evil.mkdir()
- f = evil / "x.txt"
- f.write_text("x")
- assert PathValidator.confine_to_base([str(f)], base) == []
-
- def test_symlink_loop_is_skipped_not_raised(self, tmp_path):
- """A symlink loop makes Path.resolve() raise RuntimeError (not
- OSError); the loop must be skipped and the rest returned — otherwise a
- single planted loop would propagate to the caller."""
- base = tmp_path / "docs"
- base.mkdir()
- real = base / "real.txt"
- real.write_text("x")
- loop = base / "loop.txt"
- loop.symlink_to(loop) # self-referential loop -> ELOOP on resolve()
- assert PathValidator.confine_to_base([str(loop), str(real)], base) == [
- str(real)
- ]