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
2 changes: 2 additions & 0 deletions docs/changelog/283.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
An interpreter symlink is now resolved to the real interpreter when the standard library beside it is owned by another
user. Environments built from ``system_executable`` no longer load a standard library that another local user controls.
16 changes: 13 additions & 3 deletions src/python_discovery/_py_info_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,19 @@ def _resolve_executable_symlink(self, path):
@staticmethod
def _stdlib_landmark_exists(dir_path):
lib_name = os.path.basename(os.path.dirname(os.__file__))
return any(
os.path.exists(os.path.join(dir_path, os.pardir, lib, lib_name, "os.py")) for lib in ("lib", "lib64")
)
stdlibs = [os.path.join(dir_path, os.pardir, lib, lib_name) for lib in ("lib", "lib64")]
if not hasattr(os, "geteuid"): # pragma: win32 cover
return any(os.path.exists(os.path.join(stdlib, "os.py")) for stdlib in stdlibs)
# CPython loads the stdlib from a home recorded as dir_path, so a landmark another user planted in a shared
# parent must not count; lstat, as a symlink owned by another user can be repointed later.
trusted = {0, os.geteuid(), os.stat(dir_path).st_uid}
for stdlib in stdlibs:
landmark = os.path.join(stdlib, "os.py")
if not os.path.exists(landmark):
continue
if {os.lstat(part).st_uid for part in (os.path.dirname(stdlib), stdlib, landmark)} <= trusted:
return True
return False

def _try_posix_versioned_executable(self, base_executable):
major, minor = self.version_info.major, self.version_info.minor
Expand Down
27 changes: 27 additions & 0 deletions tests/test_py_info_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
import json
import os
import stat
import sys
import sysconfig
from itertools import takewhile
Expand Down Expand Up @@ -207,6 +208,32 @@ def test_system_executable_resolves_executable_symlink_only(
assert PythonInfo().system_executable == str(expected)


@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only")
@pytest.mark.usefixtures("no_framework", "not_a_venv")
@pytest.mark.parametrize(
"foreign",
[
pytest.param("lib", id="lib"),
pytest.param("lib/{stdlib}", id="stdlib"),
pytest.param("lib/{stdlib}/os.py", id="landmark"),
],
)
def test_system_executable_ignores_foreign_stdlib_landmark(tmp_path: Path, mocker: MockerFixture, foreign: str) -> None:
link, _ = _layout_stdlib_landmark(tmp_path)
foreign_path = str(tmp_path / "alias" / foreign.format(stdlib=Path(os.__file__).parent.name))
real_lstat = os.lstat

def lstat_with_foreign_owner(path: str) -> os.stat_result:
status = list(real_lstat(path))
if os.path.normpath(path) == foreign_path:
status[stat.ST_UID] = os.geteuid() + 1
return os.stat_result(status)

mocker.patch.object(os, "lstat", side_effect=lstat_with_foreign_owner, autospec=True)
mocker.patch.object(sys, "executable", str(link))
assert PythonInfo().system_executable == str(tmp_path / "install" / "bin" / "python3.12")


@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only")
@pytest.mark.usefixtures("not_a_venv")
def test_system_executable_framework_symlink_kept(tmp_path: Path, mocker: MockerFixture) -> None:
Expand Down