diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-11 11:40:17 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-11 11:40:17 -0800 |
| commit | cc613ea3def275a3a67718df43716f3064aee30c (patch) | |
| tree | fb6af9d8649e81a60ab48c29f08a85799e5d1790 /plugins | |
| parent | 2168ef2f28ed9d02a46c502b2c3026cdd119c9b0 (diff) | |
| parent | e02b97831fc2ef09e28d20a66a1f486f493d1cf6 (diff) | |
Merge pull request #7073 from anthropics/mhegazy/sg-pip-temp-real-dir
security-guidance: keep the pip temp folder to a real folder of this user
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/ensure_agent_sdk.py | 33 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_sdk_install_timeout.py | 43 |
2 files changed, 73 insertions, 3 deletions
diff --git a/plugins/security-guidance/hooks/ensure_agent_sdk.py b/plugins/security-guidance/hooks/ensure_agent_sdk.py index 30119f8c..22b41489 100644 --- a/plugins/security-guidance/hooks/ensure_agent_sdk.py +++ b/plugins/security-guidance/hooks/ensure_agent_sdk.py @@ -18,6 +18,7 @@ from __future__ import annotations import importlib.util import json import os +import stat import subprocess import sys import time @@ -496,18 +497,34 @@ def _pip_temp_root(state_dir) -> Path: return Path(state_dir) / "pip-tmp" +def _is_own_real_dir(path) -> bool: + """True for a real directory (not a link) of this user. Raises OSError.""" + st = os.lstat(path) + if not stat.S_ISDIR(st.st_mode): + return False + if os.name == "nt": + # A junction also reads as a directory here. + return not st.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT + return st.st_uid == os.getuid() + + def _sweep_pip_temp(state_dir) -> None: """Remove pip temp folders left by a bootstrap that was killed before it could remove its own. Best-effort; a folder still in use is too young.""" + root = _pip_temp_root(state_dir) try: - entries = list(_pip_temp_root(state_dir).iterdir()) + if not _is_own_real_dir(root): + if root.is_symlink(): + root.unlink() + return + entries = list(root.iterdir()) except OSError: return # the usual case: nothing was ever left import shutil now = time.time() for entry in entries: try: - if now - entry.stat().st_mtime > _PIP_TEMP_STALE_SEC: + if _is_own_real_dir(entry) and now - entry.lstat().st_mtime > _PIP_TEMP_STALE_SEC: shutil.rmtree(entry, ignore_errors=True) except OSError: pass @@ -522,7 +539,11 @@ def _run_pip_install(cmd, state_dir): tmp = env = None try: root = _pip_temp_root(state_dir) - root.mkdir(parents=True, exist_ok=True) + root.mkdir(mode=0o700, parents=True, exist_ok=True) + if not _is_own_real_dir(root): + raise OSError("pip temp folder is not ours") + if os.name != "nt": + root.chmod(0o700) # an older version left it open under umask 002 tmp = tempfile.mkdtemp(dir=root) # tempfile reads TMPDIR, TEMP, TMP in that order on every platform. env = {**os.environ, "TMPDIR": tmp, "TEMP": tmp, "TMP": tmp} @@ -687,6 +708,12 @@ def main() -> tuple[int, str, str]: _venv_was_emptied = False _skipped_for_pip_timeout = False # Before any early return: nothing else ever clears the state dir. + # Narrow an existing state dir first; a new one is made only if needed. + if os.path.isdir(_resolve_state_dir()): + try: + make_state_dir(_resolve_state_dir()) + except OSError: + pass _sweep_pip_temp(_resolve_state_dir()) # The SDK only serves the LLM reviews. With those switched off, or the # whole plugin (SECURITY_GUIDANCE_DISABLE=1), a venv of a few hundred MB diff --git a/plugins/security-guidance/tests/test_sdk_install_timeout.py b/plugins/security-guidance/tests/test_sdk_install_timeout.py index 92edde46..f7f4f8d6 100644 --- a/plugins/security-guidance/tests/test_sdk_install_timeout.py +++ b/plugins/security-guidance/tests/test_sdk_install_timeout.py @@ -226,3 +226,46 @@ def test_marker_from_the_future_does_not_stretch_the_cooldown(install): ahead = time.time() + 6 * 3600 os.utime(install.state / "agent-sdk-venv.timeout", (ahead, ahead)) assert boot._timeout_cooldown_remaining(install.state) == boot.PIP_TIMEOUT_COOLDOWN_SEC + + +def _link(target, path): + try: + os.symlink(target, path, target_is_directory=True) + except (OSError, NotImplementedError): + pytest.skip("cannot create links here") + + +def test_sweep_needs_a_real_temp_folder(install, tmp_path): + other = tmp_path / "other" + (other / "old").mkdir(parents=True) + then = time.time() - boot._PIP_TEMP_STALE_SEC - 60 + os.utime(other / "old", (then, then)) + install.state.mkdir(exist_ok=True) + _link(other, install.state / "pip-tmp") + boot._sweep_pip_temp(install.state) + assert (other / "old").is_dir() + assert not os.path.lexists(install.state / "pip-tmp") + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX ownership") +def test_temp_folder_of_another_user_is_not_used(install, monkeypatch): + root = install.state / "pip-tmp" + root.mkdir(parents=True) + (root / "tmpold").mkdir() + then = time.time() - boot._PIP_TEMP_STALE_SEC - 60 + os.utime(root / "tmpold", (then, then)) + monkeypatch.setattr(boot.os, "getuid", lambda: os.stat(root).st_uid + 1) + boot._sweep_pip_temp(install.state) + assert _left_in(root) == ["tmpold"] + install.mode = "exit" + assert boot.main()[0] == boot.BUILT + assert _left_in(root) == ["tmpold"] + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX modes") +def test_temp_folder_is_private(install): + install.mode = "exit" + (install.state / "pip-tmp").mkdir(parents=True, mode=0o775) + (install.state / "pip-tmp").chmod(0o775) + assert boot.main()[0] == boot.BUILT + assert (install.state / "pip-tmp").stat().st_mode & 0o077 == 0 |
