Enhanced Manual IP Blocking with Detailed Error Handling, CIDR Support, and Bulk Import#24
Enhanced Manual IP Blocking with Detailed Error Handling, CIDR Support, and Bulk Import#24
Conversation
Co-authored-by: skerbis <791247+skerbis@users.noreply.github.com>
Co-authored-by: skerbis <791247+skerbis@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the manual IP blocking system with comprehensive error handling, CIDR notation support, and bulk import functionality. The changes replace generic error messages with detailed feedback, allow blocking of IP ranges, and provide bulk import capabilities for multiple IPs.
- Enhanced error handling with specific error codes and user-friendly messages
- Added CIDR notation support for blocking IP ranges (IPv4/IPv6)
- Implemented bulk import functionality with comment support and detailed results
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pages/ips.blocked.php | Enhanced UI with bulk import form, improved error handling display, client-side validation, and quick action buttons for threat IPs |
| lib/IntrusionPrevention.php | Refactored blockIpManually() to return detailed results, added CIDR validation, bulk import processing, and enhanced isBlocked() method |
Comments suppressed due to low confidence (1)
lib/IntrusionPrevention.php:1993
- The try block is opened here but there's no corresponding catch block visible in this diff section. The catch block should be at the same indentation level as the try statement.
try {
| */ | ||
| private static function validateCidrRange(string $cidr): array | ||
| { | ||
| if (!str_contains($cidr, '/')) { |
There was a problem hiding this comment.
The str_contains() function was introduced in PHP 8.0. For better compatibility with older PHP versions, consider using strpos($cidr, '/') === false instead.
| if (!str_contains($cidr, '/')) { | |
| if (strpos($cidr, '/') === false) { |
| $severityClass = match($threatIp['max_severity']) { | ||
| 'critical' => 'label-danger', | ||
| 'high' => 'label-warning', | ||
| 'medium' => 'label-info', | ||
| default => 'label-default' | ||
| }; |
There was a problem hiding this comment.
The match expression was introduced in PHP 8.0. For better compatibility with older PHP versions, consider using a switch statement or if-elseif chain instead.
| $severityClass = match($threatIp['max_severity']) { | |
| 'critical' => 'label-danger', | |
| 'high' => 'label-warning', | |
| 'medium' => 'label-info', | |
| default => 'label-default' | |
| }; | |
| switch ($threatIp['max_severity']) { | |
| case 'critical': | |
| $severityClass = 'label-danger'; | |
| break; | |
| case 'high': | |
| $severityClass = 'label-warning'; | |
| break; | |
| case 'medium': | |
| $severityClass = 'label-info'; | |
| break; | |
| default: | |
| $severityClass = 'label-default'; | |
| break; | |
| } |
| } else { | ||
| // Single IP validation | ||
| var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; | ||
| var ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$/; |
There was a problem hiding this comment.
The IPv6 regex pattern is incomplete and will not match many valid IPv6 addresses. IPv6 has complex formatting rules including compressed notation (::), mixed notation, and various valid forms. Consider using a more comprehensive regex or a dedicated IPv6 validation library.
| var ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$/; | |
| var ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^:(?::[0-9a-fA-F]{1,4}){1,7}$|^(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}$|^(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}$|^(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}$|^(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^::(?:[fF]{4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3})$/; |
|
|
||
| // Validate IP part | ||
| var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; | ||
| var ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$/; |
There was a problem hiding this comment.
The IPv6 regex is duplicated from line 184. Consider extracting this regex into a variable or function to avoid code duplication and make maintenance easier.
| var ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$/; | |
| var ipv6Regex = getIpv6Regex(); |
This PR completely resolves the manual IP blocking issues reported in #15 where users were receiving generic "Fehler beim Sperren der IP-Adresse" (Error blocking IP address) messages with no specific error details or debugging information.
Problem Solved
The original
blockIpManually()method only returned a boolean value, making it impossible to provide users with specific error feedback. Users would see generic error messages regardless of whether the issue was an invalid IP format, an already blocked IP, or a database error.Solution Overview
Enhanced Error Handling: The
blockIpManually()method now returns a detailed result array with success status, specific error messages, and error codes:CIDR Range Support: Users can now block entire IP ranges using CIDR notation (e.g.,
192.168.1.0/24,2001:db8::/32) with comprehensive validation for both IPv4 and IPv6 networks.Bulk Import Functionality: Added ability to import multiple IPs from text lists with support for comments and detailed result reporting showing exactly which IPs were blocked, which failed, and why.
Key Features Added
1. Comprehensive Error Messages
2. CIDR Network Blocking
10.0.0.0/8,192.168.1.0/24)2001:db8::/32)isBlocked()method checks both exact IPs and CIDR ranges3. Bulk Import System
#prefix for documentation4. Quick Action Integration
5. Enhanced User Interface
Technical Improvements
Backward Compatibility: Added
blockIpManuallyLegacy()wrapper to maintain compatibility with existing code while providing the new detailed error handling.Improved Logging: All blocking operations now generate detailed logs with specific error information for debugging purposes.
Input Validation: Comprehensive validation for IP addresses, CIDR notation, and duration parameters with specific error messages for each validation failure.
Performance Optimization: Efficient CIDR range checking with optimized database queries for both exact IP matches and range-based blocking.
Testing
All functionality has been thoroughly tested including:
This enhancement transforms the manual IP blocking feature from a source of user frustration into a powerful, user-friendly administrative tool with clear feedback and advanced capabilities.
Fixes #15.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.