Hardening Python AI Test Pipelines Against .pth Startup Hooks

#179

Key Takeaways

A green pytest run, clean dist-info/RECORD ownership checks, or running Python with the -I isolated flag does not prove a test runner is free of malicious startup hooks. Testers testing Python AI workflows should pair dual-lane dependency cooldowns with post-install .pth allowlist audits in credential-free CI jobs and strip unnecessary token-minting permissions and request-inspection egress from test runs.

Read Today’s Notes

  • Why green tests do not guarantee a clean runner: A compromised package—such as agent-framework versions 1.26.1 and 1.26.2 on November 17—can leave application logic and test assertions passing while a .pth file executes automatically when Python starts (including during pytest, pip install, or pip uninstall).
  • How .pth startup hooks execute: CPython’s site.py executes lines inside .pth files that begin with “import ” (space) or “import\t” (tab) before test code runs, even when the package itself is never imported.
  • Why common checks fall short: Running python -I still processes .pth files in site-packages, and a .pth file packaged inside a compromised wheel is recorded as legitimately owned in dist-info/RECORD.
  • Runtime token minting and exfiltration: Even without static .env files on disk, runners with id-token: write or cloud managed-identity access can allow malware to request fresh OIDC or managed-identity tokens at runtime. In the November 19 analysis by Amar Jeer at dope.security, credential file targets expanded from up to 60 in version 1.26.1 to up to 80 in version 1.26.2 (adding GitHub Actions OIDC and /home/runner targeting), citing SafeDep via The Hacker News on runtime token requests and researchers reviewing 50 stored requests on a webhook.site endpoint (with a 100-request free-tier cap per unclaimed URL) where 17 requests tied to three unique victims while the true total remained unknown.
  • Gate 1 — Dual-lane cooldown: Because bots like Renovate opened upgrade PRs within six minutes of the 1.26.1 upload, hold routine updates for seven days (for example via pip 26’s –uploaded-prior-to P7D flag, uv’s exclude-newer, Dependabot cooldown, or Renovate’s minimumReleaseAge) while maintaining a separate, human-reviewed fast lane for urgent security fixes.
  • Gate 2 — Startup-hook allowlist audit and clean recovery: Run an allowlist check such as Lycore Development’s pth_audit.py right after dependency installation inside a credential-free CI job. If a host is suspected of compromise, do not run pip uninstall; inspect using python -S or non-Python shell tools, rebuild from a clean base, and rotate all reachable credentials.
  • Gate 3 — Non-minting test isolation: Remove id-token: write from test and install jobs (reserving it for deployment steps) and block outbound traffic to request-inspection services such as webhook.site.
  • Operator Move and 5-day drill: Audit one Python AI test repository for unexpected executable .pth files against an allowlist, run a seven-day cooldown dry run with pip or uv, and verify id-token: write is absent from test workflows. Over a five-day challenge, test a harmless stderr-logging .pth canary in a throwaway local virtual environment with pytest and python -I, add pth_audit.py to a credential-free CI job, configure a seven-day cooldown with a documented urgent override, strip id-token: write and block webhook.site egress, and practice a clean rebuild and credential-rotation runbook.

Companion Newsletter

Passing tests tell you whether your assertions hold, not whether your execution environment remained untouched before your test suite began. In Python AI testing, teams frequently pull fast-moving dependencies into local virtual environments and CI runners, where a trojanized wheel can keep tests green while a .pth file runs automatically at interpreter startup.

For testers, this shifts the boundary of verification from test results alone to environment integrity. Because site.py runs .pth import lines at startup, python -I does not stop site-packages .pth execution, and wheel ownership metadata includes files shipped inside the wheel, relying on green runs or dist-info/RECORD checks leaves blind spots. Furthermore, a runner without a static .env file can still be exposed if CI permissions or cloud identity roles allow fresh token requests at runtime.

Today, you can pick one Python AI test repository and inspect what enters your environment before tests run. Check site-packages against an approved .pth filename allowlist, run a seven-day cooldown dry run with pip or uv to see what recent releases would be deferred, and check your test workflow definitions to confirm id-token: write is not granted to test or install steps.

Research and References