- Location
- skills/hosted-agents/references/infrastructure-patterns.md:43
- Finding
- Shell Command Injection and Token Exposure in Hosted-Agent Infrastructure Pattern<![CDATA[
## Vulnerability Details
**File Location**: `skills/hosted-agents/references/infrastructure-patterns.md:43-52`
**Vulnerability Type**: Shell command injection and credential exposure through command arguments
**Risk Level**: High
### Vulnerable Code
```python
def _clone_and_setup(self):
"""Clone repo and run initial setup."""
token = self._get_github_app_token()
os.system(f"git clone https://x-access-token:{token}@github.com/{self.repo_url}")
os.system("npm install")
os.system("npm run build")
@modal.method()
def execute_prompt(self, prompt: str, user_identity: dict) -> dict:
"""Execute a prompt in the sandbox."""
# Update git config for this user
os.system(f'git config user.name "{user_identity["name"]}"')
os.system(f'git config user.email "{user_identity["email"]}"')
```
### Technical Analysis
The reference implementation inserts a repository URL, GitHub token, user name, and email address directly into shell command strings passed to `os.system()`. `os.system()` invokes a command shell, so shell metacharacters and substitutions in interpolated values are interpreted as executable syntax.
Quoting the identity values with double quotes is insufficient. Values containing a double quote, command substitution, backticks, newlines, or other shell syntax can escape the intended argument and execute arbitrary commands.
The GitHub token is also embedded directly in the clone URL. This can expose it through process listings, command logging, exception output, shell history, telemetry, or build records.
The affected content is a reference implementation rather than an active call path in the current package. Exploitation therefore requires the pattern to be copied, adapted, or deployed as shown. Because the document presents the code as an infrastructure pattern, it can propagate the flaw into production systems.
### Attack Path
1. A hosted-agent deployment adopts the documented implementation.
2. An untrusted user
...[truncated 1366 chars]
- Remediation
- <![CDATA[
## Remediation Suggestions
1. Replace shell-string execution with argument-array execution:
```python
subprocess.run(
["git", "clone", validated_repo_url, workspace_path],
check=True,
shell=False,
)
subprocess.run(
["git", "config", "user.name", user_identity["name"]],
check=True,
shell=False,
)
subprocess.run(
["git", "config", "user.email", user_identity["email"]],
check=True,
shell=False,
)
```
2. Validate repository identifiers against a strict owner/repository format or resolve them from server-side records rather than accepting arbitrary URLs.
3. Do not place tokens in clone URLs. Use a short-lived Git credential helper, isolated askpass program, or provider-supported secret injection mechanism.
4. Prevent credentials from appearing in process arguments, logs, exceptions, snapshots, or telemetry.
5. Apply length and character constraints to identity fields even when using argument arrays.
6. Run build workers with minimal privileges, read-only host mounts, restricted egress, scoped credentials, and disposable filesystems.
7. Never promote a sandbox image or snapshot if setup or validation fails.
8. Update the reference documentation so users do not copy an unsafe pattern into production.
9. Add adversarial tests containing quotes, semicolons, newlines, command substitutions, and option-like values.
]]>