Skip to content

activate_this.py can resolve .pth imports from host first #3623

Description

@gaborbernat

activate_this.py can load a module from the host process while executing an environment's .pth file, even when the environment contains a module of the same name. The .pth hook can then behave differently from startup through the environment's own Python.

Affected code

At the reported commit, activate_this.py calls site.addsitedir() before moving the new path entries ahead of the host entries. The order remains in current main.

Mechanism

site.addsitedir() adds a site directory and processes its .pth files. Executable import lines in those files run during that call. At this point, the environment's site-packages follows the host's existing sys.path entries. If a .pth line imports a module that is present in both places and has not already been loaded, the host copy can win. The subsequent sys.path rotation cannot undo that import.

This differs from starting the environment's interpreter, which processes its .pth files with the environment's site-packages ahead of the host's site-packages. The desired order for .pth imports should match the environment interpreter's precedence, including the standard library's place ahead of site-packages; moving environment site-packages ahead of the standard library would introduce a separate mismatch.

Reproduction

The reporter reproduced this on 8e09166ddfbdaed3a5dbccc216d38ff1d3dbb4c2 with Python 3.14 and an environment seeded with setuptools. The host interpreter had no setuptools. Its distutils-precedence.pth imports _distutils_hack, and a same-name file in the working directory was available on the host process's sys.path.

virtualenv --no-periodic-update -p /usr/local/bin/python3.14 --setuptools bundle /tmp/env
mkdir /tmp/checkout
cd /tmp/checkout
printf '%s\n' 'print("checkout module loaded")' 'def add_shim(): pass' > _distutils_hack.py
/usr/local/bin/python3.14 -c 'import runpy; runpy.run_path("/tmp/env/bin/activate_this.py")'
/tmp/env/bin/python -c 'import _distutils_hack; print(_distutils_hack.__file__)'

The activation command loads the checkout's _distutils_hack.py; the environment interpreter loads its site-packages copy. In the reporter's recorded comparison, the activation session logged ['_distutils_hack'] from checkout-attack/_distutils_hack.py, while the environment interpreter logged no checkout modules and loaded env/lib/python3.14/site-packages/_distutils_hack/__init__.py. Expected: the .pth import resolves the environment's _distutils_hack in both sessions. The documented runpy.run_path activation is the affected workflow.

Impact

This requires activation inside an existing Python process, an environment .pth import of a module not already loaded by the host, and a same-name module earlier on the host path. The hook may run the host implementation or fail. This is a correctness bug; a working directory on sys.path can affect later ordinary imports in the same session too.

Reproducibility: moderate, because the setup needs the specific activation path and a conflicting module. Exposure: niche, because most users start the environment interpreter directly, and the module must be imported by a .pth file before activation reorders the path.

Credits

Reported by @oss-scanner: ANT-2026-ADKO6NBD.

Activity

  1. added a commit that references this issue on Nov 20, 2026
    f96f3b0
  2. added 2 commits that reference this issue on Nov 20, 2026
    c1084fb
    6579d24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions