Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions bin/idstack-learnings-search
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ fi

[ -z "$SOURCES" ] && exit 0

fallback_search() {
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
fi
Comment on lines +40 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Robustness & Standards Compliance

  1. Option Injection: If $KEYWORD or $TYPE starts with a hyphen (e.g., -v), grep will interpret it as a command-line option rather than a search pattern. Using -- signals the end of options and prevents this.
  2. POSIX Compliance: The tail -"$LIMIT" syntax is obsolete/deprecated in modern POSIX standards. Using tail -n "$LIMIT" is the standard, portable way to specify the line limit.
  if [ -n "$KEYWORD" ]; then
    cat $SOURCES 2>/dev/null | grep -i -- "$KEYWORD" | tail -n "$LIMIT"
  elif [ -n "$TYPE" ]; then
    cat $SOURCES 2>/dev/null | grep -- "\"type\":\"$TYPE\"" | tail -n "$LIMIT"
  else
    cat $SOURCES 2>/dev/null | tail -n "$LIMIT"
  fi

}

if command -v python3 &>/dev/null; then
python3 -c "
import json, sys
Expand Down Expand Up @@ -71,20 +81,8 @@ for m in matches[-limit:]:
print(m)
" 2>/dev/null || {
# Fallback: basic grep
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
fi
fallback_search
}
else
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
fi
fallback_search
fi