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

Open
#2,293 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, rust
Domain
devtools, security

Research direction

Start in crates/socket-patch-core/src/crawlers/python_crawler.rs, especially pdm_project_site_packages near line 899 and pdm_uses_venv near line 909; then read the PDM compatibility section in docs/testing/pdm-compatibility.md. Reproduce the supplied PDM 2.29.7 case with python.use_venv off and compare the scanned environment with pdm run imports. Done when the active __pypackages__ install is selected for patching and VEX checks it, with the compatibility docs corrected.

Written by the indexing model from the issue text.

Description

agent:triaged bug bughunt pm:pdm priority:p1

[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 [email protected]. 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

Dominant language
Rust
Stars
9
Forks
2
Avg merge
1d 8h
Merged PRs (30d)
313

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SocketDev/socket-patch

All issues in SocketDev/socket-patch

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.