Skip to content

🐛 fix(env): create a requested env dir privately - #1290

Merged
gaborbernat merged 6 commits into
pypa:mainfrom
gaborbernat:fix-env-dir-ownership
Nov 18, 2026
Merged

gaborbernat merged 6 commits into
pypa:mainfrom
gaborbernat:fix-env-dir-ownership

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Resolve symbolic links in --env-dir before checking it, and recreate the location with access by the running user only.

AI-generated supporting detail, not written by the author, but reviewed closely by them

--env-dir PATH and DefaultIsolatedEnv(path=...) accepted any existing empty directory, whoever owned it, and resolved symbolic links in the path only after checking it (old lines). After a successful build, build deleted the resolved path. This addresses draft advisory ANT-2026-3Y7YNDRN.

What changes

DefaultIsolatedEnv.__enter__ now does two things.

  1. Resolves the path first. The emptiness check used to run on the path as given, and os.path.realpath ran afterwards. An account that controlled a symbolic link at the location could point it at an empty directory for the check and at a different one for the resolution, and build then created its environment in that directory and deleted it after a successful build. Every check now runs on the resolved path.
  2. Recreates an existing empty directory with os.rmdir and os.mkdir(path, 0o700). The owner of a directory can rename the entries in it, so an account that pre-created the location could move lib aside and supply a site-packages with a sitecustomize.py, which Python imports at startup in each hook subprocess. After the recreation the running user owns the directory and nobody else can enter it. In a sticky directory such as /tmp, removing another user's directory is not permitted (inode(7)), so the build fails there.

Behavior changes

An existing empty location is removed and created again, so it loses its previous mode and owner and ends up with mode 0700. A location that cannot be removed, such as a mount point or another user's directory under /tmp, now fails with the OSError from rmdir. A file at the location still fails with is not a directory, and a directory with entries with is not empty.

Not covered: build does not check who owns or can write to the directories above the location. An account that controls one of them can replace the environment while the build runs. The CLI reference tells users to keep the parent directory writable by their user only.

Reported by Anthropic's OSS Scanner: ANT-2026-3Y7YNDRN, ANT-2026-TSYGXDSU.

A requested environment location was accepted whenever it was empty, and
its path was resolved again after that check. An account that owned the
directory could swap the environment's contents for its own code, and one
that controlled a symbolic link could point creation and the cleanup after
a successful build at a directory it could not otherwise touch.

Resolve the path once, require an existing directory to belong to the
current user, and on exit remove the location only while it is still the
directory checked on entry.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 18, 2026
@gaborbernat
gaborbernat force-pushed the fix-env-dir-ownership branch from 4fd75c4 to ff341be Compare November 18, 2026 01:28
The rejection was only exercised by POSIX tests, so the Windows jobs
measured its lines as uncovered. Swap the symbolic link test for one that
replaces the location with a file, which runs on every platform.
@gaborbernat
gaborbernat marked this pull request as ready for review November 18, 2026 05:03
Comparing owners drew an arbitrary line and refused setups that work today.
Recreate the location instead: removing the empty directory and creating it
again with mode 0700 makes it ours, so its previous owner can no longer swap
what build installs, and it fails where the removal is not permitted.

Resolving the path before the checks is kept. The identity check on exit
goes: rmtree already refuses a symbolic link at the path it is given.
@gaborbernat gaborbernat changed the title 🐛 fix(env): check ownership and identity of a requested env dir 🐛 fix(env): create a requested env dir privately Nov 18, 2026
@gaborbernat
gaborbernat merged commit cfb444f into pypa:main Nov 18, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants