Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/+base-search-prefix-boundary.security.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Resolving an environment to its base interpreter no longer searches a sibling of the base prefix for an interpreter,
where another user could plant one. This happened when the environment's resolved directory only shares a string prefix
with the environment, such as ``venv`` linked to ``venv-3.11``.
9 changes: 7 additions & 2 deletions src/python_discovery/_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,13 @@ def _find_possible_folders(self, inside_folder: str) -> list[str]:
executables[self.original_executable] = None
for exe in executables:
base = os.path.dirname(exe)
if base.startswith(self.prefix):
candidate_folder[f"{inside_folder}{base[len(self.prefix) :]}"] = None
try:
inside = os.path.commonpath([base, self.prefix]) == os.path.normpath(self.prefix)
except ValueError: # pragma: no cover # paths on different drives, or a relative executable
inside = False
if inside:
rel = os.path.relpath(base, self.prefix)
candidate_folder[inside_folder if rel == os.curdir else os.path.join(inside_folder, rel)] = None

# or at root level
candidate_folder[inside_folder] = None
Expand Down
41 changes: 41 additions & 0 deletions tests/python_discovery/test_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,44 @@ def test_discover_ok(
PythonInfo.clear_cache(session_cache)
with pytest.raises(RuntimeError):
_DISCOVERY_CURRENT.discover_exe(session_cache, inside_folder)


@pytest.mark.skipif(not _fs_supports_symlink(), reason="symlink is not supported")
@pytest.mark.parametrize(
("prefix_end", "decoy"),
[
pytest.param("", "base-3.11/bin", id="sibling"),
pytest.param(os.sep, "basebin", id="separator-suffix"),
],
)
def test_discover_exe_skips_sibling_of_prefix(
tmp_path: Path, prefix_end: str, decoy: str, session_cache: DiskCache
) -> None:
base = _link_python(tmp_path / "base" / "bin")
_link_python(tmp_path / decoy)
(tmp_path / "venv-3.11" / "bin").mkdir(parents=True)
(tmp_path / "venv").symlink_to(tmp_path / "venv-3.11")
info = _env_info(f"{tmp_path / 'venv'}{prefix_end}", str(tmp_path / "venv" / "bin" / "python"))
assert info.discover_exe(session_cache, prefix=str(tmp_path / "base")).executable == str(base)


def _link_python(folder: Path) -> Path:
folder.mkdir(parents=True)
python = folder / "python"
python.symlink_to(_DISCOVERY_CURRENT.executable)
return python


def _env_info(prefix: str, executable: str) -> PythonInfo:
info = copy.deepcopy(_DISCOVERY_CURRENT)
info.prefix = prefix
info.executable = info.original_executable = executable
return info


@pytest.mark.skipif(not _fs_supports_symlink(), reason="symlink is not supported")
def test_discover_exe_maps_root_prefix(tmp_path: Path, session_cache: DiskCache) -> None:
exe = tmp_path / "env" / "bin" / "python"
base = _link_python(tmp_path / "base" / exe.parent.relative_to(exe.anchor))
info = _env_info(exe.anchor, str(exe))
assert info.discover_exe(session_cache, prefix=str(tmp_path / "base")).executable == str(base)