Summary
hermes-agent contains an OS command injection vulnerability in the terminal_tool component, specifically within the detect_dangerous_command function in tools/approval.py.
When untrusted input is incorporated into shell commands without proper sanitization, a remote attacker can craft input that executes arbitrary operating system commands on the host where the agent runs.
Affected context
The vulnerability arises in environments where hermes-agent is deployed with the terminal_tool enabled and allowed to execute shell commands on behalf of users or upstream systems.
It becomes risky when:
- External or user-controlled text is passed into terminal commands,
- The agent runs with network exposure (e.g., serving requests over HTTP, websockets, or through a broker),
- And command strings are constructed or filtered by
detect_dangerous_commandbefore execution.
Under these conditions, crafted input can bypass or subvert the detection logic and reach an underlying shell with attacker-controlled content.
Exploit preconditions
- Attack surface: Remote, via any interface that routes user-supplied text into the
terminal_tool(API endpoints, chat interfaces, workflows, or orchestration layers). - Required access: Ability to submit or influence command strings or parameters processed by
detect_dangerous_commandintools/approval.py. - Trigger point: Execution of a shell command formed using the attacker-controlled input, often through wrappers that rely on the detection logic for safety.
- Why exploitation is possible: The function responsible for detecting or filtering dangerous commands does not fully neutralize shell metacharacters or prevent command concatenation, allowing attacker-controlled segments to be interpreted by the shell as additional commands.
Why this happens
This issue stems from constructing and executing shell commands using untrusted input without robust neutralization or isolation, while relying on incomplete "dangerous command" detection heuristics.
Instead of using parameterized or non-shell-based execution APIs, the implementation allows user-controlled strings to influence the command line that a shell interprets, making it vulnerable to injected operators (such as chaining, redirection, or subshell syntax).
Conceptually, this maps to:
- CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), where special characters and structures are not fully handled.
- And is related to CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection'), reflecting the broader pattern of embedding untrusted data into executable commands.
The core design flaw is treating user input as text to be "checked" rather than strictly treated as data separated from command execution semantics.
Potential impact
- Remote execution of arbitrary operating system commands with the privileges of the hermes-agent process.
- Reading, modifying, or deleting files accessible to the agent's runtime user.
- Pivoting to other internal services or networks reachable from the compromised host.
- Installing backdoors, miners, or other persistent malware on the host.
- Exfiltration of configuration secrets, API keys, or environment variables used by the agent.
- Disruption of normal agent operation, including denial of service through resource exhaustion.
General mitigation guidance
Effective mitigation focuses on removing shell interpretation from the path of untrusted input and hardening command execution patterns:
- Prefer high-level, parameterized APIs (e.g., direct library calls, subprocess invocation with argument lists and
shell=False) instead of passing strings to a shell. - Treat all user-supplied or remote data as untrusted; avoid building command strings through concatenation or formatting.
- Where shell usage cannot be eliminated, strictly whitelist allowed commands and arguments, rejecting any input that does not match a narrow, validated pattern.
- Implement robust input validation that focuses on what is allowed rather than trying to enumerate "dangerous" tokens, and perform validation before any command construction.
- Reduce process privileges for the agent so that, if compromise occurs, the scope of damage is limited (least-privilege user accounts, restricted file and network access).
- Isolate the command execution environment via containers, sandboxes, or separate worker processes with tightly controlled interfaces.
How teams detect issues like this
Detection typically combines code-level analysis with runtime monitoring:
- Static analysis: Configure rules to flag use of shell execution APIs (
os.system,subprocesswithshell=True, or similar) where arguments include variables or data derived from external input. - Code review signals: Look for functions that:
- Attempt to "detect dangerous commands" via string matching,
- Concatenate user input into commands,
- Or rely on blacklists of tokens instead of strict whitelists or argument lists.
- Policy enforcement: Define secure coding policies prohibiting shell execution with unsanitized input, enforced in CI via linters or security scanners.
- Runtime monitoring: Use logging and security monitoring to detect anomalous command patterns, unexpected utilities being invoked, or spikes in process creation originating from the agent.
- Automated SAST/IAST tools can assist in identifying injection sinks and untrusted data flows into command execution points.

