From 63e6cf067e93b1c02c678a3314d55c446e2c1f7a Mon Sep 17 00:00:00 2001 From: Developer Date: Thu, 23 Apr 2026 21:19:37 -0400 Subject: [PATCH] CRITICAL: Add error handling to stats dashboard calculations - Line 2290: Added 2>/dev/null fallback to wc for total_requests - Line 2291: Added 2>/dev/null fallback to unique_ips calculation - Line 2292: Added 2>/dev/null fallback to unique_domains calculation - Line 2293: Added 2>/dev/null fallback to bot_requests calculation - Line 2296: Improved error handling for private_ips calculation - Line 2302: Fixed UUOC (cat | grep) pattern - removed useless cat These operations lack proper error handling and would crash with set -e if files are missing or malformed. Also removed inefficient cat pipe. --- modules/security/bot-analyzer.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/security/bot-analyzer.sh b/modules/security/bot-analyzer.sh index 4d9d9ad..4a2ff43 100755 --- a/modules/security/bot-analyzer.sh +++ b/modules/security/bot-analyzer.sh @@ -2287,19 +2287,19 @@ generate_report() { # QUICK STATS DASHBOARD print_header "QUICK STATS DASHBOARD" - total_requests=$(wc -l < "$TEMP_DIR/parsed_logs.txt") - unique_ips=$(awk -F'|' '{print $1}' < "$TEMP_DIR/parsed_logs.txt" | sort -u | wc -l) - unique_domains=$(awk -F'|' '{print $2}' < "$TEMP_DIR/parsed_logs.txt" | sort -u | wc -l) - bot_requests=$(awk -F'|' '$9 != "unknown"' < "$TEMP_DIR/classified_bots.txt" | wc -l) + total_requests=$(wc -l < "$TEMP_DIR/parsed_logs.txt" 2>/dev/null || echo "0") + unique_ips=$(awk -F'|' '{print $1}' < "$TEMP_DIR/parsed_logs.txt" 2>/dev/null | sort -u | wc -l || echo "0") + unique_domains=$(awk -F'|' '{print $2}' < "$TEMP_DIR/parsed_logs.txt" 2>/dev/null | sort -u | wc -l || echo "0") + bot_requests=$(awk -F'|' '$9 != "unknown"' < "$TEMP_DIR/classified_bots.txt" 2>/dev/null | wc -l || echo "0") # Count private/internal IPs (excluded from threat analysis) - private_ips=$(awk -F'|' '{print $1}' < "$TEMP_DIR/parsed_logs.txt" | sort -u | grep -E '^(127\.|10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.|169\.254\.)' || true | wc -l) + private_ips=$(awk -F'|' '{print $1}' < "$TEMP_DIR/parsed_logs.txt" 2>/dev/null | sort -u | grep -E '^(127\.|10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.|169\.254\.)' 2>/dev/null | wc -l || echo "0") # Count server's own IPs in the logs server_ip_hits=0 if [ -f "$TEMP_DIR/server_ips.txt" ] && [ -s "$TEMP_DIR/server_ips.txt" ]; then while read -r server_ip; do - if cat "$TEMP_DIR/parsed_logs.txt" | grep -q "^$server_ip|" 2>/dev/null; then + if grep -q "^$server_ip|" "$TEMP_DIR/parsed_logs.txt" 2>/dev/null; then server_ip_hits=$((server_ip_hits + 1)) fi done < "$TEMP_DIR/server_ips.txt"