mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-25 00:51:20 +00:00
fix(security): block gateway and tool env vars in subprocesses
Extend subprocess env sanitization beyond provider credentials by blocking Hermes-managed tool, messaging, and related gateway runtime vars. Reuse a shared sanitizer in LocalEnvironment and ProcessRegistry so background and PTY processes honor the same blocklist and _HERMES_FORCE_ escape hatch. Add regression coverage for local env execution and process_registry spawning.
This commit is contained in:
parent
779f8df6a6
commit
b177b4abad
4 changed files with 215 additions and 27 deletions
|
|
@ -1,11 +1,13 @@
|
|||
"""Tests for tools/process_registry.py — ProcessRegistry query methods, pruning, checkpoint."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from tools.environments.local import _HERMES_PROVIDER_ENV_FORCE_PREFIX
|
||||
from tools.process_registry import (
|
||||
ProcessRegistry,
|
||||
ProcessSession,
|
||||
|
|
@ -213,6 +215,54 @@ class TestPruning:
|
|||
assert total <= MAX_PROCESSES
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Spawn env sanitization
|
||||
# =========================================================================
|
||||
|
||||
class TestSpawnEnvSanitization:
|
||||
def test_spawn_local_strips_blocked_vars_from_background_env(self, registry):
|
||||
captured = {}
|
||||
|
||||
def fake_popen(cmd, **kwargs):
|
||||
captured["env"] = kwargs["env"]
|
||||
proc = MagicMock()
|
||||
proc.pid = 4321
|
||||
proc.stdout = iter([])
|
||||
proc.stdin = MagicMock()
|
||||
proc.poll.return_value = None
|
||||
return proc
|
||||
|
||||
fake_thread = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {
|
||||
"PATH": "/usr/bin:/bin",
|
||||
"HOME": "/home/user",
|
||||
"USER": "tester",
|
||||
"TELEGRAM_BOT_TOKEN": "bot-secret",
|
||||
"FIRECRAWL_API_KEY": "fc-secret",
|
||||
}, clear=True), \
|
||||
patch("tools.process_registry._find_shell", return_value="/bin/bash"), \
|
||||
patch("subprocess.Popen", side_effect=fake_popen), \
|
||||
patch("threading.Thread", return_value=fake_thread), \
|
||||
patch.object(registry, "_write_checkpoint"):
|
||||
registry.spawn_local(
|
||||
"echo hello",
|
||||
cwd="/tmp",
|
||||
env_vars={
|
||||
"MY_CUSTOM_VAR": "keep-me",
|
||||
"TELEGRAM_BOT_TOKEN": "drop-me",
|
||||
f"{_HERMES_PROVIDER_ENV_FORCE_PREFIX}TELEGRAM_BOT_TOKEN": "forced-bot-token",
|
||||
},
|
||||
)
|
||||
|
||||
env = captured["env"]
|
||||
assert env["MY_CUSTOM_VAR"] == "keep-me"
|
||||
assert env["TELEGRAM_BOT_TOKEN"] == "forced-bot-token"
|
||||
assert "FIRECRAWL_API_KEY" not in env
|
||||
assert f"{_HERMES_PROVIDER_ENV_FORCE_PREFIX}TELEGRAM_BOT_TOKEN" not in env
|
||||
assert env["PYTHONUNBUFFERED"] == "1"
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Checkpoint
|
||||
# =========================================================================
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue