fix: improve accuracy of is_valid_hostname() (#7435)

* fix: improve accuracy of is_valid_hostname()
fixes discovery code attempting to add discover invalid dns names
the old code allowed some invalid hostnames, this is more thorough
add tests

* fix merge error
This commit is contained in:
Tony Murray
2017-10-05 16:04:17 -05:00
committed by GitHub
parent b89ed26852
commit a2b9342f05
2 changed files with 30 additions and 5 deletions

View File

@@ -626,8 +626,13 @@ function is_valid_hostname($hostname)
// labels to start with digits. No other symbols, punctuation characters, or
// white space are permitted. While a hostname may not contain other characters,
// such as the underscore character (_), other DNS names may contain the underscore
// maximum length is 253 characters, maximum segment size is 63
return ctype_alnum(str_replace(array('_', '-', '.'), '', $hostname));
return (
preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*\.?$/i", $hostname) //valid chars check
&& preg_match("/^.{1,253}$/", $hostname) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*\.?$/", $hostname)
);
}
/*