mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:04:08 +03:00
de5f9fe23f
The askpass form of sudo password delivery was selected only after a separate capability probe command, and only the local, Docker and SSH backends run that probe. Direct Modal, Managed Modal, Daytona and Vercel kept the stdin form, where a password line left unused by a sudo that hit a cached ticket (`sudo a && sudo b; cat`) is read by a later program in the same command and can be printed. The askpass prologue now makes the checks that only the probe made before, as the first steps of its existing setup condition and before any read of stdin: - for each distinct sudo command word the counted invocations run, as written in the command (`sudo`, `/usr/bin/sudo`, ...), `SUDO_ASKPASS=/bin/false <word> -A -V` with stdin from /dev/null must succeed. For `env ... sudo` the checked word is the sudo word itself, which env looks up through PATH as the shell does; - /bin/cat, /bin/rm and /bin/sleep must be executable; - after the FIFOs exist, a harmless helper (`#!/bin/sh` + `exit 0`) is written to the helper path, made mode 700 and run; only then is the FIFO-reading helper written over it. A writable but noexec temp directory therefore fails setup instead of failing authentication. The real helper is written with `>|`, so a session with noclobber (`set -C`) can still replace the harmless one. When a step fails, the existing run-time fallback runs: the password lines stay on stdin and every counted sudo keeps `-S -p ''`. `sudo_command_words` collects the words from the same marked rewrite that `has_own_stdin_or_askpass` inspects; `build_prologue` takes them as `sudo_words` (default: bare `sudo`). When the words cannot be collected, or one holds `$` or a backquote, `_transform_sudo_command` keeps the stdin form and emits no prologue. `sudo_askpass_supported_once` returns True without probing for a backend that sets the new class attribute `_sudo_askpass_runtime_checked`; the four SDK backends set it. Local, Docker and SSH keep running the probe as before. Managed Modal's own `execute` now fills the prologue's timeout token with `fill_timeout_token`, as `BaseEnvironment.execute` does. Tests: tests/tools/test_sudo_askpass_sdk_backends.py drives each SDK backend's real `execute` with the SDK or HTTP layer replaced by a local bash runner and a stand-in sudo on PATH: - `sudo true && sudo true; cat` with caller stdin `rest` prints only `rest` (on the base commit it prints the password line); - a stand-in sudo that rejects `-A` still authenticates through the stdin form in the same single command, on every SDK backend and on LocalEnvironment; - a helper the shell cannot run (a chmod that leaves it mode 600, and TMPDIR on a real noexec mount when the host has a writable one) and an absolute-path sudo that rejects `-A` while PATH sudo accepts it both fall back to the stdin form and succeed; - each distinct sudo word gets its own check line; word lists that do not match the counted sudos keep the stdin form. Controls, passing on base and on this change: commands without sudo, sudo with its own `-S`, no password, one probe run for Local, Docker and SSH, and the helper/noexec/absolute-sudo cases above. The live tests in test_sudo_askpass_delivery.py put the stand-in sudo first on PATH so the check never reaches a host sudo, and test_managed_modal_sudo_stdin.py now expects the askpass form with its timeout filled. Two live tests there pin the noclobber case: a `bash -C` command and a local backend whose BASH_ENV sets `set -C` keep the caller's input.
123 lines
4.8 KiB
Python
123 lines
4.8 KiB
Python
"""Managed Modal carries sudo password lines on the request's stdin field, not in the command.
|
|
|
|
The lines lead the caller's own stdin, the same merge ``BaseEnvironment.execute`` does, and the
|
|
command string is the prepared command: the askpass prologue (its timeout filled from the
|
|
command's timeout) plus the askpass rewrite, with no password in it.
|
|
"""
|
|
|
|
import types
|
|
|
|
import pytest
|
|
|
|
from tools import terminal_tool as tt
|
|
from tools import terminal_tool_sudo as tts
|
|
from tools import terminal_tool_sudo_askpass as askpass
|
|
|
|
CONFIGURED = "canary-configured-pw-7f3a"
|
|
PROMPTED = "canary-typed-at-prompt-91c2"
|
|
SUDO_STDIN_FLAGS = " -S -p ''"
|
|
# ManagedModalEnvironment's default timeout is 60 s; the prologue gets that plus 30.
|
|
FILLED_TIMEOUT = "90"
|
|
|
|
|
|
def _askpass_command(command: str) -> str:
|
|
rewritten, count = tts._rewrite_real_sudo_invocations(command, askpass=True)
|
|
return askpass.build_prologue(count, FILLED_TIMEOUT, askpass.has_background_sudo(command)) + rewritten
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_sudo_state(monkeypatch):
|
|
for name in ("SUDO_PASSWORD", "HERMES_INTERACTIVE", "HERMES_SINGLE_QUERY_SESSION",
|
|
"HERMES_GATEWAY_SESSION"):
|
|
monkeypatch.delenv(name, raising=False)
|
|
tts._reset_cached_sudo_passwords()
|
|
tt.set_sudo_password_callback(None)
|
|
yield
|
|
tt.set_sudo_password_callback(None)
|
|
tts._reset_cached_sudo_passwords()
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, status_code, payload):
|
|
self.status_code = status_code
|
|
self._payload = payload
|
|
self.text = ""
|
|
|
|
def json(self):
|
|
return self._payload
|
|
|
|
|
|
def _exec_payloads(monkeypatch, command, stdin_data):
|
|
"""Run *command* through the real ManagedModalEnvironment.execute over a fake HTTP transport."""
|
|
from tools import credential_files
|
|
from tools.environments import managed_modal
|
|
|
|
monkeypatch.setattr(credential_files, "get_credential_file_mounts", list)
|
|
monkeypatch.setattr(managed_modal, "resolve_managed_tool_gateway", lambda vendor: types.SimpleNamespace(
|
|
vendor=vendor, gateway_origin="https://modal-gateway.example.com",
|
|
nous_user_token="user-token", managed_mode=True))
|
|
payloads = []
|
|
|
|
def fake_request(method, url, headers=None, json=None, timeout=None):
|
|
if method == "POST" and url.endswith("/v1/sandboxes"):
|
|
return _FakeResponse(200, {"id": "sandbox-1"})
|
|
if method == "POST" and url.endswith("/execs"):
|
|
payloads.append(json)
|
|
return _FakeResponse(200, {"execId": json["execId"], "status": "completed",
|
|
"output": "ok", "returncode": 0})
|
|
if method == "POST" and url.endswith("/terminate"):
|
|
return _FakeResponse(200, {"status": "terminated"})
|
|
raise AssertionError(f"Unexpected request: {method} {url}")
|
|
|
|
monkeypatch.setattr(managed_modal.requests, "request", fake_request)
|
|
env = managed_modal.ManagedModalEnvironment(image="python:3.11")
|
|
monkeypatch.setattr(env, "_sudo_nopasswd_works", lambda: False)
|
|
try:
|
|
result = env.execute(command, stdin_data=stdin_data)
|
|
finally:
|
|
env.cleanup()
|
|
assert result == {"output": "ok", "returncode": 0}
|
|
return payloads
|
|
|
|
|
|
def test_prompted_password_goes_on_the_stdin_field_ahead_of_caller_stdin(monkeypatch):
|
|
monkeypatch.setenv("HERMES_INTERACTIVE", "1")
|
|
monkeypatch.setattr(tts, "_prompt_for_sudo_password",
|
|
lambda timeout_seconds=45, *, command="": PROMPTED)
|
|
|
|
(payload,) = _exec_payloads(monkeypatch, "sudo tee /tmp/out", "caller-data\n")
|
|
|
|
assert payload["command"] == _askpass_command("sudo tee /tmp/out")
|
|
assert askpass.SUDO_ASKPASS_TIMEOUT_TOKEN not in payload["command"]
|
|
assert PROMPTED not in payload["command"]
|
|
assert payload["stdinData"] == PROMPTED + "\n" + "caller-data\n"
|
|
|
|
|
|
def test_configured_password_goes_on_the_stdin_field_one_line_per_sudo(monkeypatch):
|
|
monkeypatch.setenv("SUDO_PASSWORD", CONFIGURED)
|
|
|
|
(payload,) = _exec_payloads(monkeypatch, "sudo true && sudo id", None)
|
|
|
|
assert payload["command"] == _askpass_command("sudo true && sudo id")
|
|
assert askpass.SUDO_ASKPASS_TIMEOUT_TOKEN not in payload["command"]
|
|
assert CONFIGURED not in payload["command"]
|
|
assert payload["stdinData"] == (CONFIGURED + "\n") * 2
|
|
|
|
|
|
def test_command_without_sudo_keeps_its_command_and_stdin(monkeypatch):
|
|
monkeypatch.setenv("SUDO_PASSWORD", CONFIGURED)
|
|
|
|
(payload,) = _exec_payloads(monkeypatch, "cat > /tmp/out", "caller-data\n")
|
|
|
|
assert payload["command"] == "cat > /tmp/out"
|
|
assert payload.get("stdinData") == "caller-data\n"
|
|
|
|
|
|
def test_sudo_with_own_stdin_option_keeps_the_stdin_form(monkeypatch):
|
|
monkeypatch.setenv("SUDO_PASSWORD", CONFIGURED)
|
|
|
|
(payload,) = _exec_payloads(monkeypatch, "sudo -S id", None)
|
|
|
|
assert payload["command"] == "sudo" + SUDO_STDIN_FLAGS + " -S id"
|
|
assert payload["stdinData"] == CONFIGURED + "\n"
|