diff --git a/classes/DNSQuery.php b/classes/DNSQuery.php index 0aa512c..f699163 100644 --- a/classes/DNSQuery.php +++ b/classes/DNSQuery.php @@ -209,13 +209,19 @@ private function sanitizeHostname(string $hostname): string { throw new InvalidArgumentException("Hostname cannot be empty"); } + // Remove protocol if present (http:// or https://) + $hostname = preg_replace('/^https?:\/\//i', '', $hostname); + + // Remove everything after the first slash (e.g., example.com/path → example.com) + $hostname = preg_replace('/\/.*$/', '', $hostname); + $hostname = preg_replace('/[^a-zA-Z0-9\.\-_:]/', '', $hostname); if (filter_var($hostname, FILTER_VALIDATE_IP)) { return $hostname; } - if (!preg_match('/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/', $hostname)) { + if (!preg_match('/^([a-zA-Z0-9_](?:[a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])?\.)*[a-zA-Z0-9_](?:[a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])?$/', $hostname)) { throw new InvalidArgumentException("Invalid hostname format"); } @@ -541,4 +547,4 @@ private function extractTLD(string $hostname): string { return ''; } -} \ No newline at end of file +}