T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/healthcheck.sh:9
- Finding
- Arbitrary Shell Command Execution Through Sourced Environment File<![CDATA[ ## Vulnerability Details **File Location**: `scripts/healthcheck.sh`, lines 9-33 **Vulnerability Type**: Unsafe execution of a configuration file **Risk Level**: High ### Vulnerable Code ```bash ENV_FILE="${ENV_FILE:-$HOME/.env}" # --- Load environment variables --- if [[ -f "$ENV_FILE" ]]; then # shellcheck disable=SC1090 source "$ENV_FILE" fi ``` ### Technical Analysis The script loads its environment configuration using the Bash `source` command. `source` does not treat the selected file as a data-only collection of key-value pairs; it executes the entire file as shell code in the current process. The default value is the generic path `$HOME/.env`, which may be shared by unrelated applications. The caller can also select another path through `ENV_FILE`. Consequently, any shell construct in the selected file—including command substitutions, function calls, redirections, or arbitrary commands—will execute with the privileges of the user running the monitor. This exceeds the access needed to read the two documented Telegram settings. The monitor only needs `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID`, but instead grants the configuration file unrestricted code-execution capability. The documented LaunchAgent or cron configuration increases the risk because the file may be executed repeatedly and without interactive confirmation. ### Attack Path 1. An attacker obtains write access to `$HOME/.env`, or to another file selected through `ENV_FILE`. 2. The attacker inserts a shell command into that file, for example: ```bash TELEGRAM_CHAT_ID=1234 malicious_command ``` 3. The user manually runs the health monitor, or its configured LaunchAgent/cron schedule invokes it. 4. `source "$ENV_FILE"` executes `malicious_command` as part of the monitoring process. 5. The command inherits the monitor's user identity, environment, filesystem access, and available credentials. 6. If scheduling is enabled, the injected command can run repeatedly ...[truncated 918 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Do not execute the configuration file with `source`. 2. Use a dedicated configuration path such as `$HOME/.config/mcp-health-monitor/env` rather than the generic `$HOME/.env`. 3. Parse only an explicit allowlist of supported keys, specifically `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID`. 4. Reject shell syntax, command substitutions, unknown variables, malformed lines, and duplicate keys. 5. Verify that the configuration is a regular file owned by the current user and is not a symbolic link. 6. Require restrictive permissions, preferably mode `0600`, and ensure its parent directory is not writable by untrusted users. 7. When scheduling the script, provide a minimal environment and run it as an unprivileged dedicated account where practical. 8. Document that the configuration file contains secrets and must not be shared with unrelated applications. A safer implementation should use a data-only format and a parser that never evaluates file contents as shell commands. ]]>
