diff --git a/whois/port43/helpers.php b/whois/port43/helpers.php index 5486d24..c43bb4f 100644 --- a/whois/port43/helpers.php +++ b/whois/port43/helpers.php @@ -112,4 +112,44 @@ function updatePermittedIPs($pool, $permittedIPsTable) { foreach ($permittedIPs as $ip) { $permittedIPsTable->set($ip, ['addr' => $ip]); } +} + +function isValidHostname($hostname) { + $hostname = trim($hostname); + + // Convert IDN (Unicode) to ASCII if necessary + if (mb_detect_encoding($hostname, 'ASCII', true) === false) { + $hostname = idn_to_ascii($hostname, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); + if ($hostname === false) { + return false; // Invalid IDN conversion + } + } + + // Ensure there is at least **one dot** (to prevent single-segment hostnames) + if (substr_count($hostname, '.') < 1) { + return false; + } + + // Regular expression for validating a hostname + $pattern = '/^((xn--[a-zA-Z0-9-]{1,63}|[a-zA-Z0-9-]{1,63})\.)*([a-zA-Z0-9-]{1,63}|xn--[a-zA-Z0-9-]{2,63})$/'; + + // Ensure it matches the hostname pattern + if (!preg_match($pattern, $hostname)) { + return false; + } + + // Ensure no label exceeds 63 characters + $labels = explode('.', $hostname); + foreach ($labels as $label) { + if (strlen($label) > 63) { + return false; + } + } + + // Ensure full hostname is not longer than 255 characters + if (strlen($hostname) > 255) { + return false; + } + + return true; } \ No newline at end of file diff --git a/whois/port43/start_whois.php b/whois/port43/start_whois.php index a7e14cc..08d97ec 100644 --- a/whois/port43/start_whois.php +++ b/whois/port43/start_whois.php @@ -657,12 +657,12 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool $nameserver = $convertedDomain; } } - - if (!preg_match('/^((xn--[a-zA-Z0-9-]{1,63}|[a-zA-Z0-9-]{1,63})\.){2,}(xn--[a-zA-Z0-9-]{2,63}|[a-zA-Z]{2,63})$/', $nameserver)) { + + if (!isValidHostname($nameserver)) { $server->send($fd, "Nameserver contains invalid characters or is not in the correct format."); $server->close($fd); } - + $query = "SELECT name,clid FROM host WHERE name = :nameserver"; $stmt = $pdo->prepare($query); $stmt->bindParam(':nameserver', $nameserver, PDO::PARAM_STR); diff --git a/whois/port43/whois_limited.php b/whois/port43/whois_limited.php index 4f7ad10..d782b12 100644 --- a/whois/port43/whois_limited.php +++ b/whois/port43/whois_limited.php @@ -295,12 +295,12 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool $nameserver = $convertedDomain; } } - - if (!preg_match('/^((xn--[a-zA-Z0-9-]{1,63}|[a-zA-Z0-9-]{1,63})\.){2,}(xn--[a-zA-Z0-9-]{2,63}|[a-zA-Z]{2,63})$/', $nameserver)) { + + if (!isValidHostname($nameserver)) { $server->send($fd, "Nameserver contains invalid characters or is not in the correct format."); $server->close($fd); } - + $query = "SELECT name,clid FROM host WHERE name = :nameserver"; $stmt = $pdo->prepare($query); $stmt->bindParam(':nameserver', $nameserver, PDO::PARAM_STR);