From 9fd276d4afb00d37d3ad54b048f7af46e3623715 Mon Sep 17 00:00:00 2001 From: georgiedekker Date: Sat, 14 Nov 2026 07:57:19 +0000 Subject: [PATCH] test(workspace): run only the programs the service issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stand-in executed whatever argv the caller posted. CodeQL flags that as py/command-line-injection, and it is a fair reading of the code even though the caller is our own test: nothing constrained it. It now runs find, cat and python and refuses anything else with a 403, so a regression that starts shelling out to a new program fails here instead of executing it. The refusal is loud on purpose — a silent fallback would let the test pass without exercising what it meant to. Verified: ruff clean; full suite 2910 passed, 5 skipped, coverage 93.19%. Co-Authored-By: Claude Code --- tests/unit/workspace_server.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/unit/workspace_server.py b/tests/unit/workspace_server.py index 2172e6f..73647ea 100644 --- a/tests/unit/workspace_server.py +++ b/tests/unit/workspace_server.py @@ -22,6 +22,16 @@ #: takes its remote (exec) path, as it does in the cluster. REMOTE_CODEBASE_PATH = "/nonexistent-remote-workspace" +#: The only programs this stand-in will run — exactly the ones the service under +#: test issues: ``find`` and ``cat`` for reads, ``python -c`` for the inspect +#: step. Anything else is refused rather than executed, so a regression that +#: starts shelling out to something new fails here loudly instead of running it. +ALLOWED_PROGRAMS = frozenset({"find", "cat", "python"}) + + +class RefusedCommandError(Exception): + """The posted ``argv[0]`` is not one this stand-in is allowed to run.""" + class WorkspaceServer: def __init__( @@ -58,8 +68,14 @@ def __exit__(self, *exc: object) -> None: def _exec(self, body: dict) -> dict: argv = list(body["command"]) + program = argv[0] if argv else "" + if program not in ALLOWED_PROGRAMS: + raise RefusedCommandError( + f"refusing to run {program!r}: this stand-in only runs " + f"{', '.join(sorted(ALLOWED_PROGRAMS))}" + ) self.commands.append(argv) - if argv and argv[0] == "python": + if program == "python": argv[0] = sys.executable proc = subprocess.run( argv, @@ -126,6 +142,11 @@ def do_POST(self) -> None: # noqa: N802 self._reply(404, {"detail": "not found"}) return body = json.loads(self.rfile.read(int(self.headers["Content-Length"]))) - self._reply(200, server._exec(body)) + try: + result = server._exec(body) + except RefusedCommandError as exc: + self._reply(403, {"detail": str(exc)}) + return + self._reply(200, result) return Handler