Repository navigation
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 agentprints "Patched 1 package" and the manifest records the patch, butpdm runimports the unpatched__pypackages__copy.vexattests the vulnerability asnot_affected. Users and CI gating onsocket-patch vexget a false attestation. - Hosted / vendored (fails closed, but stuck): after the scan,
pdm syncinstalls the patched wheel into__pypackages__. Thenvexchecks the stale.venvcopy, omits the patch asnot_appliedand 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
vexcheck it"): the env PDM installs into, here__pypackages__/<X.Y>/lib, gets patched and is whatvexchecks. CLI_CONTRACT.md: VEX only attests patches whose files hold the patched content. - Actual: the unused
.venvcopy 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
pdm: 2026-11-26 run, filed #2292 #2293, compact state
mikolalysenko commentedon Nov 28, 2026
[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 defaultuse_venv = false, so PDM installs into__pypackages__/3.8/lib), and urllib3 1.26.18 already synced there. Thenscan --mode vendored:.pdm.tomlpdm sync(__pypackages__)vexpypi_pdm_legacy_sync_required,pypi_pdm_stale_installnot_affected+vendored_tree_out_of_synchint.venv/bin/python(empty venv)pypi_pdm_legacy_sync_requiredonlynot_affected, no warningWith 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 maincfc870f(Linux), with the base-interpreter control between the two runs.Repro (local mock patch API serving a marker patch for
urllib3@1.26.18):It's the same root cause (
pdm_project_site_packagesreturns the venv before consultingpdm_uses_venv,crates/socket-patch-core/src/crawlers/python_crawler.rs:899), so a fix there should also be checked against vendored mode: thepypi_pdm_stale_installprobe incrates/socket-patch-core/src/vendor/pypi.rs(around line 983) and vendoredvex's tree check.Generated by Claude Code