T09 · Insecure Skill Coding Practices
- Location
scripts/run_step.sh:8- Finding
Path Traversal and Unsafe Shell Argument Expansion in Step Runner
- Content
View full analysis
<脚本文件>" echo "示例: bash run_step.sh qc 01_qc.sh" exit 1 fi if [ ! -f "$SCRIPT" ]; then echo "错误: 脚本 $SCRIPT 不存在" exit 1 fi echo "[$(date '+%Y-%m-%d %H:%M:%S')] 开始执行: $SCRIPT" | tee $LOG_FILE echo "[$(date '+%Y-%m-%d %H:%M:%S')] 日志: $LOG_FILE" | tee -a $LOG_FILE set -e bash $SCRIPT 2>&1 | tee -a $LOG_FILE ``` ### Technical Analysis The script accepts `STEP_NAME` from its first positional argument and directly incorporates it into `LOG_FILE`: ```bash LOG_FILE="${LOG_DIR}/${STEP_NAME}.log" ``` No validation prevents directory separators, `..` traversal components, or other unsafe path content. Consequently, a value such as `../../target` produces `logs/../../target.log`. The first `tee` invocation opens the resulting path without append mode and therefore truncates an existing writable file before writing the initial status message. The script also uses `$SCRIPT` and `$LOG_FILE` without quotation when invoking `bash` and `tee`. Shell expansion therefore applies word splitting and pathname expansion. This can make the command operate on multiple arguments or paths rather than the single file that was validated. It does not, by itself, reinterpret shell metacharacters embedded in the variable as new shell syntax, but it can still cause unintended script selection, additional arguments, or multiple log destinations. The vulnerability is reachable by anyone able to invoke this wrapper with attacker-controlled arguments. Exploitation occurs with the privileges of the account running the Skill. ### Attack Path 1. The attacker identifies a wri ...[truncated 1767 chars]- Remediation
View remediation
&2 exit 1 fi ``` 2. Quote every variable used as a shell argument and use `--` to terminate utility options: ```bash mkdir -p -- "$LOG_DIR" bash -- "$SCRIPT" 2>&1 | tee -a -- "$LOG_FILE" ``` 3. Canonicalize the log directory and verify that the final destination remains inside it before opening the file. 4. Reject pre-existing symbolic links as log destinations where practical. For stronger protection, create log files using a mechanism that performs atomic creation and disallows symlink following. 5. Use strict shell behavior and preserve pipeline error handling explicitly: ```bash set -euo pipefail ``` 6. A hardened implementation could begin as follows: ```bash #!/usr/bin/env bash set -euo pipefail STEP_NAME=${1:-step} SCRIPT=${2:-} LOG_DIR="logs" if [[ ! "$STEP_NAME" =~ ^[A-Za-z0-9._-]+$ ]]; then printf 'Invalid step name\n' >&2 exit 1 fi if [[ -z "$SCRIPT" || ! -f "$SCRIPT" ]]; then printf 'Invalid script file: %s\n' "$SCRIPT" >&2 exit 1 fi mkdir -p -- "$LOG_DIR" LOG_FILE="${LOG_DIR}/${STEP_NAME}.log" printf '[%s] Starting: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$SCRIPT" | tee -- "$LOG_FILE" bash -- "$SCRIPT" 2>&1 | tee -a -- "$LOG_FILE" ``` 7. Add regression tests covering traversal strings, absolute-looking paths, whitespace, wildcard characters, option-like values, and symbolic-link destinations. ]]>
