T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/fetch_file.sh:91
- Finding
- SFTP Batch Command Injection Through Unvalidated Paths<![CDATA[ ## Vulnerability Details **File Location**: `scripts/fetch_file.sh:91-103` **Vulnerability Type**: SFTP batch command injection **Risk Level**: High ### Vulnerable Code ```bash REMOTE_PATH="${SFTP_PATH#*:}" echo " User/Host: $USER_HOST" echo " Remote path: $REMOTE_PATH" # Create temporary batch file BATCH_FILE=$(mktemp) cat > "$BATCH_FILE" << EOF get $REMOTE_PATH $OUTPUT EOF # Execute SFTP sftp -b "$BATCH_FILE" "$USER_HOST" ``` ### Technical Analysis The script derives `REMOTE_PATH` from a caller-controlled SFTP URL and writes it directly into an SFTP batch file. The caller-controlled `OUTPUT` value is inserted into the same command without validation or escaping. Shell quoting around the here-document does not make its contents safe for the SFTP batch-command parser. In particular, newline characters in either value can terminate the intended `get` command and introduce another SFTP batch instruction. OpenSSH SFTP supports commands interpreted by its own command language, including local shell execution through the `!` command. Consequently, an attacker able to influence the script arguments can turn a file-retrieval operation into arbitrary command execution under the account running the Skill. ### Attack Path 1. The attacker supplies a crafted `sftp://` or `ssh://` source to `fetch_file.sh`. 2. The source contains a newline in its remote-path component followed by an additional SFTP batch instruction. 3. The script extracts the crafted value into `REMOTE_PATH`. 4. The here-document writes both the intended `get` instruction and the injected instruction into the temporary batch file. 5. `sftp -b` parses the injected line as a separate command. 6. If a local-shell instruction is injected, the command runs with the permissions and environment of the Skill process. The output filename is another injection surface if an untrusted caller can control the second script argument. ### Impact Assessment Successful exploitation can provide ...[truncated 602 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Reject carriage returns, line feeds, NUL bytes, and other SFTP command-language control characters in `REMOTE_PATH`, `OUTPUT`, and `USER_HOST`. - Apply a strict allowlist for SFTP endpoint syntax and expected remote-path characters. - Do not construct SFTP batch programs from untrusted text where an API or safer transfer mechanism is available. - If batch mode must be retained, use a dedicated escaping routine designed for OpenSSH SFTP syntax; shell escaping alone is insufficient. - Restrict output to a controlled working directory and reject absolute paths and traversal components when arbitrary destinations are unnecessary. - Install an `EXIT` trap immediately after creating the temporary file so it is removed on errors or interruption. - Run SFTP operations in a sandbox with minimal filesystem and network access. - Add regression tests containing newlines, quotes, spaces, command prefixes, and malicious output filenames, verifying that all such inputs are rejected before SFTP starts. ]]>
