validate URLs: convert IDN to punycode before passing URL to filter_var()

This commit is contained in:
Andrew Dolgov 2020-09-22 15:32:22 +03:00
parent 6811d0bde2
commit a897c4165b
1 changed files with 7 additions and 8 deletions

View File

@ -69,6 +69,13 @@ class UrlHelper {
$tokens['path'] = implode("/", array_map("rawurlencode", explode("/", $tokens['path']))); $tokens['path'] = implode("/", array_map("rawurlencode", explode("/", $tokens['path'])));
} }
//convert IDNA hostname to punycode if possible
if (function_exists("idn_to_ascii")) {
if (mb_detect_encoding($tokens['host']) != 'ASCII') {
$tokens['host'] = idn_to_ascii($tokens['host']);
}
}
$url = self::build_url($tokens); $url = self::build_url($tokens);
if (filter_var($url, FILTER_VALIDATE_URL) === false) if (filter_var($url, FILTER_VALIDATE_URL) === false)
@ -82,14 +89,6 @@ class UrlHelper {
return false; return false;
} }
//convert IDNA hostname to punycode if possible
if (function_exists("idn_to_ascii")) {
if (mb_detect_encoding($tokens['host']) != 'ASCII') {
$tokens['host'] = idn_to_ascii($tokens['host']);
$url = self::build_url($tokens);
}
}
return $url; return $url;
} }