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

未关闭
#2,293 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, rust
领域
devtools, security

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Rust
星标
9
派生
2
平均合并
1 天 8 小时
30 天内合并 PR
313

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

SocketDev/socket-patch 的其他 Issue

查看 SocketDev/socket-patch 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。