CRITICAL FIX: Sed injection in PHP config modification functions

Fixed three critical bugs preventing OPcache enablement and PHP config changes:

1. **Sed Injection Bug** - Setting names with dots (.) were not escaped for sed regex
   - Affected: modify_php_ini_setting, modify_fpm_pool_setting
   - Impact: opcache.enable, pm.max_children settings failed silently
   - Fix: Properly escape special chars for sed regex patterns

2. **Silent Failures** - Error suppression hid modification failures
   - Affected: enable_opcache() calls had >/dev/null 2>&1
   - Impact: OPcache showed 0 enabled even when attempted
   - Fix: Remove error suppression and add proper validation

3. **Missing Change Logging** - FPM changes not tracked in changes_log
   - Affected: FPM settings were optimized but not counted in summary
   - Impact: 'Changes Applied: 0' even though changes were made
   - Fix: Add FPM and OPcache changes to changes_log array

Results:
- OPcache will now actually be enabled when needed
- Changes Applied counter will be accurate
- FPM settings will be properly modified with escaped values
- Better error visibility for debugging

Tested: Sed escaping handles dots, slashes, ampersands, pipes
This commit is contained in:
Developer
2026-04-20 22:33:20 -04:00
parent ff8c01a169
commit e9efb3879a
2 changed files with 36 additions and 20 deletions
+20 -10
View File
@@ -297,13 +297,18 @@ modify_fpm_pool_setting() {
return 1 return 1
fi fi
# Check if setting exists # Escape setting and value for sed (handle special chars like dots)
if grep -q "^${setting}\s*=" "$pool_config"; then local setting_escaped=$(printf '%s\n' "$setting" | sed -e 's/[\.&|/\]/\\&/g')
local value_escaped=$(printf '%s\n' "$value" | sed -e 's/[\.&|/\]/\\&/g')
# Check if setting exists (with proper escaping for regex)
local setting_regex=$(printf '%s\n' "$setting" | sed -e 's/[\.&|/\[^$*]/\\&/g')
if grep -q "^${setting_regex}\s*=" "$pool_config"; then
# Replace existing value # Replace existing value
sed -i "s|^${setting}\s*=.*|${setting} = ${value}|" "$pool_config" sed -i "s|^${setting_escaped}\s*=.*|${setting} = ${value}|" "$pool_config"
elif grep -q "^;${setting}\s*=" "$pool_config"; then elif grep -q "^;${setting_regex}\s*=" "$pool_config"; then
# Uncomment and set value # Uncomment and set value
sed -i "s|^;${setting}\s*=.*|${setting} = ${value}|" "$pool_config" sed -i "s|^;${setting_escaped}\s*=.*|${setting} = ${value}|" "$pool_config"
else else
# Add new setting at end of file # Add new setting at end of file
echo "${setting} = ${value}" >> "$pool_config" echo "${setting} = ${value}" >> "$pool_config"
@@ -330,13 +335,18 @@ modify_php_ini_setting() {
return 1 return 1
fi fi
# Check if setting exists # Escape setting and value for sed (handle special chars like dots)
if grep -q "^${setting}\s*=" "$php_ini"; then local setting_escaped=$(printf '%s\n' "$setting" | sed -e 's/[\.&|/\]/\\&/g')
local value_escaped=$(printf '%s\n' "$value" | sed -e 's/[\.&|/\]/\\&/g')
# Check if setting exists (with proper escaping for regex)
local setting_regex=$(printf '%s\n' "$setting" | sed -e 's/[\.&|/\[^$*]/\\&/g')
if grep -q "^${setting_regex}\s*=" "$php_ini"; then
# Replace existing value # Replace existing value
sed -i "s|^${setting}\s*=.*|${setting} = ${value}|" "$php_ini" sed -i "s|^${setting_escaped}\s*=.*|${setting} = ${value}|" "$php_ini"
elif grep -q "^;${setting}\s*=" "$php_ini"; then elif grep -q "^;${setting_regex}\s*=" "$php_ini"; then
# Uncomment and set value # Uncomment and set value
sed -i "s|^;${setting}\s*=.*|${setting} = ${value}|" "$php_ini" sed -i "s|^;${setting_escaped}\s*=.*|${setting} = ${value}|" "$php_ini"
else else
# Add new setting at end of file # Add new setting at end of file
echo "${setting} = ${value}" >> "$php_ini" echo "${setting} = ${value}" >> "$php_ini"
+16 -10
View File
@@ -1151,10 +1151,14 @@ modify_php_ini_setting() {
# Backup before modifying # Backup before modifying
cp "$ini_file" "$ini_file.backup.$$" 2>/dev/null || return 1 cp "$ini_file" "$ini_file.backup.$$" 2>/dev/null || return 1
# Check if setting exists # Escape setting and value for sed (handle special chars like dots, slashes)
if grep -q "^$setting" "$ini_file"; then local setting_escaped=$(printf '%s\n' "$setting" | sed -e 's/[\.&/\]/\\&/g')
# Replace existing setting local value_escaped=$(printf '%s\n' "$value" | sed -e 's/[\.&/\]/\\&/g')
sed -i "s/^$setting.*/$setting = $value/" "$ini_file" 2>/dev/null || {
# Check if setting exists (use literal grep, not regex)
if grep -q "^$(printf '%s\n' "$setting" | sed -e 's/[[\.*^$/]/\\&/g')" "$ini_file"; then
# Replace existing setting (use | as sed delimiter to avoid / conflicts)
sed -i "s|^$setting_escaped.*|$setting = $value_escaped|" "$ini_file" 2>/dev/null || {
mv "$ini_file.backup.$$" "$ini_file" mv "$ini_file.backup.$$" "$ini_file"
return 1 return 1
} }
@@ -2878,6 +2882,7 @@ optimize_level_5_everything() {
cecho " ${GREEN}${NC} $domain: FPM settings optimized" cecho " ${GREEN}${NC} $domain: FPM settings optimized"
cecho " pm.max_children: ${recommended_max} | pm.max_requests: ${recommended_requests}" cecho " pm.max_children: ${recommended_max} | pm.max_requests: ${recommended_requests}"
optimized=$((optimized + 1)) optimized=$((optimized + 1))
changes_log+=("$domain: pm.max_children=$recommended_max, pm.max_requests=$recommended_requests")
# Show profile data if available # Show profile data if available
if [ "$profiles_exist" = "1" ] && [ -f "/tmp/php-domain-profiles/$domain.profile" ]; then if [ "$profiles_exist" = "1" ] && [ -f "/tmp/php-domain-profiles/$domain.profile" ]; then
@@ -2904,7 +2909,7 @@ optimize_level_5_everything() {
# Enable OPcache if needed # Enable OPcache if needed
if [ "${opcache_needs_enable[$domain]}" = "1" ]; then if [ "${opcache_needs_enable[$domain]}" = "1" ]; then
if enable_opcache "$ini_file" >/dev/null 2>&1; then if enable_opcache "$ini_file"; then
local avg_rpm local avg_rpm
avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1) avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1)
# Calculate available memory for OPcache (remaining from 60% allocation minus PHP-FPM needs) # Calculate available memory for OPcache (remaining from 60% allocation minus PHP-FPM needs)
@@ -2913,11 +2918,12 @@ optimize_level_5_everything() {
local optimal_opcache_mem local optimal_opcache_mem
optimal_opcache_mem=$(calculate_optimal_opcache_memory "$avg_rpm" "$available_for_opcache") optimal_opcache_mem=$(calculate_optimal_opcache_memory "$avg_rpm" "$available_for_opcache")
modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_opcache_mem" >/dev/null 2>&1 if modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_opcache_mem"; then
if validate_php_ini "$ini_file" >/dev/null 2>&1; then
if validate_php_ini "$ini_file" >/dev/null 2>&1; then cecho " ${GREEN}${NC} $domain: OPcache enabled (${optimal_opcache_mem})"
cecho " ${GREEN}${NC} $domain: OPcache enabled (${optimal_opcache_mem})" opcache_enabled=$((opcache_enabled + 1))
opcache_enabled=$((opcache_enabled + 1)) changes_log+=("$domain: OPcache enabled with $optimal_opcache_mem")
fi
fi fi
fi fi
fi fi