Repository navigation
fix(terminal): force=True no longer skips the hardline floor #159276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
73a5081
50558c3
8272878
a124d8e
34e5649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """``terminal_tool(force=True)`` skips the approval layers, never the floor (#157918). | ||
|
|
||
| ``force`` says the caller already confirmed the command. That cannot outrank what YOLO and | ||
| ``approvals.mode: off`` cannot: the hardline floor, the ``sudo -S`` guard, the operator's | ||
| ``approvals.deny`` rules, and the refusal of a command the hardline scan could not read to the | ||
| end. Command strings are only passed to the guards, never run. | ||
| """ | ||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from tools import approval_context | ||
| from tools import terminal_tool | ||
|
|
||
| GREP = 'v=$(grep -o "[^\\"]*" f)' | ||
| LOCAL = {"env_type": "local"} | ||
| ISOLATED_DOCKER = {"env_type": "docker"} | ||
| BIND_MOUNTED_DOCKER = {"env_type": "docker", "docker_volumes": ["/home/u/project:/workspace"]} | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _no_approval_surface(monkeypatch): | ||
| for var in ("HERMES_YOLO_MODE", "HERMES_GATEWAY_SESSION", "HERMES_INTERACTIVE", | ||
| "HERMES_EXEC_ASK", "HERMES_CRON_SESSION", "SUDO_PASSWORD"): | ||
| monkeypatch.delenv(var, raising=False) | ||
|
|
||
|
|
||
| def _forced_rejection(command: str, config: dict) -> dict: | ||
| with pytest.raises(terminal_tool._Rejected) as exc: | ||
| terminal_tool._run_approval_guards(command, config["env_type"], config, force=True) | ||
| return json.loads(exc.value.result_json) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command,config,reason", [ | ||
| pytest.param("rm -rf /", LOCAL, "hardline", id="hardline-local"), | ||
| pytest.param("mkfs.ext4 /dev/sda1", BIND_MOUNTED_DOCKER, "hardline", id="hardline-docker-with-host-mounts"), | ||
| pytest.param("echo hunter2 | sudo -S apt install -y x", LOCAL, "sudo", id="sudo-stdin"), | ||
| pytest.param("\n".join([GREP] * 500), LOCAL, "too long or complex", id="unread-by-the-scan"), | ||
| ]) | ||
| def test_force_does_not_run_what_the_floor_refuses(command, config, reason): | ||
| result = _forced_rejection(command, config) | ||
| assert result["status"] == "blocked" | ||
| assert reason in result["error"] | ||
|
|
||
|
|
||
| def test_force_keeps_the_operators_deny_rules_in_an_isolated_container(monkeypatch): | ||
| monkeypatch.setattr(approval_context, "_get_approval_config", | ||
| lambda: {"mode": "manual", "deny": ["git push --force*"]}) | ||
| result = _forced_rejection("git push --force origin main", ISOLATED_DOCKER) | ||
| assert result["status"] == "blocked" and "approvals.deny" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command,config", [ | ||
| pytest.param("ls -la", LOCAL, id="confirmed-command-local"), | ||
| pytest.param("rm -rf /", ISOLATED_DOCKER, id="isolated-container-is-its-own-floor"), | ||
| ]) | ||
| def test_force_runs_a_confirmed_command_the_floor_allows(command, config): | ||
| verdict = terminal_tool._run_approval_guards(command, config["env_type"], config, force=True) | ||
| assert verdict.approved_run is True | ||
|
|
||
|
|
||
| def test_force_skips_the_approval_layers(monkeypatch): | ||
| """Only the floor binds a forced command, not an approval layer that refuses it unforced (cron's default deny).""" | ||
| monkeypatch.setenv("HERMES_CRON_SESSION", "1") | ||
| with pytest.raises(terminal_tool._Rejected): | ||
| terminal_tool._run_approval_guards("rm -rf build/", "local", LOCAL, force=False) | ||
| assert terminal_tool._run_approval_guards("rm -rf build/", "local", LOCAL, force=True).approved_run is True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,6 +139,7 @@ def _container_profile() -> str: | |
|
|
||
| from tools.approval import ( | ||
| check_all_command_guards as _check_all_guards_impl, | ||
| check_preconfirmed_command_guards as _check_preconfirmed_guards, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new That test installs a
The fix goes in the stub, outside this diff; production code stays as it is: diff --git a/tests/tools/test_managed_browserbase_and_modal.py b/tests/tools/test_managed_browserbase_and_modal.py
index 6031bdb..5322360 100644
--- a/tests/tools/test_managed_browserbase_and_modal.py
+++ b/tests/tools/test_managed_browserbase_and_modal.py
@@ -170,6 +170,7 @@ def _install_fake_tools_package():
detect_dangerous_command=lambda *args, **kwargs: None,
check_dangerous_command=lambda *args, **kwargs: {"approved": True},
check_all_command_guards=lambda *args, **kwargs: {"approved": True},
+ check_preconfirmed_command_guards=lambda *args, **kwargs: {"approved": True},
load_permanent_allowlist=lambda *args, **kwargs: [],
DANGEROUS_PATTERNS=[],
)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 34e5649: the |
||
| ) | ||
|
|
||
|
|
||
|
|
@@ -1179,7 +1180,7 @@ class _ApprovalVerdict: | |
| """Outcome of the pre-exec guard pass. | ||
| ``note`` is the audit note attached to the result. ``approved_run`` is True | ||
| when the user explicitly approved (or pre-confirmed via ``force``); it drives | ||
| when a human approved it or its caller vetted it via ``force``; it drives | ||
| the clean-interrupt-slate clear before ``env.execute`` so an approved command | ||
| can't be SIGINT-killed by a bit that landed during the approval-wait. | ||
| """ | ||
|
|
@@ -1188,12 +1189,17 @@ class _ApprovalVerdict: | |
|
|
||
|
|
||
| def _run_approval_guards(command: str, env_type: str, config: dict[str, Any], *, force: bool) -> _ApprovalVerdict: | ||
| """Run the command guards; ``force`` skips them entirely. | ||
| """Run the command guards; ``force`` skips the approval layers but not the floor | ||
| (``check_preconfirmed_command_guards``). | ||
| Raises :class:`_Rejected` when the command may not run (denied, or pending | ||
| gateway approval).""" | ||
| has_host_access = _docker_has_host_access(config) | ||
| if force: | ||
| return _ApprovalVerdict(approved_run=True) | ||
| approval = _check_all_guards(command, env_type, has_host_access=_docker_has_host_access(config)) | ||
| approval = _check_preconfirmed_guards(command, env_type, has_host_access=has_host_access) | ||
| if approval["approved"]: | ||
| return _ApprovalVerdict(approved_run=True) | ||
| else: | ||
| approval = _check_all_guards(command, env_type, has_host_access=has_host_access) | ||
| if not approval["approved"]: | ||
| if approval.get("status") == "pending_approval": # gateway ask mode | ||
| raise _Rejected(_error_json( | ||
|
|
@@ -1584,8 +1590,9 @@ def terminal_tool( | |
| ) -> str: | ||
| """Execute *command* in the configured terminal environment; returns a JSON string. | ||
| ``force`` (internal, not in the model schema) skips the dangerous-command | ||
| check after the user confirmed. ``workdir`` is per-command and never | ||
| ``force`` (internal, not in the model schema) marks a command its caller already | ||
| vetted: it skips the approval layers, never the floor. | ||
| ``workdir`` is per-command and never | ||
| recorded as the session cwd. ``pty`` applies to the local backend only. | ||
| ``notify_on_complete`` and ``watch_patterns`` are mutually exclusive | ||
| background-only flags: on conflict watch_patterns is dropped. watch_patterns | ||
|
|
@@ -1666,7 +1673,7 @@ def terminal_tool( | |
| status="error", | ||
| )) | ||
| # Pre-exec security checks (floors + dangerous command detection); | ||
| # force=True means the user already confirmed. | ||
| # force: the caller already vetted the command; the floor still runs. | ||
| verdict = _run_approval_guards(command, env_type, plan.config, force=force) | ||
|
|
||
| pty_disabled = pty and _command_requires_pipe_stdin(command) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: nothing pins the other half of the contract, that
forcestill skips the approval layers.Both allowed rows (
ls -lalocally,rm -rf /in an isolated container) are also approved bycheck_all_command_guardswhen no approval surface is present. So a change that sends the force branch back through_check_all_guardspasses all 7 tests here and the 60 in the four neighbouring files I ran (test_approved_command_clean_slate,test_command_guards,test_approval_deny_rules,tui_gateway/test_backend_dir_entries_preconfirmed, which fakesterminal_toolentirely). That's the regression the completion path'sforce=Trueexists to avoid (#115478).The test below uses cron's default
cron_mode: denyas the approval layer. It asserts that the unforced call is refused, so the row isn't vacuous, and that the forced one runs. It passes on this head, and it is the only test that catches that mutant (1 failed of 8).scripts/check --only ruff,healthis clean with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added as
test_force_skips_the_approval_layersin 34e5649, using your cron-default-deny version. Thanks for running the mutation probe.