From a uv workspace member, an agent-mode scan patches the Python on PATH instead of the workspace .venv: an OS package is modified, the project is reported as patched and vex attests it

Open
#2,308 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
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python, rust
Domain
backend, cli

Research direction

Start with core/crawlers/python_crawler.rs, especially local_sites_with, uv_owns_project_env, and the adjacent uv_project_environment_is_the_project_env test; read utils/uv_workspace.rs for the existing workspace-root lookup. Run the proposed a_uv_workspace_member_never_reads_the_interpreter_on_path test and inspect how workspace environment paths are resolved. Done means agent-mode scans from a member use the workspace environment and never fall back to the Python on PATH.

Written by the indexing model from the issue text.

Description

agent:triaged arch-audit bug priority:p1

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L48), on main at cfc870f.

Problem

In a uv workspace, uv.lock and the environment (.venv) live at the workspace root, and uv sync or uv run from a member directory use that environment. A member directory holds only its own pyproject.toml.

An agent-mode run from a member directory (cd packages/lib && socket-patch scan, or --cwd packages/lib) does not know the member belongs to a workspace. It sees a pyproject.toml with no uv.lock and no .venv beside it, takes the directory for a project with no environment of its own, and reads the Python on PATH instead. When that interpreter holds a package the member declares (any version of it), the run patches that copy:

  1. The wrong copy is patched. The file written belongs to the OS Python (/usr/lib/python3/dist-packages/six.py, owned by the distribution's python3-six package), not to the project. Every other program on the machine that imports it now runs the patched bytes, and dpkg --verify reports the file as modified.
  2. The project stays unpatched and is reported as patched. The scan says Applied 1 patch and Patched 1 package, exit 0. apply --check says Patches are in sync. uv run from the same directory still imports the unpatched file from the workspace's .venv.
  3. The VEX statement is false. vex writes not_affected for the member's product. When the workspace locks another version than the OS has, the statement is for a version the workspace does not install at all.
  4. With nothing matching in the OS Python, the advice is wrong. The scan says No packages found: the 25 packages installed in the Python on PATH are not ones this project declares. Install its requirements first, though uv sync has installed them.

A relative UV_PROJECT_ENVIRONMENT has the same cause: uv resolves it against the workspace root, socket-patch against the member directory, finds nothing there and reads the Python on PATH.

The rule that is broken is stated in the code and has been fixed three times, one package manager at a time: "A uv project or script lock installs only into uv's own env. With none synced yet nothing is installed for it […]; the OS Python is never its env (#964)" (core crawlers/python_crawler.rs, lines 3628 to 3630). The test for a uv project is a uv.lock in the directory itself (uv_owns_project_env, line 1125), which a workspace member never has.

Vendored and hosted mode already know the layout. utils/uv_workspace.rs walks up to the [tool.uv.workspace] root, and both modes refuse a member with its name (#1138, fixed in 2a11629f). The contract lists it under "Reads bounded to the repository root": "a uv workspace pyproject.toml above (redirect_workspace_lockfile_elsewhere, pypi_uv_workspace_unsupported)". pixi workspace members have a rule of their own in the crawler ("From a workspace member directory […] the environment pixi run / pixi shell activated for that workspace is used instead"). The agent-mode crawler has neither for uv.

Who it hurts

  • A uv monorepo that runs socket-patch per package directory, the way the contract describes for Python monorepos ("invoking the tool once per subproject (--cwd each), as a per-directory CI step would"), on a machine whose system Python has any of the member's dependencies. The Ubuntu image these runs used has six, PyYAML, cryptography, PyJWT, pillow, packaging, pyparsing, httplib2, oauthlib and setuptools in /usr/lib/python3/dist-packages. The versions need not match the workspace's: the scan looks up patches for the version the OS has.
  • CI and container builds most of all: they run as root, so the write to the OS package succeeds, and nobody has activated the workspace's .venv (with VIRTUAL_ENV set to it the run is correct).
  • Whoever reads the VEX document, and whatever else on the machine imports the OS package.

Evidence on main

The binary is built from cfc870f. uv is 0.11.32. The machine's python3-six is 1.16.0-4. The patch API is a local stand-in that serves one patch for pkg:pypi/[email protected]; the patched six.py defines SOCKET_PATCHED.

$ cat pyproject.toml                       # the workspace root, which also holds uv.lock and .venv
[project]
name = "app"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = ["lib"]

[tool.uv.sources]
lib = { workspace = true }

[tool.uv.workspace]
members = ["packages/*"]
$ cat packages/lib/pyproject.toml
[project]
name = "lib"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = ["six==1.16.0"]
$ uv sync
 + lib==0.1.0 (from file://<w>/w1/packages/lib)
 + six==1.16.0

$ cd packages/lib
$ uv run python -c 'import six; print(hasattr(six, "SOCKET_PATCHED"), six.__version__, six.__file__)'
False 1.16.0 <w>/w1/.venv/lib/python3.11/site-packages/six.py
$ dpkg --verify python3-six                # clean

$ socket-patch scan --mode agent
Found 1 package (1 python)
Warning: PyPI: run `pip check` (or `uv pip check`) to verify .dist-info/RECORD consistency. …
Applied 1 patch.
  HIGH  [email protected]  CVE-2026-0001
Next steps:
  1. Commit .socket/ to keep the patches.
  2. Run `socket-patch apply` after every `pip install`, locally and in CI. A fresh install removes the patches.

Patched 1 package, fixing 1 vulnerability (1 high).
…                                                           (exit 0)

$ uv run python -c 'import six; print(hasattr(six, "SOCKET_PATCHED"), six.__version__, six.__file__)'
False 1.16.0 <w>/w1/.venv/lib/python3.11/site-packages/six.py
$ grep -c SOCKET_PATCHED <w>/w1/.venv/lib/python3.11/site-packages/six.py /usr/lib/python3/dist-packages/six.py
<w>/w1/.venv/lib/python3.11/site-packages/six.py:0
/usr/lib/python3/dist-packages/six.py:1
$ 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/[email protected] -O - | jq -c '.statements[] | {status, impact_statement}'
{"status":"not_affected","impact_statement":"Patched via Socket patch dddddddd-0000-4000-8000-000000000001"}
$ socket-patch scan --mode agent --json | jq -c '[.events[] | .action], [.warnings[].code]'     # on a fresh copy
["downloaded","applied"] []

socket-patch rollback from the member restores the OS file.

The same workspace locking six==1.17.0, which has no patch here. The OS still has 1.16.0:

$ uv run python -c 'import six; print(six.__version__, six.__file__)'
1.17.0 <w>/w2/.venv/lib/python3.11/site-packages/six.py
$ socket-patch scan --mode agent
Found 1 package (1 python)
Applied 1 patch.
  HIGH  [email protected]  CVE-2026-0001
Patched 1 package, fixing 1 vulnerability (1 high).         (exit 0)
$ dpkg --verify python3-six
??5??????   /usr/lib/python3/dist-packages/six.py
$ socket-patch vex --product pkg:generic/[email protected] -O - | jq -c '.statements[] | {status, products: [.products[] | .subcomponents[]? | ."@id"]}'
{"status":"not_affected","products":["pkg:pypi/[email protected]"]}

A member whose dependency (iniconfig==2.0.0) the OS Python does not have:

$ uv run python -c 'import iniconfig; print(iniconfig.__file__)'
<w>/w3/.venv/lib/python3.11/site-packages/iniconfig/__init__.py
$ socket-patch scan --mode agent
No packages found: the 25 packages installed in the Python on PATH are not ones this project declares. Install its requirements first; `--global` scans them with every other package on the machine.   (exit 0)

A relative UV_PROJECT_ENVIRONMENT=.venv-ci, synced from the member (uv puts it at <w>/w4/.venv-ci):

$ UV_PROJECT_ENVIRONMENT=.venv-ci socket-patch scan --mode agent --dry-run      # in packages/lib
Found 1 package (1 python)                                                      # the OS copy
$ UV_PROJECT_ENVIRONMENT=.venv-ci socket-patch scan --mode agent --dry-run      # in the workspace root
Found 2 packages (2 python)                                                     # six and lib, in .venv-ci

Controls, all correct:

Run Result
scan --mode agent from the workspace root patches <w>/w1/.venv; uv run from the member imports the patched file; the OS file is untouched
the member, with VIRTUAL_ENV=<w>/w1/.venv the same
scan --mode vendored from the member exit 1: ". is a member of the uv workspace rooted at <w>/w1, which installs it from <w>/w1/uv.lock […] nothing was written"
a lone uv project (uv.lock beside pyproject.toml, no .venv) [email protected] is listed from the lock as not installed and skipped; nothing is read from the OS Python (#964)

A unit test beside uv_project_environment_is_the_project_env (core crawlers/python_crawler.rs) fails on main:

#[tokio::test]
async fn a_uv_workspace_member_never_reads_the_interpreter_on_path() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().join("ws");
    let member = root.join("packages").join("lib");
    std::fs::create_dir_all(&member).unwrap();
    std::fs::write(
        root.join("pyproject.toml"),
        "[project]\nname = \"app\"\nversion = \"0.1.0\"\ndependencies = [\"lib\"]\n\n\
         [tool.uv.sources]\nlib = { workspace = true }\n\n\
         [tool.uv.workspace]\nmembers = [\"packages/*\"]\n",
    )
    .unwrap();
    std::fs::write(root.join("uv.lock"), "version = 1\n").unwrap();
    std::fs::write(
        member.join("pyproject.toml"),
        "[project]\nname = \"lib\"\nversion = \"0.1.0\"\ndependencies = [\"six==1.16.0\"]\n",
    )
    .unwrap();
    let workspace_site = fake_venv(&root, ".venv");

    // Control: from the workspace root, the env is the workspace's.
    let (root_sites, root_shared) = local_sites_with(&root, &env_of(&[])).await;
    assert_eq!(root_sites, vec[workspace_site]);
    assert!(!root_shared);

    let (sites, shared) = local_sites_with(&member, &env_of(&[])).await;
    assert!(
        !shared,
        "a uv workspace member was given the interpreter on PATH: {sites:?}"
    );
}
$ cargo test -p socket-patch-core --lib a_uv_workspace_member_never_reads
a uv workspace member was given the interpreter on PATH: ["/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. 0 passed; 1 failed

Cause

local_sites_with (core crawlers/python_crawler.rs, lines 3613 to 3650) tries the project's own environments, then asks whether a package manager owns the project (is_pipenv_project, uv_owns_project_env, poetry_owns_project_env, pixi_owns_project_env), and otherwise hands a directory with a Python marker the interpreter on PATH (project_interpreter_sites, line 3647). Each ownership test reads only --cwd:

  • uv_owns_project_env (line 1121) wants <cwd>/uv.lock;
  • uv_project_environment_site_packages (line 1081) joins a relative UV_PROJECT_ENVIRONMENT onto --cwd (line 1091);
  • the .venv probe (line 789) looks in --cwd.

A workspace member fails all three and falls through.

The class has a history of one-sided fixes. "A project its package manager installs elsewhere falls back to the system Python" was fixed for Pipenv (#504, #947; 54bc27f0), for a lone uv project (#964; 00df0c42) and for Poetry (#1023; 83969ec3). "A uv workspace member is read as a project of its own" was fixed for vendored and hosted mode (#1138; 2a11629f), which added the walk in utils/uv_workspace.rs. pixi members got their rule with pixi support (e123f6b2). The agent-mode crawler is the copy none of them reached.

Planned fix

The part that is not in doubt: a directory that utils/uv_workspace.rs finds governed by a uv workspace is a uv project, so the interpreter on PATH is never its environment.

What it reads instead is the workspace's environment, as uv does and as pixi members already get: UV_PROJECT_ENVIRONMENT resolved against the workspace root, else <root>/.venv. With neither present nothing is installed for it, as for a fresh uv clone. When the walk leaves the repository with a pyproject.toml above it (the contract's "readers withhold" case), it reads nothing. The human hint then names the workspace root instead of telling the user to install.

One point a maintainer may want to weigh in on: reading the workspace's environment means a run from one member patches the .venv every member shares, and records the patch in the member's own .socket/. If a run from a member should refuse instead (the vendored-mode answer), say so here; the first paragraph of the fix holds either way.

No change to JSON shapes, flags or exit codes. CLI_CONTRACT.md's "cwd-only" paragraph gains the uv member sentence beside the pixi one.

Also seen, not part of this issue

  • An agent-mode scan from an npm workspace member (node_modules hoisted to the root) says No packages found. Run your package manager's install first. Nothing is written and nothing is reported as patched.
  • With COMPOSER=composer-alt.json (Composer's variable for another manifest name), the scan reads composer.json, finds none and says the same line.

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.