A PDM project with no usable .pdm-python is read from the Python on PATH: OS packages are patched and attested as the project's, while PDM's own venv stays unpatched

オープン
#2,310 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
56/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, rust
領域
backend, devtools

調査の方向性

Start in core crawlers/python_crawler.rs, especially pdm_project_site_packages, managed_or_local_site_packages, and local_sites_with; compare PDM environment resolution with the behavior described for PDM 2.29.2. Add the fresh checkout regression test beside poetry_fresh_checkout_has_no_env_and_no_global_fallback, and cover the hashed out of tree venv case. Update the two contract paragraphs in CLI_CONTRACT.md; done means PDM projects no longer scan the shared PATH interpreter and the tests cover their resolved environments.

索引モデルが issue の本文から書いたものです。

説明

agent:triaged arch-audit bug priority:p1

[agent] Found by the architecture refactor routine, which ran agent mode against real PDM 2.29.2 and compared the copy pdm run imports with the copy the scan patched. This settles lead L50 of the register.

The problem

For a PDM project the Python crawler finds the environment through .pdm-python (or PDM_PYTHON). When that file is absent, ignored or stale, it tries $VIRTUAL_ENV, ./.venv, ./venv and __pypackages__, finds nothing, and then reads the Python on PATH as "a project with no environment of its own".

PDM never installs a project into that interpreter. With no saved interpreter it takes an activated venv, then ./.venv, ./venv or ./env, then the project's venvs under venv.location (<data dir>/pdm/venvs/<directory name>-<hash of the path>-*), and creates a venv when none exists (Project.resolve_interpreter and iter_venvs in PDM 2.29.2). With python.use_venv off it uses __pypackages__.

Two states reach the fallback:

  1. PDM keeps the venv outside the project (venv.in_project = false) and .pdm-python does not name it. The scan patches the copy in the Python on PATH, reports the project as patched, apply --check says in sync and vex attests not_affected. pdm run still imports the unpatched copy from PDM's venv.
  2. A checkout before its first pdm install. The scan reads every package of the Python on PATH, unscoped because of pdm.lock, and patches each one that has a patch, whether or not the project declares it.

In both, socket-patch writes to an interpreter the project never uses. In a container or a CI job that runs as root, that is the OS Python.

Evidence on main (cfc870f)

All runs use the binary built from main, PDM 2.29.2, a stand-in patch API with one patch for pkg:pypi/[email protected], and a PATH whose python3 is the OS Python 3.11 (it holds python3-six 1.16.0). The project is a pyproject.toml with dependencies = ["six==1.16.0"] and a [tool.pdm] table.

State 1: the venv is outside the project and .pdm-python is ignored
$ cat pdm.toml
[venv]
in_project = false
$ pdm install
Virtualenv is created successfully at <home>/.local/share/pdm/venvs/p2-5hYsFcQL-3.11
$ export PDM_IGNORE_SAVED_PYTHON=1
$ pdm run python -c 'import six; print(hasattr(six, "SOCKET_PATCHED"), six.__version__, six.__file__)'
False 1.16.0 <home>/.local/share/pdm/venvs/p2-5hYsFcQL-3.11/lib/python3.11/site-packages/six.py
$ socket-patch scan --mode agent
Found 25 packages (25 python)
Applied 1 patch.
  HIGH  [email protected]  CVE-2026-0001
Patched 1 package, fixing 1 vulnerability (1 high).                              (exit 0)
$ pdm run python -c 'import six; print(hasattr(six, "SOCKET_PATCHED"), six.__version__, six.__file__)'
False 1.16.0 <home>/.local/share/pdm/venvs/p2-5hYsFcQL-3.11/lib/python3.11/site-packages/six.py
$ dpkg --verify python3-six
??5??????   /usr/lib/python3/dist-packages/six.py
$ socket-patch apply --check
Patches are in sync (1 patch checked).                                           (exit 0)
$ socket-patch vex --product pkg:generic/app@1 -O - | jq -c '.statements[] | {status, impact_statement}'
{"status":"not_affected","impact_statement":"Patched via Socket patch eeeeeeee-0000-4000-8000-000000000001"}

socket-patch apply over a committed .socket/ gives the same result after a fresh pdm install: 1 of 1 targeted patch applied, exit 0, the OS copy patched and PDM's copy not.

Three ways into this state, each run:

