T09 · Insecure Skill Coding Practices
- Location
scripts/audit.sh:16- Finding
Untrusted Markdown filenames are passed to command-line tools as options
- Content
View full analysis
Vulnerability Details
File Location:
scripts/audit.sh, lines 16-69
Vulnerability Type: Command-line option injection and audit-result manipulation
Risk Level: MediumTechnical Analysis
The script expands
*.mddirectly into arguments forlsandgrepwithout terminating option parsing with--or prefixing generated paths with./.Vulnerable code includes:
bash NODES=$(ls *.md 2>/dev/null | grep -v "^MEMORY.md$" | wc -l | tr -d ' ') for link in $(grep -h -oE '\[\[[a-z_-]+\]\]' *.md 2>/dev/null | sed 's/\[\[//;s/\]\]//' | sort -u); do if [ ! -f "${link}.md" ]; then echo " ❌ [[$link]] referenced from: $(grep -l "\[\[$link\]\]" *.md | tr '\n' ' ')" broken=$((broken+1)) fi done for f in *.md; do [ "$f" = "MEMORY.md" ] && continue base="${f%.md}" out=$(grep -oE '\[\[[a-z_-]+\]\]' "$f" 2>/dev/null | sort -u | wc -l | tr -d ' ') in=$(grep -l -E "\[\[$base\]\]" *.md 2>/dev/null | grep -v "^$f$" | wc -l | tr -d ' ') done TOTAL_EDGES=$(grep -h -oE '\[\[[a-z_-]+\]\]' *.md 2>/dev/null | wc -l | tr -d ' ') UNIQ_EDGES=$(grep -h -oE '\[\[[a-z_-]+\]\]' *.md 2>/dev/null | sort -u | wc -l | tr -d ' ')Unix filenames may begin with a hyphen. After shell expansion, a filename such as
--exclude=*.mdcan therefore be interpreted by GNUgrepas an option rather than as an input file. Short-option-like filenames can similarly change behavior or produce errors.Additionally,
baseis inserted directly into an extended regular expression:bash in=$(grep -l -E "\[\[$base\]\]" *.md 2>/dev/null | grep -v "^$f$" | wc -l | tr -d ' ')A filename containing regular-expression metacharacters can alter the intended link-matching expression and generate incorrect in-degree or orphan-page results.
Because the script does not enable
pipefail, failures from commands at the beginning of pipelines can be hidden by a successfulwc,grep, ortrat the end. Re ...[truncated 1715 chars]- Remediation
View remediation
Remediation Suggestions
- Stop using
lsfor file enumeration. - Use
findwith null-delimited output or Bash arrays so filenames remain data rather than options. - Insert
--before every file-list argument accepted bygrep. - Prefix glob expansions with
./where practical. - Treat filenames as fixed strings when matching references, or escape all regular-expression metacharacters before interpolation.
- Enable pipeline failure propagation with
set -euo pipefail. - Do not suppress all diagnostics during audit-critical operations; explicitly handle “no match” status separately from actual execution errors.
A hardened enumeration pattern is:
bash set -euo pipefail shopt -s nullglob files=(./*.md) nodes=() for file in "${files[@]}"; do [[ ${file##*/} == "MEMORY.md" ]] && continue nodes+=("$file") done NODES=${#nodes[@]}File-list calls should terminate option parsing:
bash grep -h -oE -- '\[\[[a-z_-]+\]\]' "${files[@]}"For incoming-link checks, avoid interpreting the filename as an arbitrary regular expression. Given the currently permitted wikilink alphabet, validate
basebefore use:bash if [[ $base =~ ^[a-z_-]+$ ]]; then pattern="\\[\\[$base\\]\\]" grep -l -E -- "$pattern" "${files[@]}" fiAlternatively, extract wikilink targets once, compare them as fixed strings, and construct graph relationships without dynamically generating regular expressions.
- Stop using
