fix(approval): honor pattern-key allowlists when unattended

This commit is contained in:
Hukla
2026-09-14 19:22:43 +03:00
committed by Teknium
parent c1b7f28693
commit 05e7e89175
2 changed files with 74 additions and 4 deletions
+64 -1
View File
@@ -264,6 +264,70 @@ class TestCronDenyModeAllGuards:
result = check_all_command_guards("echo hello", "local")
assert result["approved"]
def test_permanent_pattern_key_allows_matching_command_in_cron_deny(self, monkeypatch):
"""A canonical dangerous-pattern key in command_allowlist applies in unattended mode."""
monkeypatch.setenv("HERMES_CRON_SESSION", "1")
monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)
monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False)
monkeypatch.delenv("HERMES_EXEC_ASK", raising=False)
monkeypatch.delenv("HERMES_YOLO_MODE", raising=False)
approval_module.load_permanent({"script execution via heredoc"})
from unittest.mock import patch as mock_patch
with (
mock_patch("tools.approval_context._get_cron_approval_mode", return_value="deny"),
mock_patch("tools.tirith_security.check_command_security",
return_value={"action": "allow", "findings": [], "summary": ""}),
):
result = check_all_command_guards("python3 - <<'PY'\nprint('ok')\nPY", "local")
assert result["approved"] is True
def test_session_pattern_key_does_not_allow_command_in_cron_deny(self, monkeypatch):
"""A session-only approval must not become standing authority for unattended work."""
monkeypatch.delenv("HERMES_CRON_SESSION", raising=False)
monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)
monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False)
monkeypatch.delenv("HERMES_EXEC_ASK", raising=False)
monkeypatch.delenv("HERMES_YOLO_MODE", raising=False)
approval_module.approve_session("test-session", "script execution via heredoc")
from unittest.mock import patch as mock_patch
tokens = set_session_vars(session_key="test-session", cron_session="1")
try:
with mock_patch("tools.approval_context._get_cron_approval_mode", return_value="deny"):
result = check_all_command_guards("python3 - <<'PY'\nprint('ok')\nPY", "local")
finally:
clear_session_vars(tokens)
assert result["approved"] is False
assert "BLOCKED" in result["message"]
def test_pattern_key_allowlist_does_not_bypass_tirith_in_cron_deny(self, monkeypatch):
"""Approving one dangerous pattern must not suppress an independent Tirith finding."""
monkeypatch.setenv("HERMES_CRON_SESSION", "1")
monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)
monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False)
monkeypatch.delenv("HERMES_EXEC_ASK", raising=False)
monkeypatch.delenv("HERMES_YOLO_MODE", raising=False)
approval_module.load_permanent({"script execution via heredoc"})
from unittest.mock import patch as mock_patch
threat = {
"action": "block",
"findings": [{"severity": "HIGH", "title": "Independent threat",
"description": "content remains unsafe"}],
"summary": "independent threat",
}
with (
mock_patch("tools.approval_context._get_cron_approval_mode", return_value="deny"),
mock_patch("tools.tirith_security.check_command_security", return_value=threat),
):
result = check_all_command_guards("python3 - <<'PY'\nprint('ok')\nPY", "local")
assert result["approved"] is False
assert "Independent threat" in result["message"]
def test_combined_guard_approve_mode(self, monkeypatch):
monkeypatch.setenv("HERMES_CRON_SESSION", "1")
monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)
@@ -476,4 +540,3 @@ class TestCronWithGatewayOrigin:
assert result.get("status") != "approval_required"
finally:
clear_session_vars(tokens)
+10 -3
View File
@@ -332,6 +332,13 @@ def is_approved(session_key: str, pattern_key: str) -> bool:
return any(alias in approved for alias in aliases)
def _is_permanently_approved(pattern_key: str) -> bool:
"""Permanent approval only, with compatibility for migrated pattern keys."""
aliases = _approval_key_aliases(pattern_key)
with _lock:
return any(alias in _permanent_set() for alias in aliases)
def approve_permanent(pattern_key: str):
"""Add a pattern to the permanent allowlist."""
with _lock:
@@ -618,11 +625,11 @@ def _unattended_deny(command: str, ctx: _Unattended) -> dict | None:
subject, noun="dangerous commands",
advice="Find an alternative approach that avoids this command.")}
is_dangerous, _pk, description = detect_dangerous_command(command)
if is_dangerous:
is_dangerous, pattern_key, description = detect_dangerous_command(command)
if is_dangerous and not _is_permanently_approved(pattern_key):
result = block(f"Command flagged as dangerous ({description})")
if ctx.name == "single_query":
result.update(pattern_key=_pk, description=description)
result.update(pattern_key=pattern_key, description=description)
return result
try:
from tools.tirith_security import check_command_security