Skip to content

🐛 fix(discovery): don't search siblings of the base prefix - #293

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/base-search-prefix-boundary
Nov 30, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/base-search-prefix-boundary

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Resolving a Python 3.9 or 3.10 virtual environment to its base Python could search a folder next to the base install, and start any interpreter found there. With the base Python directly in a shared folder such as /tmp, another local user could put a program in that neighbouring folder and have it run as whoever resolved the environment. 🔒 Discovery now searches only inside the base install. This addresses draft advisory ANT-2026-X5XPI5K4.

base-3.10/bin/python stands in for a program another user placed next to the base install; it only records that it was started:

$ cp -a "$(dirname "$(dirname "$(readlink -f "$(uv python find 3.10)")")")" base
$ base/bin/python3.10 -m venv --copies --without-pip venv-3.10 && ln -s venv-3.10 venv
$ mkdir -p base-3.10/bin && printf '#!/bin/sh\ntouch "%s/started"\n' "$PWD" > base-3.10/bin/python && chmod +x base-3.10/bin/python
$ python -c "from python_discovery import get_interpreter; print(get_interpreter('$PWD/venv/bin/python').system_executable)"
$ ls

Before:

subprocess /home/me/demo/base-3.10/bin/python omitted query markers; stdout 0 chars
subprocess /home/me/demo/base-3.10/bin/python omitted query markers; stdout 0 chars
/home/me/demo/base/bin/python3.10
base
base-3.10
started
venv
venv-3.10

After:

/home/me/demo/base/bin/python3.10
base
base-3.10
venv
venv-3.10

Folders are now compared by whole path components with os.path.commonpath instead of str.startswith.

Python 3.11 and later find the base through getpath and do not use this search. A base install at the file system root now maps correctly, where it used to produce a broken path. Windows was not tested, because the test machine cannot create symlinks.

Reported by oss-scanner: ANT-2026-X5XPI5K4.

Deciding that an executable lies inside the environment used a bare
string prefix, so <dir>/venv-3.11/bin counted as inside <dir>/venv. Its
leftover "-3.11/bin" was then appended to the base prefix, and discovery
ran whatever interpreter-named file sat in <base>-3.11/bin before the
real base install. A user who can create entries in a shared directory
could plant that file and have it returned as the base interpreter.

Compare whole path components instead, so a folder is only mapped onto
the base prefix when it is really inside the environment. A prefix that
ends in a separator, such as the filesystem root, now maps correctly too.

Reported by oss-scanner as ANT-2026-X5XPI5K4.
@gaborbernat gaborbernat added bug Something isn't working security Security fixes and advisories labels Nov 29, 2026
os.path.commonpath also raises when it mixes a relative executable folder
with an absolute prefix, not only for paths on different Windows drives.
@gaborbernat
gaborbernat merged commit 53ea06e into tox-dev:main Nov 30, 2026
24 checks passed
@gaborbernat
gaborbernat deleted the fix/base-search-prefix-boundary branch November 30, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working security Security fixes and advisories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant