mirror of
https://github.com/walkinglabs/learn-harness-engineering.git
synced 2026-09-19 05:06:27 +08:00
Fix command injection (CWE-78) in audit-harness.sh
any_file_match() used `ls $REPO/$pattern` without quoting $REPO. Because the variable was unquoted during word splitting, a crafted REPO value such as "/tmp; rm -rf /" would be split on the semicolon into a second, independent command — a classic shell injection (CWE-78). Replace the `ls | grep` pipeline with `compgen -G "$REPO/$pattern"`. The entire `$REPO/$pattern` is now double-quoted, so no splitting or pathname expansion happens on $REPO, and compgen performs the glob match without spawning an external process. All other 17 `$REPO` uses in the file were already correctly quoted (inside `[[ ]]`, `"$REPO/..."`, or `find "$REPO"`), so this was the single exploitable point.
This commit is contained in:
@@ -67,8 +67,10 @@ dir_exists() { [[ -d "$REPO/$1" ]] && echo "pass" || echo "fail"; }
|
||||
any_file_match() {
|
||||
# any_file_match "pattern1" "pattern2" ...
|
||||
for pattern in "$@"; do
|
||||
# shellcheck disable=SC2086
|
||||
if ls $REPO/$pattern 2>/dev/null | grep -q .; then
|
||||
# Quote "$REPO/$pattern" to prevent word-splitting / command injection (CWE-78).
|
||||
# compgen -G does glob matching without spawning ls, so a crafted $REPO value
|
||||
# such as "/tmp; rm -rf /" can no longer be split into a second command.
|
||||
if compgen -G "$REPO/$pattern" > /dev/null; then
|
||||
echo "pass"; return
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user