From c437409fda0d7f7f39bed06947c9a23dd18e13da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:13:20 -0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(discovery):=20skip=20PAT?= =?UTF-8?q?H=20for=20relative=20specs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A relative path spec such as ../venv/bin/python that is missing from the working directory was joined to every PATH entry and probed there. The ".." segments carried the join out of PATH, so a file planted in a writable directory outside PATH ran with the caller's privileges during the search. The result was never selected, but it had already run. The return meant to stop the search for path specs only covered absolute paths, which are handled earlier, so it never fired for a relative one. Stop after the explicit candidates for any spec with a directory component. Bare names still search PATH and uv installs. Reported by oss-scanner as ANT-2026-NS23L1D8. --- .../+relative-spec-skip-path.security.rst | 3 ++ src/python_discovery/_discovery.py | 2 +- tests/python_discovery/test_discovery.py | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 docs/changelog/+relative-spec-skip-path.security.rst diff --git a/docs/changelog/+relative-spec-skip-path.security.rst b/docs/changelog/+relative-spec-skip-path.security.rst new file mode 100644 index 0000000..fa2bef1 --- /dev/null +++ b/docs/changelog/+relative-spec-skip-path.security.rst @@ -0,0 +1,3 @@ +A relative path spec such as ``../venv/bin/python`` is now looked up only in the working directory. Discovery no +longer joins it to ``PATH`` entries or searches uv-managed installs for it, so ``..`` segments can no longer run a file +outside ``PATH``. diff --git a/src/python_discovery/_discovery.py b/src/python_discovery/_discovery.py index 00a70c2..84b1f2d 100644 --- a/src/python_discovery/_discovery.py +++ b/src/python_discovery/_discovery.py @@ -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) diff --git a/tests/python_discovery/test_discovery.py b/tests/python_discovery/test_discovery.py index a6f6885..3aec8fd 100644 --- a/tests/python_discovery/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -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) From 7070f7a008b0e6d8ef311f9437906bb8413ee8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:19:59 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9D=20docs(changelog):=20scope=20r?= =?UTF-8?q?elative=20spec=20entry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit try_first_with paths are still probed for a relative path spec, so the entry now states only what stopped: the PATH join and the uv search. --- docs/changelog/+relative-spec-skip-path.security.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/changelog/+relative-spec-skip-path.security.rst b/docs/changelog/+relative-spec-skip-path.security.rst index fa2bef1..f406090 100644 --- a/docs/changelog/+relative-spec-skip-path.security.rst +++ b/docs/changelog/+relative-spec-skip-path.security.rst @@ -1,3 +1,2 @@ -A relative path spec such as ``../venv/bin/python`` is now looked up only in the working directory. Discovery no -longer joins it to ``PATH`` entries or searches uv-managed installs for it, so ``..`` segments can no longer run a file -outside ``PATH``. +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``.