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/+relative-spec-skip-path.security.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A relative path spec such as ``../venv/bin/python`` is no longer joined to ``PATH`` entries or searched for among
uv-managed installs, so its ``..`` segments can no longer run a file outside ``PATH``.
2 changes: 1 addition & 1 deletion src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def propose_interpreters(
return

yield from _propose_explicit(spec, try_first_with, cache, env, tested_exes)
if spec.path is not None and spec.is_abs: # pragma: no cover # relative spec.path is never abs
if spec.path is not None and os.path.dirname(spec.path): # a ".." joined to a PATH entry escapes it
return
yield from _propose_from_path(spec, cache, env, tested_exes, all_implementations=all_implementations)
yield from _propose_from_uv(spec, cache, env, all_implementations=all_implementations)
Expand Down
32 changes: 32 additions & 0 deletions tests/python_discovery/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,38 @@ def test_relative_path_with_parent_reference(
assert Path(result.executable).samefile(sys_executable)


@pytest.mark.parametrize(
("planted", "expected"),
[
pytest.param([], set(), id="missing"),
pytest.param([Path("repo", "venv", _VENV_PYTHON)], {Path("repo", "venv", _VENV_PYTHON)}, id="present"),
],
)
def test_relative_path_searches_only_working_directory(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
mocker: MockerFixture,
planted: list[Path],
expected: set[Path],
) -> None:
for relative in (
*planted,
Path("tools", "venv", _VENV_PYTHON),
Path("tools", _SCRIPTS_DIR, _PYTHON_NAME),
Path("uv", "some-py-impl", _SCRIPTS_DIR, _PYTHON_NAME),
):
(exe := tmp_path / relative).parent.mkdir(parents=True, exist_ok=True)
exe.touch(mode=0o755)
(tmp_path / "repo" / "package").mkdir(parents=True)
monkeypatch.chdir(tmp_path / "repo" / "package")
from_exe = mocker.patch("python_discovery._discovery.PythonInfo.from_exe", return_value=None, autospec=True)
env = {"PATH": str(tmp_path / "tools" / _SCRIPTS_DIR), "UV_PYTHON_INSTALL_DIR": str(tmp_path / "uv")}
spec = str(Path(os.pardir, "venv", _VENV_PYTHON))

assert (get_interpreter(spec, [], env=env), list(iter_interpreters(spec, [], env=env))) == (None, [])
assert {Path(call.args[0]).relative_to(tmp_path) for call in from_exe.call_args_list} == expected


def test_discovery_fallback_fail(session_cache: DiskCache, caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level(logging.DEBUG)
result = get_interpreter(["magic-one", "magic-two"], cache=session_cache)
Expand Down