Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.d/1109-store-quote.md
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 5 additions & 0 deletions docs/dsh-super-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions scripts/dsh-superagent-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
63 changes: 63 additions & 0 deletions tests/test_dsh_superagent_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,69 @@ 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)

# 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), "--"] + child,
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().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",
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):
Expand Down
Loading