T09 · Insecure Skill Coding Practices
- Location
scripts/research_quick.sh:5- Finding
Path Traversal Through Unsanitized Topic in Output Directory Construction
- Content
View full analysis
Vulnerability Details
File Location:
scripts/research_quick.sh, lines 5–9
Vulnerability Type: User-controlled path traversal and unsafe temporary directory handling
Risk Level: MediumVulnerable Code
bash set -euo pipefail TOPIC="${1:?Usage: research_quick.sh TOPIC [FRESHNESS]}" FRESHNESS="${2:-month}" OUTDIR="/tmp/research-$(echo "$TOPIC" | tr ' ' '_' | head -c 40)" mkdir -p "$OUTDIR"Technical Analysis
The script incorporates the user-supplied
TOPICdirectly into a filesystem path. Its only normalization replaces spaces with underscores; it does not reject directory separators,..path components, control characters, or symbolic-link interactions.Although the final variable is quoted and therefore is not vulnerable to shell word splitting or direct command injection, quoting does not prevent filesystem path traversal. A topic containing slash-separated parent-directory components can cause the resolved path to leave the intended
/tmp/research-*namespace.The use of a predictable path under the shared
/tmpdirectory also permits collisions with pre-existing paths. An attacker able to create entries in/tmpmay prepare directories or symbolic links that influence wheremkdir -presolves subsequent path components.Attack Path
-
An attacker supplies a crafted topic containing path separators and parent-directory components, for example:
bash ./scripts/research_quick.sh 'foo/../../../var/tmp/attacker-dir' -
The script constructs the following path:
text /tmp/research-foo/../../../var/tmp/attacker-dir -
mkdir -presolves the..components and attempts to create the resulting directory outside the intended research output location. -
Directory creation succeeds wherever the account running the script has write permission. If the script is invoked by a more privileged account, the writable scope increases accordin ...[truncated 1216 chars]
-
- Remediation
View remediation
Remediation Suggestions
Avoid deriving a temporary directory path directly from user input. Create an unpredictable directory under a fixed trusted base using
mktemp:bash set -euo pipefail TOPIC="${1:?Usage: research_quick.sh TOPIC [FRESHNESS]}" FRESHNESS="${2:-month}" OUTDIR="$(mktemp -d -- /tmp/research-XXXXXXXXXX)"If a human-readable topic must appear in the directory name, apply strict allowlist validation and retain an unpredictable suffix:
bash SAFE_TOPIC="$(printf '%s' "$TOPIC" | LC_ALL=C tr -cd 'A-Za-z0-9._-' | cut -c1-40)" [[ -n "$SAFE_TOPIC" ]] || SAFE_TOPIC="topic" OUTDIR="$(mktemp -d -- "/tmp/research-${SAFE_TOPIC}-XXXXXXXXXX")"Additional hardening should include:
- Explicitly reject
/, backslashes,..path components, newlines, and other control characters when accepting a topic for use in a filename. - Use a dedicated base directory with restrictive permissions rather than a predictable path directly under shared
/tmp. - Set a restrictive
umask, such asumask 077, before creating temporary resources. - If constructing paths remains necessary, resolve and verify the canonical destination before use, ensuring it remains beneath the approved base directory.
- Do not run the script with elevated privileges when its arguments can be influenced by an untrusted user.
- Explicitly reject
