diff --git a/docs/changelog/+base-search-prefix-boundary.security.rst b/docs/changelog/+base-search-prefix-boundary.security.rst new file mode 100644 index 0000000..f9b1ade --- /dev/null +++ b/docs/changelog/+base-search-prefix-boundary.security.rst @@ -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``. diff --git a/src/python_discovery/_py_info.py b/src/python_discovery/_py_info.py index 606cced..831e0bc 100644 --- a/src/python_discovery/_py_info.py +++ b/src/python_discovery/_py_info.py @@ -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 diff --git a/tests/python_discovery/test_py_info.py b/tests/python_discovery/test_py_info.py index 36aa4e7..0d21bb6 100644 --- a/tests/python_discovery/test_py_info.py +++ b/tests/python_discovery/test_py_info.py @@ -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)