.pdm-python How it gets there Lasts
ignored PDM_IGNORE_SAVED_PYTHON=1 in the environment (socket-patch honors the variable too) as long as the variable is set
absent git clean -fdx, or a fresh checkout at a path whose venv still exists (the file is in PDM's default .gitignore) until the next PDM command saves it again
stale it names a venv that was deleted until the next PDM command

In the last two, once PDM saves the file again apply --check exits 1 and rollback keeps the record with a warning that names rollback pkg:pypi/[email protected] -g. That part is right. The OS copy stays patched until the user runs it.

With .pdm-python present and honored, the same project is patched in PDM's venv. So are an in-project .venv with no .pdm-python, a PEP 582 project, a base interpreter chosen with pdm use, and an activated venv with and without PDM_IGNORE_ACTIVE_VENV.

State 2: nothing installed yet

The catalog here also holds a patch for [email protected], an OS package the project does not declare.

$ ls -a
.  ..  .git  pdm.lock  pyproject.toml
$ grep dependencies pyproject.toml
dependencies = ["six==1.16.0"]
$ socket-patch scan --mode agent --dry-run
Found 25 packages (25 python)
[dry-run] Would download and apply 2 patches. No changes made.
  CRITICAL  [email protected]  CVE-2026-0002
  HIGH      [email protected]       CVE-2026-0001
Would patch 2 packages, fixing 2 vulnerabilities (1 critical, 1 high).           (exit 0)
$ socket-patch scan --mode vendored --dry-run
Found 25 packages (25 python)
[dry-run] Would download and vendor 1 of 2 patches (1 would be refused). No changes made.
  [would-refuse] pkg:pypi/[email protected] (pypi_pdm_lock_package_missing): pdm.lock has no [[package]] entry for httplib2; run `pdm lock` first
$ pdm install
Virtualenv is created successfully at <w>/p9/.venv
$ pdm run python -c 'import six; print(six.__file__)'
<w>/p9/.venv/lib/python3.11/site-packages/six.py

The transcripts keep the lines that matter; the "Next steps" and time-saved lines are left out.

The same checkout as a uv project (uv.lock in place of pdm.lock):

$ socket-patch scan --mode agent --dry-run
Found 1 package (1 python)
Note: 1 package from project lockfiles is not yet installed (lockfile-only).
  [skip] pkg:pypi/[email protected] (not installed; run your package manager's install first, or `socket-patch scan --mode vendored` to vendor it from the lockfile)
A unit test that fails on main

In the tests of core crawlers/python_crawler.rs, beside poetry_fresh_checkout_has_no_env_and_no_global_fallback:

#[tokio::test]
async fn pdm_fresh_checkout_has_no_env_and_no_global_fallback() {
    let tmp = tempfile::tempdir().unwrap();
    let project = tmp.path().join("app");
    std::fs::create_dir_all(&project).unwrap();
    std::fs::write(
        project.join("pyproject.toml"),
        "[project]\nname = \"app\"\nversion = \"0.1.0\"\ndependencies = [\"six==1.16.0\"]\n\n[tool.pdm]\ndistribution = false\n",
    )
    .unwrap();
    std::fs::write(project.join("pdm.lock"), "[metadata]\n").unwrap();
    let home = tmp.path().join("home").to_string_lossy().into_owned();
    let var = env_of(&[("HOME", home)]);
    assert!(pdm_drives_project(&project).await);
    let (sites, shared) = local_sites_with(&project, &var).await;
    assert!(
        sites.is_empty() && !shared,
        "a PDM project read a shared interpreter: {sites:?}"
    );
}
$ cargo test -p socket-patch-core --lib -- pdm_fresh_checkout_has_no_env poetry_fresh_checkout_has_no_env
test crawlers::python_crawler::tests::poetry_fresh_checkout_has_no_env_and_no_global_fallback ... ok
test crawlers::python_crawler::tests::pdm_fresh_checkout_has_no_env_and_no_global_fallback ... FAILED
a PDM project read a shared interpreter: ["/usr/local/lib/python3.11/dist-packages", "/usr/lib/python3/dist-packages", "/usr/lib/python3.11/dist-packages", "/root/.local/lib/python3.11/site-packages"]
test result: FAILED. 1 passed; 1 failed

The out-of-tree venv needs PDM's path hash (the first eight characters of the URL-safe base64 of the MD5 of the project path), so its test belongs with the fix.

The cause

Core crawlers/python_crawler.rs:

  • pdm_project_site_packages (lines 881 to 914) returns None when no saved interpreter is usable, and leaves the rest to the generic probes.
  • managed_or_local_site_packages (lines 749 to 803) then tries $VIRTUAL_ENV, ./.venv, ./venv and __pypackages__. It never looks under PDM's venv.location, and not at ./env.
  • local_sites_with (lines 3613 to 3650) returns nothing for a project that only Pipenv, uv, Poetry or pixi installs. It has no such line for PDM, so a PDM project falls through to project_interpreter_sites.

The same class has been fixed one manager at a time: PDM's saved interpreter (#502, b0a32db3), Pipenv (#504, #947), a lone uv project (#964, 00df0c42), Poetry (#1023, 83969ec3), pixi. #2308 is the uv workspace member. PDM without a usable .pdm-python is the branch left. #2293 (a saved venv with python.use_venv off) is open against the same function, pdm_project_site_packages; one change can take both.

CLI_CONTRACT.md (line 1432 and the paragraph from line 1438) lists the environments read and says a project with none of them "installs into the interpreter on PATH". For a PDM project that is not so, as the runs above show.

Planned fix

  • Resolve a PDM project's environment the way PDM does when no saved interpreter is usable: the activated venv (unless PDM_IGNORE_ACTIVE_VENV), then ./.venv, ./venv, ./env, then the venvs under venv.location whose name starts with PDM's prefix for the project, then __pypackages__.
  • A project PDM drives (pdm_drives_project) never reads the Python on PATH. With no environment yet it has no installed packages, and its lock-only packages come from pdm.lock, as for uv, Poetry and Pipenv.
  • Update the two contract paragraphs.

The second point changes what the contract documents for a pdm.lock project with no environment. Four of the five managers already behave that way. A maintainer who wants PDM to keep the PATH fallback should say so here; the first point stands either way.


Generated by Claude Code

主要言語
Rust
スター
9
フォーク
2
平均マージ
1日 8時間
マージ済み PR(30日)
313

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

SocketDev/socket-patch のほかの issue

SocketDev/socket-patch の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。