Skip to content

Commit

Permalink
Do not fail if IDNA<->UNICODE conversion fails
Browse files Browse the repository at this point in the history
Signed-off-by: Christian König <[email protected]>
  • Loading branch information
yubiuser committed Jan 19, 2023
1 parent 678d7b9 commit 1a7b4e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 18 additions & 8 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions scripts/pi-hole/php/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = '';
Expand Down

0 comments on commit 1a7b4e2

Please sign in to comment.