isSuspiciousRequest($uri)) { http_response_code(403); echo "
Access denied due to security policy.
"; return; } // Check request size if ($this->isRequestTooLarge()) { http_response_code(413); echo "Request size exceeds allowed limit.
"; return; } // Continue to next middleware $next(); } /** * Check for suspicious request patterns */ private function isSuspiciousRequest(string $uri): bool { $suspiciousPatterns = [ '/\.\./', // Directory traversal '/\.env/', // Environment file access '/\.git/', // Git directory access '/\.htaccess/', // Apache config access '/\.htpasswd/', // Apache password file '/admin\.php/', // Admin file access '/config\.php/', // Config file access '/wp-admin/', // WordPress admin '/wp-login/', // WordPress login '/phpmyadmin/', // phpMyAdmin '/\.sql/', // SQL file access '/\.bak/', // Backup file access ]; foreach ($suspiciousPatterns as $pattern) { if (preg_match($pattern, $uri)) { return true; } } return false; } /** * Check if request is too large */ private function isRequestTooLarge(): bool { $maxSize = 10 * 1024 * 1024; // 10MB $contentLength = (int)($_SERVER['CONTENT_LENGTH'] ?? 0); return $contentLength > $maxSize; } }