From 68ac05fb7e2849db38f3a50ee3ad096a9d94e93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Thu, 5 Jan 2023 23:16:53 +0100 Subject: [PATCH] Do not fail if IDNA<->UNICODE conversion fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- scripts/pi-hole/php/func.php | 26 ++++++++++++++++++-------- scripts/pi-hole/php/groups.php | 16 +++++----------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index 10f899bac..6a7ed0f5a 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -640,7 +640,7 @@ function getGateway() } // Try to convert possible IDNA domain to Unicode -function convertIDNAToUnicode($unicode) +function convertIDNAToUnicode($IDNA) { if (extension_loaded('intl')) { // we try the UTS #46 standard first @@ -652,32 +652,42 @@ function convertIDNAToUnicode($unicode) // to ensure sparkasse-gießen.de is not converted into // sparkass-giessen.de but into xn--sparkasse-gieen-2ib.de // as mandated by the UTS #46 standard - $unicode = idn_to_utf8($unicode, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); + $unicode = idn_to_utf8($IDNA, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); } elseif (defined('INTL_IDNA_VARIANT_2003')) { // If conversion failed, try with the (deprecated!) IDNA 2003 variant // We have to check for its existence as support of this variant is // scheduled for removal with PHP 8.0 // see https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003 - $unicode = idn_to_utf8($unicode, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003); + $unicode = idn_to_utf8($IDNA, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003); } } - return $unicode; + // if the conversion failed (e.g. domain to long) return the original domain + if ($unicode == false) { + return $IDNA; + } else { + return $unicode; + } } // Convert a given (unicode) domain to IDNA ASCII -function convertUnicodeToIDNA($IDNA) +function convertUnicodeToIDNA($unicode) { if (extension_loaded('intl')) { // Be prepared that this may fail and see our comments about convertIDNAToUnicode() if (defined('INTL_IDNA_VARIANT_UTS46')) { - $IDNA = idn_to_ascii($IDNA, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); + $IDNA = idn_to_ascii($unicode, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); } elseif (defined('INTL_IDNA_VARIANT_2003')) { - $IDNA = idn_to_ascii($IDNA, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003); + $IDNA = idn_to_ascii($unicode, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003); } } - return $IDNA; + // if the conversion failed (e.g. domain to long) return the original domain + if ($IDNA == false) { + return $unicode; + } else { + return $IDNA; + } } // Return PID of FTL (used in settings.php) diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php index 7357b037c..7cd418609 100644 --- a/scripts/pi-hole/php/groups.php +++ b/scripts/pi-hole/php/groups.php @@ -510,14 +510,11 @@ function verify_ID_array($arr) $res['groups'] = $groups; if ($res['type'] === LISTTYPE_WHITELIST || $res['type'] === LISTTYPE_BLACKLIST) { // Convert domain name to international form - // Skip this for the root zone `.` - if ($res['domain'] != '.') { - $utf8_domain = convertIDNAToUnicode($res['domain']); + $utf8_domain = convertIDNAToUnicode($res['domain']); - // if domain and international form are different, show both - if ($res['domain'] !== $utf8_domain) { - $res['domain'] = $utf8_domain.' ('.$res['domain'].')'; - } + // if domain and international form are different, show both + if ($res['domain'] !== $utf8_domain) { + $res['domain'] = $utf8_domain.' ('.$res['domain'].')'; } } // Prevent domain and comment fields from returning any arbitrary javascript code which could be executed on the browser. @@ -600,10 +597,7 @@ function verify_ID_array($arr) // If not adding a RegEx.... $input = $domain; // Convert domain name to IDNA ASCII form for international domains - // Skip this for the root zone `.` - if ($domain != '.') { - $domain = convertUnicodeToIDNA($domain); - } + $domain = convertUnicodeToIDNA($domain); // convert the domain lower case and check whether it is valid $domain = strtolower($domain); $msg = '';