From 1363d53e66de211c0f899b5c2999f6c8a6413fea Mon Sep 17 00:00:00 2001 From: HugoXK Date: Mon, 16 Nov 2026 03:21:20 +0000 Subject: [PATCH 1/2] fix(hosts): hand the log path to the driver as an argument, not shell syntax (#1109) The wrapper built its launch command as a shell string and spliced the log path into it inside single quotes (`"$QUOTED >> '$LOG' 2>&1"`). An apostrophe in `--store` closed the quote, and everything after it ran as shell words outside the worker's process group, with the operator's privileges; `/home/o'connor/` was enough to misfire and `;` made it code execution. The page prescribes that invocation, so a normal Ticket path could reach it. Both drivers now receive the log path and the run's command as arguments: bash -c 'exec >>"$1" 2>&1; shift; exec "$@"' _ "$LOG" "${CMD[@]}" `exec` keeps the run in the process the driver started (same process group, same exit status), `shift` drops the log path from argv, and neither value is parsed by a shell again. `waiting.json` keeps every field it had; `command` is still the `%q`-quoted display string and is never executed. The new test row is red on `main` for both drivers -- a store named `it's; > $PWNED; echo '$HOME` created the injected file and the wrapper still exited 0 -- and green after the fix, with `worker.log` written at the literal path. The setsid row runs through a PATH shim without tmux so it cannot go stale on a host that has tmux. Docs: one sentence on the page's handoff section, plus a Security fragment. --- changelog.d/1109-store-quote.md | 6 ++++ docs/dsh-super-agent.md | 5 +++ scripts/dsh-superagent-run.sh | 13 ++++++-- tests/test_dsh_superagent_run.py | 55 ++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 changelog.d/1109-store-quote.md diff --git a/changelog.d/1109-store-quote.md b/changelog.d/1109-store-quote.md new file mode 100644 index 00000000..bdd6ca0b --- /dev/null +++ b/changelog.d/1109-store-quote.md @@ -0,0 +1,6 @@ +Security +The handoff wrapper no longer builds its launch command as a shell string: the +log path and the run's command reach `tmux`/`setsid` as arguments, so a `--store` +containing `'`, `;`, `$` or a space is used literally instead of closing the +quote and running the rest of the path as shell words outside the worker's +process group (#1109). diff --git a/docs/dsh-super-agent.md b/docs/dsh-super-agent.md index da6cb139..40d99681 100644 --- a/docs/dsh-super-agent.md +++ b/docs/dsh-super-agent.md @@ -162,6 +162,11 @@ with `run_id`, `round`, `state`, `run_dir`, `driver`, `session` and `log`, and exits 0 within seconds. Post the run id, the round and the run directory on the Ticket and end the turn; nothing holds the slot. +The wrapper hands the log path and the run's command to the driver as +arguments rather than splicing them into a shell string, so a `--store` +containing a quote, a space, `$` or `;` is used literally instead of being read +as shell syntax (`#1109`). + A store is reused for every Ticket, so it already holds earlier runs. The wrapper only reports a `status.json` that appears or changes after it launches, so a handoff into a used store reports `found: false` until its own run writes diff --git a/scripts/dsh-superagent-run.sh b/scripts/dsh-superagent-run.sh index 85e4312b..6d9d5ef0 100644 --- a/scripts/dsh-superagent-run.sh +++ b/scripts/dsh-superagent-run.sh @@ -44,7 +44,16 @@ LOG="$STORE/worker.log" # both in a directory called `runs` -- so the session carries a per-launch # suffix. Two handoffs then never ask tmux for the same name (#1084 review). SESSION="ad-$(printf '%s' "$(basename "$STORE")" | tr -c 'A-Za-z0-9_.-' '-')-$$-$RANDOM" +# Report text only: this string is what `waiting.json` shows as `command`, and +# nothing ever executes it. QUOTED="$(printf '%q ' "${CMD[@]}")" +# The log path and the command reach the driver as **arguments**, never spliced +# into a shell string: the old `>> '$LOG'` let an apostrophe in --store close the +# quote, and the rest of the path ran as shell words outside the worker's process +# group with the operator's privileges (#1109). `exec >>"$1"` opens the log, +# `shift` drops it from argv, and the run is exec'd in this process, so it keeps +# the same process group and exit status and nothing re-parses either value. +LAUNCH=(bash -c 'exec >>"$1" 2>&1; shift; exec "$@"' _ "$LOG") # What the store already holds, taken before the launch: a reused store always # has earlier runs in it, and reporting the newest of those as this handoff is @@ -67,11 +76,11 @@ with open(out, "w", encoding="utf-8") as fh: PY if command -v tmux >/dev/null 2>&1; then DRIVER=tmux - tmux new-session -d -s "$SESSION" -c "$PWD" "$QUOTED >> '$LOG' 2>&1" \ + tmux new-session -d -s "$SESSION" -c "$PWD" "${LAUNCH[@]}" "${CMD[@]}" \ || die "tmux refused the session $SESSION" elif command -v setsid >/dev/null 2>&1; then DRIVER=setsid - setsid sh -c "$QUOTED >> '$LOG' 2>&1" >/dev/null 2>&1 & + setsid "${LAUNCH[@]}" "${CMD[@]}" >/dev/null 2>&1 & else die "neither tmux nor setsid is available; this host cannot hand off a run" fi diff --git a/tests/test_dsh_superagent_run.py b/tests/test_dsh_superagent_run.py index 33aebbb7..61f62767 100644 --- a/tests/test_dsh_superagent_run.py +++ b/tests/test_dsh_superagent_run.py @@ -135,6 +135,61 @@ def test_the_page_documents_a_wrapper_that_exists_and_takes_its_flags(): assert "--detach" in evolve.stdout, "the page tells a worker to pass a flag the CLI dropped" +def _path_without_tmux(base: Path) -> str: + """A PATH holding the wrapper's tools and no `tmux` (the fallback row). + + The setsid row has to reach the fallback on a host that has tmux installed, + so `command -v tmux` must fail without also removing bash or python3. + """ + shim = base / "shim" + shim.mkdir() + for tool in ("bash", "sh", "basename", "tr", "mkdir", "python3", "setsid", + "sleep", "rm"): + found = shutil.which(tool) + assert found is not None, f"the wrapper needs {tool}, which this host lacks" + os.symlink(found, shim / tool) + return str(shim) + + +@pytest.mark.parametrize("driver", ["tmux", "setsid"]) +def test_a_store_path_with_shell_metacharacters_hands_off_without_running_them( + tmp_path, driver): + # The launch command used to be built as a shell string with the log path + # spliced in as `>> '$LOG'`: an apostrophe in --store closed the quote, and + # the rest of the path became shell words run outside the worker's process + # group with the operator's privileges (#1109). The path below carries an + # apostrophe, a semicolon, spaces and a dollar sign. + pwned = tmp_path / "PWNED" + store = tmp_path / f"it's; > {pwned}; echo '$HOME" + + env = dict(os.environ, DSH_RUN_HANDOFF_WAIT="1") + if driver == "tmux": + if shutil.which("tmux") is None: + pytest.skip("no tmux on this host: the session row cannot run") + else: + if shutil.which("setsid") is None: + pytest.skip("no setsid on this host: the fallback row cannot run") + env["PATH"] = _path_without_tmux(tmp_path) + + handed = subprocess.run( + ["bash", str(WRAPPER), "--store", str(store), "--", "sleep", "0.2"], + cwd=REPO, capture_output=True, text=True, timeout=60, env=env) + + assert handed.returncode == 0, handed.stderr + assert not pwned.exists(), ( + f"the --store path ran as shell code and created {pwned}; " + f"wrapper stderr: {handed.stderr!r}") + report = json.loads((store / "waiting.json").read_text(encoding="utf-8")) + assert report["driver"] == driver, report + assert report["log"] == str(store / "worker.log"), report + assert report["command"].strip() == "sleep 0.2", report + # `waiting.json` is the page's contract; a quoting fix must not rename, add + # or drop a field the read-back documents. + assert set(report) == {"status", "found", "run_id", "round", "state", + "run_dir", "driver", "session", "log", "command"} + assert (store / "worker.log").is_file(), report + + @pytest.mark.skipif(os.environ.get("AGENTDESCENT_LONG_RUN_SMOKE") != "1", reason="set AGENTDESCENT_LONG_RUN_SMOKE=1 to run the detached smoke") def test_a_run_outlives_the_turn_that_started_it_and_leaves_no_orphan(tmp_path): From dc16eef30f1cb315001968d62deb74a00092eeb6 Mon Sep 17 00:00:00 2001 From: HugoXK Date: Mon, 16 Nov 2026 03:22:58 +0000 Subject: [PATCH 2/2] test(hosts): measure that the handed-off run actually started (#1109) The red row asserted the injection and the log file, both of which a broken launch can produce without ever starting the run. The child now writes a marker through the handed-off stdout, so "the run starts" is measured; re-checked against `f5cc52ef`'s wrapper, where both driver rows still fail on the injected file. --- tests/test_dsh_superagent_run.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_dsh_superagent_run.py b/tests/test_dsh_superagent_run.py index 61f62767..9c842caf 100644 --- a/tests/test_dsh_superagent_run.py +++ b/tests/test_dsh_superagent_run.py @@ -171,8 +171,14 @@ def test_a_store_path_with_shell_metacharacters_hands_off_without_running_them( pytest.skip("no setsid on this host: the fallback row cannot run") env["PATH"] = _path_without_tmux(tmp_path) + # The child writes a marker, so "the run started" is measured rather than + # inferred from a log file the redirection alone would have created. + marker = tmp_path / "ran.txt" + child = [sys.executable, "-c", + "import pathlib, sys; pathlib.Path(sys.argv[1]).write_text('ran\\n')", + str(marker)] handed = subprocess.run( - ["bash", str(WRAPPER), "--store", str(store), "--", "sleep", "0.2"], + ["bash", str(WRAPPER), "--store", str(store), "--"] + child, cwd=REPO, capture_output=True, text=True, timeout=60, env=env) assert handed.returncode == 0, handed.stderr @@ -182,12 +188,14 @@ def test_a_store_path_with_shell_metacharacters_hands_off_without_running_them( report = json.loads((store / "waiting.json").read_text(encoding="utf-8")) assert report["driver"] == driver, report assert report["log"] == str(store / "worker.log"), report - assert report["command"].strip() == "sleep 0.2", report + assert report["command"].strip().startswith(sys.executable), report # `waiting.json` is the page's contract; a quoting fix must not rename, add # or drop a field the read-back documents. assert set(report) == {"status", "found", "run_id", "round", "state", "run_dir", "driver", "session", "log", "command"} assert (store / "worker.log").is_file(), report + assert marker.read_text(encoding="utf-8") == "ran\n", ( + f"the run did not start through the handed-off log: {report}") @pytest.mark.skipif(os.environ.get("AGENTDESCENT_LONG_RUN_SMOKE") != "1",