T09 · Insecure Skill Coding Practices
- Location
scripts/create-package.sh:6- Finding
Path Traversal Allows File Creation and Overwrite Outside the Intended Package Directory
- Content
View full analysis
"src/$PACKAGE_DIR/domain/features.py" << 'EOF' ``` The same attacker-controlled path is subsequently used as the destination for additional redirections that create `io/data.py` and `application/train.py`. ### Technical Analysis The first command-line argument is treated as a package name and incorporated directly into filesystem paths. The only normalization replaces hyphens with underscores: ```bash PACKAGE_DIR="${PACKAGE_NAME//-/_}" ``` This does not reject absolute paths, forward slashes, backtracking components such as `..`, or other values that are invalid as Python package identifiers. Shell quoting prevents ordinary shell metacharacter expansion, but it does not prevent filesystem path traversal. For example, the argument `../../target` produces paths beginning with: ```text src/../../target/ ``` After path resolution, these paths point outside the intended `src/` directory. The script uses `mkdir -p`, `touch`, and truncating output redirections (`cat >`) on the resulting locations. Existing `features.py`, `data.py`, and `train.py` files at a traversed destination may therefore be replaced without confirmation. This is an exploitable coding flaw rather than evidence that the script intentionally performs malicious activity. ### Attack Path 1. An attacker controls or influe ...[truncated 1434 chars]- Remediation
View remediation
&2 exit 1 fi PACKAGE_DIR="${PACKAGE_NAME//-/_}" ``` 2. **Enforce destination containment.** Resolve both the intended base directory and destination to canonical absolute paths, then verify that the destination remains beneath the base directory before writing any files. 3. **Reject existing destinations by default.** Avoid silently truncating source files: ```bash if [[ -e "src/$PACKAGE_DIR" ]]; then echo "Error: destination already exists." >&2 exit 1 fi ``` 4. **Use no-clobber behavior or explicit overwrite consent.** If regeneration is required, add a deliberate `--force` option rather than overwriting files automatically. 5. **Create the scaffold only after all validation succeeds.** Validate the package name, canonical destination, containment, and collision state before the first `mkdir`, `touch`, or redirection. 6. **Add negative tests.** Verify rejection of values such as `../target`, `../../target`, `/tmp/target`, `name/subdir`, `.`, and `..`, as well as protection against overwriting an existing package. ]]>
