Domain validation fix

This commit is contained in:
Pinga 2025-02-26 14:50:37 +02:00
parent 2de19b4996
commit a36fb6a908
2 changed files with 34 additions and 28 deletions

View file

@ -230,19 +230,24 @@ function validate_label($domain, $db) {
// Split domain into labels (subdomains, SLD, TLD) // Split domain into labels (subdomains, SLD, TLD)
$labels = explode('.', $domain); $labels = explode('.', $domain);
if (count($labels) > 1) { // Ensure there is at least an SLD and TLD foreach ($labels as $index => $label) {
$firstLabel = $labels[0]; $len = strlen($label);
$len = strlen($firstLabel);
// Label cannot be empty, shorter than 2, or longer than 63 characters // Stricter validation for the first label
if ($len < 2 || $len > 63) { if ($index === 0) {
return 'The domain label must be between 2 and 63 characters'; if ($len < 2 || $len > 63) {
return 'The domain must be between 2 and 63 characters';
}
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/', $label)) {
return 'The domain must start and end with a letter or number and contain only letters, numbers, or hyphens';
}
} }
} // Basic validation for other labels
else {
foreach ($labels as $label) { if (!preg_match('/^[a-zA-Z0-9-]+$/', $label)) {
if (!preg_match('/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$/', $label)) { return 'Each domain label must contain only letters, numbers, or hyphens';
return 'Each domain label must start and end with a letter or number and contain only letters, numbers, or hyphens'; }
} }
// Check if it's a Punycode label (IDN) // Check if it's a Punycode label (IDN)

View file

@ -238,23 +238,24 @@ function validate_label($domain, $pdo) {
// Split domain into labels (subdomains, SLD, TLD) // Split domain into labels (subdomains, SLD, TLD)
$labels = explode('.', $domain); $labels = explode('.', $domain);
if (count($labels) > 1) { // Ensure there is at least an SLD and TLD foreach ($labels as $index => $label) {
$firstLabel = $labels[0]; $len = strlen($label);
$len = strlen($firstLabel);
// Label cannot be empty, shorter than 2, or longer than 63 characters // Stricter validation for the first label
if ($len < 2 || $len > 63) { if ($index === 0) {
return 'The domain label must be between 2 and 63 characters'; if ($len < 2 || $len > 63) {
return 'The domain must be between 2 and 63 characters';
}
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/', $label)) {
return 'The domain must start and end with a letter or number and contain only letters, numbers, or hyphens';
}
} }
} // Basic validation for other labels
else {
foreach ($labels as $label) { if (!preg_match('/^[a-zA-Z0-9-]+$/', $label)) {
if (!preg_match('/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$/', $label)) { return 'Each domain label must contain only letters, numbers, or hyphens';
return 'Each domain label must start and end with a letter or number and contain only letters, numbers, or hyphens'; }
}
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/', $label)) {
return 'Each domain label must start and end with a letter or number and contain only letters, numbers, or hyphens';
} }
// Check if it's a Punycode label (IDN) // Check if it's a Punycode label (IDN)