Skip to content

fix(tests): test helpers write to the real home outside pytest #4739

Closed

Description

@dan-petty

Problem Description

On 2026-11-09 at 19:18:52 UTC, a helper of the tests wrote the 22-byte stub #!/bin/sh\necho mytool\n into the real user data root /home/vscode/.local/share/devops-cli/tools/<tool>/<version>/. This content is _EXECUTABLE in tests/support/tools.py. On 2026-11-11, 18 files are still stubs: k9s, argocd, cloudflared, gh-dash, gitleaks, golangci-lint, kube-linter, kubeconform, kubectl-argo-rollouts, osv-scanner, pluto, popeye, tflint, tuicr, bandit/1.9.4/bin/bandit, semgrep/1.179.0/bin/semgrep, bin/pysemgrep and devcontainers-cli/0.89.0/node_modules/@devcontainers/cli/devcontainer.js.

Cause (proved). An agent ran a test helper outside pytest. At 19:18:48 UTC, an Antigravity agent session that worked on #1336 (worktree issue1336_openspec_plans) ran this command from the root of its worktree:

python3 -c '
from pathlib import Path
import tempfile
from tests.support.tools import link_every_tool_at_pin
from devops_cli.commands.devcontainer import _bootstrap_managed_tools

with tempfile.TemporaryDirectory() as td:
    p = Path(td)
    link_every_tool_at_pin(p)
    res = _bootstrap_managed_tools(p)
    print("At pin result:", res)
    file_path = p / "a_file"
    file_path.write_text("x")
    warn = _bootstrap_managed_tools(file_path)
    print("Warn result:", warn)
'

No pytest run started, so no conftest ran:

  1. The autouse fixture isolate_user_data_root (tests/conftest.py) did not set DEVOPS_CLI_USER_DATA_ROOT.
  2. isolate_home (tests/support/bootstrap.py) did not change HOME.
  3. link_at_pin gets each path from installed_commands(), which calls user_data_root() (src/devops_cli/core/repo.py).
  4. With no variable, user_data_root() returns CONST_USER_DATA_ROOT, which is Path.home() / ".local/share/devops-cli" (src/devops_cli/config/constants.py).
  5. The temporary directory held only the links. The helper wrote the stub into the version directory of each tool of tools.lock, in the real home.

The second call, _bootstrap_managed_tools(file_path), started a real install_managed_tools into the real root with a file as the target directory. It downloaded kustomize (19:18:59 UTC) and argo (19:19:06 UTC) again and started argocd. The agent stopped the task at 19:19:06 UTC. The empty folder argocd/.stage-uih9h51c stays.

Why the floor did not stop it. Inside pytest, the floor isolates HOME and the user data root, so this helper is safe there. The helper itself has no guard: it writes to the path that the product code resolves, also when no pytest run is active. The two write guards of the floor (config_write_guard.py, spend_write_guard.py) refuse only config files and spend ledgers. No guard refuses another file under the real home.

Ruled out. A pytest run with --noconftest, a different rootdir, a copied test file, a test that clears the environment, and a fixture order problem. The full devops ci run of another worktree ran from 19:05 to 19:21 UTC, but its tests ran under the floor.

