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

Đang mở
#2,310 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
56/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, rust
Lĩnh vực
backend, devtools

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Rust
Star
9
Fork
2
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
313

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của SocketDev/socket-patch

Tất cả issue của SocketDev/socket-patch

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.