From 45e115ec4bd9c71dc8ef0a9b6582b899234d0f5f Mon Sep 17 00:00:00 2001 From: cschantz Date: Fri, 2 Jan 2026 17:26:21 -0500 Subject: [PATCH] Fix SOURCE command safety issues (HIGH priority) Added existence checks and error handling for all source commands to prevent silent failures when dependencies are missing. Library files (use 'return' for error): - reference-db.sh: Added checks for 3 dependencies - mysql-analyzer.sh: Added checks for 3 dependencies - domain-discovery.sh: Added checks for 2 dependencies - system-detect.sh: Added check for common-functions.sh - plesk-helpers.sh: Added check for common-functions.sh - user-manager.sh: Added checks for 2 dependencies Executable scripts (use 'exit' for error): - wordpress-cron-manager.sh: Added checks for 2 dependencies - website-error-analyzer.sh: Added checks for 4 dependencies Pattern: [ -f "file" ] && source "file" || { echo "ERROR" >&2; return/exit 1; } This ensures scripts fail fast with clear error messages when required dependencies are missing, rather than continuing with undefined functions. --- lib/domain-discovery.sh | 5 +++-- lib/mysql-analyzer.sh | 7 ++++--- lib/plesk-helpers.sh | 2 +- lib/reference-db.sh | 7 ++++--- lib/system-detect.sh | 2 +- lib/user-manager.sh | 5 +++-- modules/website/website-error-analyzer.sh | 9 +++++---- modules/website/wordpress/wordpress-cron-manager.sh | 5 +++-- 8 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/domain-discovery.sh b/lib/domain-discovery.sh index 9b47b7f..43a2ebf 100644 --- a/lib/domain-discovery.sh +++ b/lib/domain-discovery.sh @@ -8,8 +8,9 @@ # Source dependencies if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$SCRIPT_DIR/common-functions.sh" - source "$SCRIPT_DIR/system-detect.sh" + + [ -f "$SCRIPT_DIR/common-functions.sh" ] && source "$SCRIPT_DIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } + [ -f "$SCRIPT_DIR/system-detect.sh" ] && source "$SCRIPT_DIR/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; return 1; } fi # Source control panel helpers if available diff --git a/lib/mysql-analyzer.sh b/lib/mysql-analyzer.sh index 87733c5..60fb4e6 100755 --- a/lib/mysql-analyzer.sh +++ b/lib/mysql-analyzer.sh @@ -8,9 +8,10 @@ # Source dependencies if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$SCRIPT_DIR/common-functions.sh" - source "$SCRIPT_DIR/system-detect.sh" - source "$SCRIPT_DIR/user-manager.sh" + + [ -f "$SCRIPT_DIR/common-functions.sh" ] && source "$SCRIPT_DIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } + [ -f "$SCRIPT_DIR/system-detect.sh" ] && source "$SCRIPT_DIR/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; return 1; } + [ -f "$SCRIPT_DIR/user-manager.sh" ] && source "$SCRIPT_DIR/user-manager.sh" || { echo "ERROR: user-manager.sh not found" >&2; return 1; } fi ############################################################################# diff --git a/lib/plesk-helpers.sh b/lib/plesk-helpers.sh index 3c70c45..e0f84b2 100644 --- a/lib/plesk-helpers.sh +++ b/lib/plesk-helpers.sh @@ -8,7 +8,7 @@ # Source common functions if not already loaded if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$SCRIPT_DIR/common-functions.sh" + [ -f "$SCRIPT_DIR/common-functions.sh" ] && source "$SCRIPT_DIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } fi ############################################################################# diff --git a/lib/reference-db.sh b/lib/reference-db.sh index 38a252f..4c576f6 100755 --- a/lib/reference-db.sh +++ b/lib/reference-db.sh @@ -9,9 +9,10 @@ # Source dependencies if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$SCRIPT_DIR/common-functions.sh" - source "$SCRIPT_DIR/system-detect.sh" - source "$SCRIPT_DIR/user-manager.sh" + + [ -f "$SCRIPT_DIR/common-functions.sh" ] && source "$SCRIPT_DIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } + [ -f "$SCRIPT_DIR/system-detect.sh" ] && source "$SCRIPT_DIR/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; return 1; } + [ -f "$SCRIPT_DIR/user-manager.sh" ] && source "$SCRIPT_DIR/user-manager.sh" || { echo "ERROR: user-manager.sh not found" >&2; return 1; } fi # Reference database location diff --git a/lib/system-detect.sh b/lib/system-detect.sh index 88d6b19..745b606 100755 --- a/lib/system-detect.sh +++ b/lib/system-detect.sh @@ -9,7 +9,7 @@ # Source common functions if not already loaded if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$SCRIPT_DIR/common-functions.sh" + [ -f "$SCRIPT_DIR/common-functions.sh" ] && source "$SCRIPT_DIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } fi # Global variables (session-only) - only initialize if not already set diff --git a/lib/user-manager.sh b/lib/user-manager.sh index 96b7de6..22d73e4 100755 --- a/lib/user-manager.sh +++ b/lib/user-manager.sh @@ -8,8 +8,9 @@ # Source dependencies if [ -z "$TOOLKIT_BASE_DIR" ]; then _LIB_SRCDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - source "$_LIB_SRCDIR/common-functions.sh" - source "$_LIB_SRCDIR/system-detect.sh" + + [ -f "$_LIB_SRCDIR/common-functions.sh" ] && source "$_LIB_SRCDIR/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; return 1; } + [ -f "$_LIB_SRCDIR/system-detect.sh" ] && source "$_LIB_SRCDIR/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; return 1; } fi # Initialize temp session directory if not set diff --git a/modules/website/website-error-analyzer.sh b/modules/website/website-error-analyzer.sh index ba08956..382bfe8 100755 --- a/modules/website/website-error-analyzer.sh +++ b/modules/website/website-error-analyzer.sh @@ -9,10 +9,11 @@ ################################################################################ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" -source "$SCRIPT_DIR/lib/common-functions.sh" -source "$SCRIPT_DIR/lib/system-detect.sh" -source "$SCRIPT_DIR/lib/user-manager.sh" -source "$SCRIPT_DIR/lib/ip-reputation.sh" + +[ -f "$SCRIPT_DIR/lib/common-functions.sh" ] && source "$SCRIPT_DIR/lib/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; exit 1; } +[ -f "$SCRIPT_DIR/lib/system-detect.sh" ] && source "$SCRIPT_DIR/lib/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; exit 1; } +[ -f "$SCRIPT_DIR/lib/user-manager.sh" ] && source "$SCRIPT_DIR/lib/user-manager.sh" || { echo "ERROR: user-manager.sh not found" >&2; exit 1; } +[ -f "$SCRIPT_DIR/lib/ip-reputation.sh" ] && source "$SCRIPT_DIR/lib/ip-reputation.sh" || { echo "ERROR: ip-reputation.sh not found" >&2; exit 1; } # Configuration - Use system-detected paths APACHE_ERROR_LOG="/var/log/apache2/error_log" # Will be auto-detected diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index 2b476ab..55186f3 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -12,8 +12,9 @@ ################################################################################ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" -source "$SCRIPT_DIR/lib/common-functions.sh" -source "$SCRIPT_DIR/lib/system-detect.sh" + +[ -f "$SCRIPT_DIR/lib/common-functions.sh" ] && source "$SCRIPT_DIR/lib/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; exit 1; } +[ -f "$SCRIPT_DIR/lib/system-detect.sh" ] && source "$SCRIPT_DIR/lib/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; exit 1; } if [ "$EUID" -ne 0 ]; then print_error "This script must be run as root"