Effects. ~/.local/bin comes before /usr/local/bin on PATH, so each stub hides the real tool. In another worktree, devops openspec archive reported "Archived" at 19:21:23 and 19:21:47 UTC and moved nothing. A third agent found openspec list --json printing mytool and repaired openspec.js by hand at 19:22:01 UTC. devops install-tools --check and status still report each stub as at pin (comment on #4363).

Reproduction Steps

This reproduction sets HOME to a new temporary folder. Do not run it with the real HOME.

tmp=$(mktemp -d); mkdir -p "$tmp/home" "$tmp/links"
cd <repository root>
env -u DEVOPS_CLI_USER_DATA_ROOT HOME="$tmp/home" .venv/bin/python -c '
import sys; sys.path.insert(0, ".")
from pathlib import Path
from tests.support.tools import link_at_pin
link_at_pin("gitleaks", Path(sys.argv[1]) / "links")
' "$tmp"
find "$tmp/home" -type f

Result on 2026-11-11 at d2e75974: $tmp/home/.local/share/devops-cli/tools/gitleaks/8.30.1/gitleaks, 22 bytes, #!/bin/sh\necho mytool\n. With the real HOME, the same call writes into the tools of the developer.

Expected vs Actual Behavior

Expected: A helper of the tests writes only inside the temporary directory of a pytest run, and refuses to write when no pytest run is active. A test that writes a file under the real home of the developer fails at that write and names itself.

Actual: The helper writes to the path that the product code resolves. With no pytest run, that path is in the real home. The floor refuses only config files and spend ledgers outside the run directory.

Proposed fix at the source:

  1. A support helper that writes a file gets its root from a fixture (tmp_path or isolate_user_data_root), not from a product default.
  2. Before each write, the helper refuses a path outside the temporary directory of the pytest run (the root that config_write_guard.py records at pytest_configure). With no run, there is no root, so the helper refuses. pytest 8.2 and later sets PYTEST_VERSION in the environment during a run; the helper can also refuse at once when this variable is absent.
  3. The floor adds a PEP 578 audit hook (sys.addaudithook), in the same form as config_write_guard.py and spend_write_guard.py. It fails a test that opens for writing, renames onto or removes a file under the real home (the HOME before isolate_home changes it) and outside the run directory.
  4. A test proves each refusal: a helper call outside a run, and a write under the recorded real home in a test.

#4363 removes tests/support/tools.py together with tools_lock. The guard is still necessary, because other support code can run outside pytest in the same way, and the floor has no guard for a write under the real home.

Environment Diagnostics

  • devops-cli 0.112.2 (d2e75974) for the reproduction. At the event, tests/support/tools.py held the text of aea5adc8 (the agent read it at 19:18:44 UTC).
  • Python 3.14.10, pytest 9.1.1.
  • Devcontainer on a Linux host (WSL2, rootless Podman). /home/vscode is on a persistent home volume.

Logs, Traces, or Terminal Output

  • Antigravity transcript of session 4279abea: .system_generated/logs/transcript_full.jsonl, steps 3153 to 3160. Step 3153 (19:18:44 UTC) sends the command. Step 3159 (19:19:00 UTC) stops the task. tasks/task-3154.log holds At pin result: [].
  • File times (UTC), in the order of tools.lock: argocd 19:18:52.922, cloudflared .927, kubectl-argo-rollouts .931, kube-linter .940, popeye .945, pluto .948, kubeconform .952, tflint .960, k9s .970, gh-dash .977, tuicr .981, gitleaks .983, osv-scanner .987, golangci-lint .990, semgrep 19:18:53.015, pysemgrep .016, bandit .027, devcontainer.js .034.
  • Real binaries from the second call: kustomize/5.8.3/kustomize 19:18:59.608 (13443232 bytes), argo/4.1.5/argo 19:19:06.324 (196450586 bytes). Empty folder argocd/.stage-uih9h51c 19:19:06.672.
  • Probable, not proved: the helper also wrote kubectl 1.37.1, helm 4.3.0, trivy 0.75.0 and promtool 3.15.1. The second call replaced kubectl and helm, and later installs on 2026-11-10 replaced trivy and promtool. The repair of openspec.js at 19:22:01 UTC is proved (session a862087a, steps 1744 to 1777).

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Activity

  1. added
    source/agent
    type/bug
    scope/tests
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/tests. The (tests) scope of the title gives the scope.
    • Priority: P2-Medium. The helper already corrupted 18 files in a real home, but triggering it requires a non-standard manual call outside pytest.
    • Value: Medium. It prevents silent corruption of the user's real tool stubs when a helper is run outside the pytest isolation floor.
    • Effort: Low. Adding a guard that checks for an active pytest session or a set environment variable in the helper is a small change.
    • Related: fix(ci): tests touch no state outside their temporary directories #1311, which asks for a different change. Both address test code writing outside its intended scope, but #1311 covers writes during pytest runs while this candidate covers writes when a helper is invoked outside pytest.
  3. added
    priority/p2-medium
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 11, 2026
  4. removed
    status/in-review
    owner/agent
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 12, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-12 04:08 UTC

  6. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    On 2026-11-12 one stub of this incident is still in the workstation home: ~/.local/share/devops-cli/tools/kubeconform/0.8.0/kubeconform is a 22-byte script that prints mytool (date 2026-11-09). ~/.local/bin/kubeconform links to it, so kubeconform and the commands that start it (devops k8s validate, devops security kubeconform) validate nothing. kubectl, helm, promtool and kustomize are real binaries. A person must install kubeconform 0.8.0 again. Found during #4991 (PR #5030).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/testssource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions