T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/qr.sh:69
- Finding
- WiFi SSID Path Traversal Can Write QR Images Outside the Intended Directory<![CDATA[ ## Vulnerability Details **File Location**: `scripts/qr.sh`, lines 69-72 **Vulnerability Type**: Path traversal through an unsanitized filename component **Risk Level**: Medium ### Vulnerable Code ```bash local content="WIFI:T:$encryption;S:$ssid;P:$password;;" local output="$QR_DIR/wifi_${ssid}_$(get_timestamp).png" generate_qr "$content" "$output" ``` The resulting path is passed to the file-writing command in `generate_qr`: ```bash qrencode -o "$output" -s 10 -l M "$content" ``` ### Technical Analysis The user-controlled `ssid` value is inserted directly into the output path without removing directory separators, normalizing the path, or verifying that the final destination remains inside `$QR_DIR`. Shell quoting prevents command injection, but it does not prevent filesystem traversal. An SSID containing slash-separated components and `..` sequences can cause path resolution outside the intended QR directory. Because the fixed `wifi_` prefix precedes the SSID, exploitation generally requires the first attacker-selected path component, such as `$QR_DIR/wifi_<component>`, to already exist. This limits but does not eliminate the vulnerability. The destination also has a timestamp and `.png` suffix, restricting which filenames can be targeted. Nevertheless, the script does not enforce its intended output-directory boundary. ### Attack Path 1. An attacker identifies or creates a suitable directory under `$QR_DIR` whose name begins with `wifi_`. 2. The attacker supplies an SSID containing a normal first component followed by `/../` traversal sequences and an external destination path. 3. The script concatenates the SSID into: `"$QR_DIR/wifi_${ssid}_<timestamp>.png"`. 4. Filesystem path resolution processes the embedded traversal components. 5. `qrencode -o` creates or overwrites the resolved PNG destination outside `$QR_DIR`, provided the necessary parent directories exist and the process has write permission. ### Impact Assessment The att ...[truncated 441 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Do not use the SSID as part of the filesystem path. Generate an opaque filename using a timestamp plus cryptographically random data. - If a descriptive filename is required, replace every character outside a strict allowlist such as `[A-Za-z0-9._-]` with `_`. - Explicitly reject `/`, `\`, `..`, control characters, and empty sanitized names. - Resolve the canonical output directory and candidate path, then verify that the candidate remains beneath the canonical `$QR_DIR`. - Use restrictive file permissions and refuse to overwrite existing files where possible. Example approach: ```bash local safe_id safe_id="$(printf '%s' "$ssid" | tr -c 'A-Za-z0-9._-' '_')" local output="$QR_DIR/wifi_${safe_id}_$(get_timestamp).png" case "$(realpath -m "$output")" in "$(realpath "$QR_DIR")"/*) ;; *) echo "Invalid output path" >&2 return 1 ;; esac ``` Using a fully opaque filename independent of the SSID is preferable. ]]>
