Skip to content

Seed cleanup looks up outside paths before containment #3624

Description

@gaborbernat

The app-data seeder checks paths from an existing distribution's RECORD or top_level.txt before deciding whether they are inside the environment. An absolute entry can therefore cause a filesystem lookup outside the environment during reseeding, even though cleanup will not remove it. The Windows network effect of a UNC entry has not been tested.

Affected code

Mechanism

The seeder joins each metadata name to site-packages. pathlib discards the base when the right-hand path is absolute. The resulting path enters exists() and is_symlink() before the resolved-path containment check; a missing path skips that check and its warning. Current _bytecode_of calls glob() on the parent of a .py entry. RECORD normally lists paths relative to the installed library directory, so the absolute entry requires malformed metadata.

Reproduction

This script records calls to Path.exists and Path.glob using only temporary local paths. It invokes the same cleanup function on a temporary environment.

from pathlib import Path, PureWindowsPath
from tempfile import TemporaryDirectory
from unittest.mock import patch

from virtualenv.seed.embed.via_app_data.pip_install.base import PipInstall

with TemporaryDirectory() as tmp:
    root = Path(tmp)
    dist = root / "venv" / "lib" / "python3.15" / "site-packages" / "pip-1.0.dist-info"
    dist.mkdir(parents=True)
    outside = root / "outside-missing.py"
    (dist / "RECORD").write_text(f"{outside},,\n", encoding="utf-8")
    calls = []
    original_exists, original_glob = Path.exists, Path.glob

    def traced_exists(path):
        if path == outside:
            calls.append("exists outside")
        return original_exists(path)

    def traced_glob(path, pattern):
        if path == outside.parent / "__pycache__":
            calls.append("glob outside __pycache__")
        return original_glob(path, pattern)

    with patch.object(Path, "exists", traced_exists), patch.object(Path, "glob", traced_glob):
        PipInstall._uninstall_dist(dist, root / "venv")
    print(f"outside_calls={calls!r}")
    print(f"outside_exists={outside.exists()}")

print(PureWindowsPath(r"C:\project\venv\Lib\site-packages") / "//host/share/x")

Run with PYTHONPATH=src python3 repro.py from each checkout. The archived 8e09166 checkout needed its generated src/virtualenv/version.py supplied before import. Output:

8e09166d: outside_calls=['exists outside']
8e09166d: outside_exists=False
8e09166d: \\host\share\x
ebe06113: outside_calls=['glob outside __pycache__', 'exists outside']
ebe06113: outside_exists=False
ebe06113: \\host\share\x

Expected: cleanup should reject a name outside the environment before any filesystem query on that name or its derived bytecode directory. Paths such as ../../../bin/pip that remain inside the environment still need to work.

Impact

This requires reseeding an existing environment without --clear whose seed-package metadata has a malformed absolute path. The lookup is reproducible with that setup (moderate). Such metadata is unusual and already resides in a trusted environment directory (unlikely real-world exposure). A .pth file in the same site-packages directory can already run code when that environment starts, so this lookup has no demonstrated additional attacker capability. On Windows, PureWindowsPath joins //host/share/x to \\host\share\x; SMB traffic or credential use from an actual Windows run has not been observed.

The lookup remains after #3428, which added the post-lookup containment check. A fix needs to cover the earlier bytecode glob() on the current branch as well as the cleanup loop.

Credits

Reported by oss-scanner: ANT-2026-5PAH96SM.

Activity

  1. added 5 commits that reference this issue on Nov 20, 2026
    ab23807
    5f930fb
    7e8ca0d
    f4e7018
    c29a8d7
  2. added a commit that references this issue on Nov 20, 2026
    6f0966f
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