Summary
OpenHands contains a command injection issue in the get_git_diff() method, where the path parameter from the /api/conversations/{conversation_id}/git/diff endpoint is passed directly into a shell command.
An authenticated user can craft a malicious path value to execute arbitrary commands inside the agent sandbox, bypassing the normal, mediated command-execution channel.
Affected context
The vulnerability occurs in OpenHands deployments that expose the /api/conversations/{conversation_id}/git/diff API and rely on the get_git_diff() helper in runtime/utils/git_handler.py.
It manifests when user-supplied path values are interpolated into shell commands used to compute git diffs, without adequate sanitization or escaping.
The issue is limited to the agent sandbox environment but effectively bypasses the intended control layer that normally mediates which commands the agent runs.
Exploit preconditions
- Attack surface: HTTP API at
/api/conversations/{conversation_id}/git/diff - Required access: Authenticated ability to invoke conversation APIs and supply a
pathparameter - Trigger point: Calling the git diff endpoint with a crafted
paththat alters the shell command line - Why exploitation is possible: The
pathvalue is concatenated or formatted directly into a shell command without strict validation, escaping, or the use of non-shell execution APIs, allowing additional commands or arguments to be injected.
Why this happens
This weakness maps to CWE-78: Improper Neutralization of Special Elements used in an OS Command.
The core problem is treating user-controlled input as trusted when constructing shell commands, typically via string concatenation, interpolation, or shell=True invocation.
Because the shell parses metacharacters (such as separators, redirections, and substitutions), any untrusted string placed into a command context can change the intended behavior, leading to execution of attacker-controlled commands within the agent sandbox.
Potential impact
- Execution of arbitrary shell commands within the agent sandbox environment
- Bypassing OpenHands' normal agent command mediation and logging pathways
- Modification, exfiltration, or deletion of files accessible in the sandbox
- Use of the sandbox as a pivot point to attempt further attacks against adjacent resources, depending on sandbox isolation strength
- Evasion of higher-level policy or guardrail logic that assumes all commands come through sanctioned channels
General mitigation guidance
Effective mitigation focuses on eliminating direct shell interpretation of user input and tightening trust boundaries around command execution:
- Prefer high-level language APIs (e.g., direct Git library calls or subprocess execution without a shell) instead of constructing shell strings.
- If external commands are necessary, use parameterized process invocation (e.g., argument arrays) and explicitly avoid shell parsing.
- Validate and normalize user-supplied paths against an allowlist of expected patterns or directories, rejecting or sanitizing unexpected input.
- Treat all API parameters, even from authenticated users, as untrusted and subject to strict input handling.
- Enforce sandbox hardening and least-privilege on the process executing git operations, minimizing the blast radius if command execution is abused.
How teams detect issues like this
Detection typically combines code-centric and runtime approaches:
- Static analysis: Configure rules to flag shell-command construction involving user-controlled variables (e.g.,
subprocesswithshell=True,os.system, or similar APIs), especially where HTTP or API parameters flow into those calls. - Code review: Look for patterns where request parameters (e.g.,
path) are interpolated into command strings, or where git operations are invoked via shell instead of safer library calls. - Security testing: Include automated tests and dynamic analysis that fuzz command-related parameters for injection indicators (unexpected output, failures, or extra processes).
- Policy enforcement: Integrate pre-merge checks that reject new uses of unsafe command execution primitives, and require justification or security review for any remaining shell-based code.

