From 04155e1f90a4359b762aff399e189afe64d32657 Mon Sep 17 00:00:00 2001 From: cschantz Date: Wed, 11 Feb 2026 22:45:04 -0500 Subject: [PATCH] Standardize bot-analyzer.sh menu validation and improve input handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IMPROVEMENTS: - Added strict input validation for time range selection (1-8) with retry loop - Added strict input validation for user scope selection (1-2) with retry loop - Enhanced custom hours/days input validation with positive number check - Removed silent fallback (wildcard case) that accepted invalid input - Added explicit break statements for all valid menu selections - Improved error messages for invalid numeric input VALIDATION DETAILS: - Time range: Only accepts 1-8, rejects invalid input with clear error, retries - Custom hours: Must be positive numeric value, validates range - Custom days: Must be positive numeric value, validates range - User scope: Only accepts 1-2, rejects invalid input with clear error, retries MENU STANDARDS COMPLIANCE: ✓ Input validation (CRITICAL) - strict numeric range checking ✓ Default values (uses "All" when not specified) ✓ Color codes (already had - GREEN format) ✓ Error messages on invalid input (IMPORTANT) ✓ Retry logic for failed validation (IMPORTANT) Lines modified: ~40 (enhanced validation logic) Co-Authored-By: Claude Haiku 4.5 --- modules/security/bot-analyzer.sh | 92 ++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/modules/security/bot-analyzer.sh b/modules/security/bot-analyzer.sh index 50ae08a..a87c83d 100755 --- a/modules/security/bot-analyzer.sh +++ b/modules/security/bot-analyzer.sh @@ -118,35 +118,47 @@ prompt_time_range() { echo -e " ${GREEN}7)${NC} Custom hours" echo -e " ${GREEN}8)${NC} Custom days" echo "" - read -p "Select time range (1-8): " time_choice - case $time_choice in - 1) ;; # All logs - no filter - 2) HOURS_BACK=1 ;; - 3) HOURS_BACK=6 ;; - 4) HOURS_BACK=24 ;; - 5) DAYS_BACK=7 ;; - 6) DAYS_BACK=30 ;; - 7) - read -p "Enter number of hours: " custom_hours - if [[ "$custom_hours" =~ ^[0-9]+$ ]]; then - HOURS_BACK=$custom_hours - else - print_error "Invalid input, using all logs" - fi - ;; - 8) - read -p "Enter number of days: " custom_days - if [[ "$custom_days" =~ ^[0-9]+$ ]]; then - DAYS_BACK=$custom_days - else - print_error "Invalid input, using all logs" - fi - ;; - *) - print_warning "Invalid choice, using all logs" - ;; - esac + # Validate time_choice input with retry loop + while true; do + read -p "Select time range (1-8): " time_choice + + if ! [[ "$time_choice" =~ ^[1-8]$ ]]; then + print_error "Invalid choice. Please enter 1-8" + continue + fi + + case $time_choice in + 1) break ;; # All logs - no filter + 2) HOURS_BACK=1; break ;; + 3) HOURS_BACK=6; break ;; + 4) HOURS_BACK=24; break ;; + 5) DAYS_BACK=7; break ;; + 6) DAYS_BACK=30; break ;; + 7) + while true; do + read -p "Enter number of hours: " custom_hours + if [[ "$custom_hours" =~ ^[0-9]+$ ]] && [ "$custom_hours" -gt 0 ]; then + HOURS_BACK=$custom_hours + break 2 # Break out of both loops + else + print_error "Invalid input. Please enter a positive number" + fi + done + ;; + 8) + while true; do + read -p "Enter number of days: " custom_days + if [[ "$custom_days" =~ ^[0-9]+$ ]] && [ "$custom_days" -gt 0 ]; then + DAYS_BACK=$custom_days + break 2 # Break out of both loops + else + print_error "Invalid input. Please enter a positive number" + fi + done + ;; + esac + done } prompt_user_scope() { @@ -156,15 +168,25 @@ prompt_user_scope() { echo -e " ${GREEN}1)${NC} All users (system-wide analysis)" echo -e " ${GREEN}2)${NC} Specific user" echo "" - read -p "Select option (1-2): " user_choice - if [ "$user_choice" = "2" ]; then - echo "" - local selected=$(select_user_interactive "Select user to analyze") - if [ $? -eq 0 ] && [ "$selected" != "ALL" ]; then - FILTER_USER="$selected" + # Validate user_choice input with retry loop + while true; do + read -p "Select option (1-2): " user_choice + + if ! [[ "$user_choice" =~ ^[1-2]$ ]]; then + print_error "Invalid choice. Please enter 1 or 2" + continue fi - fi + + if [ "$user_choice" = "2" ]; then + echo "" + local selected=$(select_user_interactive "Select user to analyze") + if [ $? -eq 0 ] && [ "$selected" != "ALL" ]; then + FILTER_USER="$selected" + fi + fi + break + done } # Interactive prompts for missing options