T09 · Insecure Skill Coding Practices
- Location
scripts/setup_bots.py:125- Finding
Arbitrary Shell Command Injection Through Unvalidated agentId
- Content
View full analysis
str: """运行命令并返回输出""" print(f"Running: {cmd}") result = subprocess.run(cmd, shell=True, capture_output=True, text=True) if check and result.returncode != 0: print(f"Error: {result.stderr}") print(f"Exit code: {result.returncode}") return "" return result.stdout ``` ```python agent_id = bot['agentId'] ``` ```python if not exists: # 创建 agent run_command(f"openclaw agents add {agent_id} --workspace {workspace_path} --non-interactive") ``` ### Technical Analysis The `agentId` field is read directly from the operator-supplied JSON configuration and interpolated into a command string. The resulting string is passed to `subprocess.run()` with `shell=True`. Because no allowlist validation or shell-safe argument separation is applied, shell metacharacters in `agentId` are interpreted by the command shell rather than treated as literal identifier characters. The derived `workspace_path` also embeds the same untrusted value in the command. This permits command substitution, command chaining, redirection, pipelines, or similar shell syntax. The vulnerability does not require a defect in the `openclaw` executable; exploitation occurs in the shell before or while that executable is invoked. ### Attack Path 1. An attacker obtains the ability to create or modify the JSON configuration passed to `setup_bots.py`. 2. The attacker places shell syntax in a bot's `agentId`, such as an identifier followed by a command separator and an attacker-selected command. 3. The operator executes: ```bash python3 scripts/setup_bots.py /path/to/bots_config.json ``` 4. The script reads the malicious `agentId` without valid ...[truncated 1217 chars]- Remediation
View remediation
str: print(f"Running: {cmd!r}") result = subprocess.run( cmd, shell=False, capture_output=True, text=True, check=False, ) if check and result.returncode != 0: print(f"Error: {result.stderr}") print(f"Exit code: {result.returncode}") return "" return result.stdout run_command([ "openclaw", "agents", "add", agent_id, "--workspace", workspace_path, "--non-interactive", ]) ``` Apply defense in depth: 1. Validate `agentId` before using it in a path or command. Use a conservative allowlist such as `^[A-Za-z0-9_-]+$` and impose a reasonable maximum length. 2. Reject identifiers containing path separators, traversal components, whitespace, control characters, or shell metacharacters. 3. Construct workspace paths with `pathlib.Path`, resolve them, and verify that the resolved path remains beneath the intended `~/.openclaw/agents` directory. 4. Validate the complete configuration against a schema before making any filesystem or configuration changes. 5. Avoid logging secrets or complete command material if future arguments may contain sensitive values. 6. Fail safely when agent creation fails instead of updating the central configuration as though creation succeeded. ]]>
