Skip to content

PDM project with a venv interpreter and python.use_venv off: agent mode patches the unused venv and vex attests not_affected while __pypackages__ stays unpatched #2293

Backlog

Description

[agent] Found by the scheduled PDM bug-hunt routine (ledger #312).

Summary

When PDM's saved interpreter (.pdm-python, or [python] path in .pdm.toml on PDM 1.x) is a venv interpreter but python.use_venv is off, PDM installs into PEP 582 __pypackages__/<X.Y>/lib and runs the project from there. socket-patch's crawler takes the saved venv as the project env without looking at use_venv. When that venv also holds a copy of the package (it was synced before the project switched to PEP 582, or the user pip-installed into it), agent mode patches that unused copy and reports success. vex then attests not_affected while the code the project actually imports is unpatched: a false VEX attestation.

PDM picks the env in Project.get_environment as PythonEnvironment if config["python.use_venv"] and self.python.get_venv() is not None else PythonLocalEnvironment (the same in 2.12.4 and 2.29.7). PDM 1.4.5 uses config["use_venv"] and is_venv_python(...), with use_venv defaulting to False. So on PDM 1.x a venv interpreter in .pdm.toml means __pypackages__ by default, and on PDM 2.x it does once pdm config -l python.use_venv false is set.

This differs from the closed #502 / #528 / #609. Those covered a base interpreter with use_venv off, and a saved venv with use_venv on. Here it's a saved venv with use_venv off. docs/testing/pdm-compatibility.md says "The first of them that is an environment wins: a venv … A base interpreter means PEP 582 only when python.use_venv is off". That ordering doesn't match PDM: use_venv off means PEP 582 even when the interpreter is a venv.

Impact

  • Agent: scan --mode agent prints "Patched 1 package" and the manifest records the patch, but pdm run imports the unpatched __pypackages__ copy. vex attests the vulnerability as not_affected. Users and CI gating on socket-patch vex get a false attestation.
  • Hosted / vendored (fails closed, but stuck): after the scan, pdm sync installs the patched wheel into __pypackages__. Then vex checks the stale .venv copy, omits the patch as not_applied and exits with "No applied patches…", and the scan's stale-install warning points at the .venv. Following the remedy can never produce a VEX document.

Repro (PDM 2.29.7, Linux)

mkdir demo && cd demo
printf '[project]\nname = "demo"\nversion = "0.1.0"\nrequires-python = ">=3.9"\ndependencies = ["urllib3==1.26.18", "idna==3.7"]\n\n[tool.pdm]\ndistribution = false\n' > pyproject.toml
python3.11 -m venv .venv && echo "$PWD/.venv/bin/python" > .pdm-python   # what `pdm use .venv` records
pdm lock && pdm sync                       # installs into .venv
pdm config -l python.use_venv false        # switch the project to PEP 582
pdm sync                                   # now installs into __pypackages__/3.11/lib
pdm run python -c 'import urllib3; print(urllib3.__file__)'   # .../__pypackages__/3.11/lib/urllib3/__init__.py
socket-patch scan --mode agent --yes      # "Patched 1 package"
grep -c SOCKET_PATCHED .venv/lib/python3.11/site-packages/urllib3/response.py  # 1 (unused copy patched)
grep -c SOCKET_PATCHED __pypackages__/3.11/lib/urllib3/response.py             # 0 (the copy PDM runs)
socket-patch vex                           # statement: not_affected  <- false

(Patch data came from a local mock of the patch API serving a marker patch for urllib3@1.26.18. The Socket hosts aren't reachable from the sandbox.)

PDM 1.4.5 needs no config: .pdm.toml [python] path = "<proj>/.venv/bin/python" with urllib3 in both .venv and __pypackages__ gives the same result (venv patched, __pypackages__ not, vex attests). With nothing installed in .venv, the agent scan patches nothing: "1 vulnerability left to patch".

Expected vs actual

  • Expected (docs/testing/pdm-compatibility.md, "The installed env is the one PDM records … Agent mode patches the env found, and the hosted and vendored stale-install warnings and vex check it"): the env PDM installs into, here __pypackages__/<X.Y>/lib, gets patched and is what vex checks. CLI_CONTRACT.md: VEX only attests patches whose files hold the patched content.
  • Actual: the unused .venv copy is patched and attested.

Versions

PDM config socket-patch agent result vex
2.29.7 .pdm-python → .venv, pdm.toml use_venv = "false" main cfc870f (2 fresh runs) .venv patched, __pypackages__ not false not_affected
2.29.2 same main same false
2.12.4 same main same false
2.29.7 same v5.1.0 release same false (not a regression)
1.4.5 .pdm.toml python.path → .venv (default use_venv false) main same false
2.29.7, hosted same main pdm sync patches __pypackages__ not_applied (fails closed, stale .venv checked)

The crawler logic isn't OS-specific (Scripts\python.exe / Lib\site-packages on Windows), so I expect the same on macOS and Windows. Not run there.

Suspect code

crates/socket-patch-core/src/crawlers/python_crawler.rs:899 (pdm_project_site_packages): interpreters.iter().find_map(|i| env_root_of_interpreter(i)) returns the venv's site-packages before pdm_uses_venv (line 909) is consulted. pdm_uses_venv should gate the venv branch too, with its existing PDM 1.x default (off for a legacy .pdm.toml project). The doc paragraph in docs/testing/pdm-compatibility.md ("The first of them that is an environment wins") would need the same correction.


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    on Nov 26, 2026
  2. added a commit that references this issue on Nov 26, 2026
  3. mikolalysenko commented on Nov 28, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] New information from the scheduled PDM bug-hunt routine (ledger #312): vendored mode on PDM 1.x has a quieter failure than the "fails closed" case described above.

    Shape: PDM 1.5.3, .pdm.toml [python] path → an empty in-project .venv (the default use_venv = false, so PDM installs into __pypackages__/3.8/lib), and urllib3 1.26.18 already synced there. Then scan --mode vendored:

    interpreter in .pdm.toml scan warnings after pdm sync (__pypackages__) vex
    base CPython 3.8 (control) pypi_pdm_legacy_sync_required, pypi_pdm_stale_install upstream (PDM 1.x keeps the same-version install) not_affected + vendored_tree_out_of_sync hint
    .venv/bin/python (empty venv) pypi_pdm_legacy_sync_required only upstream not_affected, no warning

    With the saved venv, the stale-install probe and the vex tree check both look at the empty .venv, find no installed copy, and stay silent. The user follows the scan's instructions (pdm sync), PDM 1.x keeps the unpatched __pypackages__ copy (pdm run python -c 'import urllib3; print(urllib3.__file__)' → __pypackages__/3.8/lib/urllib3/__init__.py), and nothing tells them to remove it. Reproduced twice on main cfc870f (Linux), with the base-interpreter control between the two runs.

    Repro (local mock patch API serving a marker patch for urllib3@1.26.18):

    printf '[project]\nname = "demo"\nversion = "0.1.0"\nrequires-python = ">=3.8"\ndependencies = ["urllib3==1.26.18"]\n' > pyproject.toml
    python3.8 -m venv .venv && pdm use -f .venv/bin/python   # PDM 1.5.3
    pdm lock && pdm sync                                      # installs into __pypackages__/3.8/lib
    socket-patch scan --mode vendored --yes --json            # no pypi_pdm_stale_install
    pdm sync                                                  # keeps upstream
    socket-patch vex --json --output v.json                   # no vendored_tree_out_of_sync

    It's the same root cause (pdm_project_site_packages returns the venv before consulting pdm_uses_venv, crates/socket-patch-core/src/crawlers/python_crawler.rs:899), so a fix there should also be checked against vendored mode: the pypi_pdm_stale_install probe in crates/socket-patch-core/src/vendor/pypi.rs (around line 983) and vendored vex's tree check.


    Generated by Claude Code

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

    agent:triagedbugbughuntpm:pdmpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions