From a60a33012be7f5cfb7062c47f8706f9216f3405a Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Sat, 28 Nov 2026 13:11:26 +0000 Subject: [PATCH] Run approved commands with a fixed minimal environment --- core/execution.py | 3 +++ tests/unit/test_execution.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/execution.py b/core/execution.py index a03edae..1bba437 100644 --- a/core/execution.py +++ b/core/execution.py @@ -105,6 +105,9 @@ class CommandRunner: stdout=staged, stderr=subprocess.DEVNULL, shell=False, + # Resolve bare executables via the OS default path and + # exclude ambient credentials and runtime overrides. + env={"PATH": os.defpath, "LC_ALL": "C"}, start_new_session=True, ) diff --git a/tests/unit/test_execution.py b/tests/unit/test_execution.py index 75910de..a2fab4c 100644 --- a/tests/unit/test_execution.py +++ b/tests/unit/test_execution.py @@ -1,8 +1,11 @@ import io +import json +import os import sys import tempfile import unittest from pathlib import Path +from unittest.mock import patch from core.execution import ( ExecutionError, @@ -43,6 +46,23 @@ class ExecutionTests(unittest.TestCase): b"SUCCESS\n", ) + def test_subprocess_environment_excludes_parent_credentials(self): + + command = self.command( + "import json, os; print(json.dumps(dict(os.environ), sort_keys=True))" + ) + output = io.BytesIO() + with patch.dict(os.environ, { + "PATH": "/untrusted", + "LC_ALL": "untrusted", + "ABSDEV_TEST_SECRET": "synthetic", + "DOCKER_HOST": "synthetic", + "PGPASSWORD": "synthetic", + }): + self.runner(command)(command, output) + + self.assertEqual(json.loads(output.getvalue()), {"PATH": os.defpath, "LC_ALL": "C"}) + def test_binary_output_preserved(self): command = self.command(