From e80c26bd1665c3b5cefcd0b84f88628b1682f662 Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sat, 31 Oct 2026 21:12:11 -0400 Subject: [PATCH] fix(plugins): keep Windows system variables in plugin subprocess envs The plugin installer and the plugin worker run their children with a POSIX-only allowlist (PATH, HOME, LANG, TMPDIR, ...). On Windows that drops SYSTEMROOT, which Winsock and the TLS providers need: pip fails to build its SSL context, so every plugin install exited 2, and a worker cannot open a socket (WinError 10106), so asyncio and HTTP clients fail before plugin code runs. Keep SYSTEMROOT, WINDIR, TEMP, TMP, USERPROFILE and PATHEXT when present. They carry no credentials; provider keys are still dropped. --- deeptutor/plugins/lifecycle.py | 7 ++++++- deeptutor/plugins/runtime.py | 7 ++++++- tests/plugins/test_lifecycle.py | 16 ++++++++++++++++ tests/plugins/test_runtime.py | 27 +++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/deeptutor/plugins/lifecycle.py b/deeptutor/plugins/lifecycle.py index 0ed61622a..de717953b 100644 --- a/deeptutor/plugins/lifecycle.py +++ b/deeptutor/plugins/lifecycle.py @@ -377,7 +377,12 @@ def _venv_python(root: Path) -> Path: def _clean_env() -> dict[str, str]: - keep = ("PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "SSL_CERT_FILE", "SSL_CERT_DIR") + keep = ( + *("PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "SSL_CERT_FILE", "SSL_CERT_DIR"), + # Windows counterparts: without SYSTEMROOT, Winsock and the TLS + # providers fail to load, so pip cannot even build its SSL context. + *("SYSTEMROOT", "WINDIR", "TEMP", "TMP", "USERPROFILE", "PATHEXT"), + ) env = {key: os.environ[key] for key in keep if key in os.environ} for key in ("HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "http_proxy", "https_proxy"): if key in os.environ: diff --git a/deeptutor/plugins/runtime.py b/deeptutor/plugins/runtime.py index d856f44fe..e973dcea8 100644 --- a/deeptutor/plugins/runtime.py +++ b/deeptutor/plugins/runtime.py @@ -513,7 +513,12 @@ def _stream_event(raw: Any, *, source: str) -> StreamEvent: def _clean_env() -> dict[str, str]: - keep = ("PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "SSL_CERT_FILE", "SSL_CERT_DIR") + keep = ( + *("PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "SSL_CERT_FILE", "SSL_CERT_DIR"), + # Windows counterparts: without SYSTEMROOT a worker cannot open a + # socket (WinError 10106), so asyncio and HTTP clients fail at start-up. + *("SYSTEMROOT", "WINDIR", "TEMP", "TMP", "USERPROFILE", "PATHEXT"), + ) return {key: os.environ[key] for key in keep if key in os.environ} diff --git a/tests/plugins/test_lifecycle.py b/tests/plugins/test_lifecycle.py index fa0f7cfeb..fe317891b 100644 --- a/tests/plugins/test_lifecycle.py +++ b/tests/plugins/test_lifecycle.py @@ -3,11 +3,13 @@ from __future__ import annotations import json +import os from pathlib import Path import zipfile import pytest +from deeptutor.plugins import lifecycle from deeptutor.plugins.lifecycle import ( LifecycleResult, PluginLifecycleError, @@ -150,3 +152,17 @@ def test_manifest_from_wheel_is_parsed_without_importing_plugin(tmp_path) -> Non record = manager.registry.get_plugin(result.plugin_id) assert record is not None and record.manifest is not None assert parse_manifest(record.manifest.to_dict()).id == "org.author.example" + + +def test_install_env_keeps_windows_system_variables_only(monkeypatch, tmp_path) -> None: + # pip builds an SSL context before it reads a local wheel; on Windows that + # fails without SYSTEMROOT and the install exits 2. + windows = ("SYSTEMROOT", "WINDIR", "TEMP", "TMP", "USERPROFILE", "PATHEXT") + for key in windows: + monkeypatch.setenv(key, os.environ.get(key) or str(tmp_path)) + monkeypatch.setenv("DEEPTUTOR_TEST_PROVIDER_KEY", "not-for-plugins") + + env = lifecycle._clean_env() + + assert {key: env.get(key) for key in windows} == {key: os.environ[key] for key in windows} + assert "DEEPTUTOR_TEST_PROVIDER_KEY" not in env diff --git a/tests/plugins/test_runtime.py b/tests/plugins/test_runtime.py index dcba89595..a2b47b6f5 100644 --- a/tests/plugins/test_runtime.py +++ b/tests/plugins/test_runtime.py @@ -4,6 +4,7 @@ from __future__ import annotations import os from pathlib import Path +import subprocess import sys from types import SimpleNamespace @@ -276,3 +277,29 @@ def test_one_failed_worker_does_not_block_other_managed_extensions(tmp_path, mon workers = runtime.load_enabled_workers(registry) assert isinstance(workers[0], _Capability) + + +_WINDOWS_SYSTEM_ENV = ("SYSTEMROOT", "WINDIR", "TEMP", "TMP", "USERPROFILE", "PATHEXT") + + +def test_worker_env_keeps_windows_system_variables_only(monkeypatch, tmp_path) -> None: + for key in _WINDOWS_SYSTEM_ENV: + monkeypatch.setenv(key, os.environ.get(key) or str(tmp_path)) + monkeypatch.setenv("DEEPTUTOR_TEST_PROVIDER_KEY", "not-for-plugins") + + env = runtime._clean_env() + + assert {key: env.get(key) for key in _WINDOWS_SYSTEM_ENV} == { + key: os.environ[key] for key in _WINDOWS_SYSTEM_ENV + } + assert "DEEPTUTOR_TEST_PROVIDER_KEY" not in env + + +def test_worker_env_can_open_a_socket() -> None: + # On Windows a child without SYSTEMROOT fails here with WinError 10106, + # before any plugin code (asyncio, httpx, ...) gets to run. + probe = "import socket; socket.socket().close()" + completed = subprocess.run( + [sys.executable, "-c", probe], env=runtime._clean_env(), capture_output=True + ) + assert completed.returncode == 0, completed.stderr.decode(errors="replace")