ValidateWhiteList use IDN punycode

This commit is contained in:
the-djmaze 2024-03-12 00:42:09 +01:00
parent a093eb3231
commit 9e562ac882
2 changed files with 24 additions and 35 deletions

View file

@ -165,16 +165,6 @@ abstract class Utils
|| !\preg_match('/[^\x09\x10\x13\x0A\x0D\x20-\x7E]/', $sValue); || !\preg_match('/[^\x09\x10\x13\x0A\x0D\x20-\x7E]/', $sValue);
} }
private static function StrMailDomainToLower(string $sValue) : string
{
$aParts = \explode('@', $sValue);
if ($iLast) {
$iLast = \count($aParts) - 1;
$aParts[$iLast] = \mb_strtolower($aParts[$iLast]);
}
return \implode('@', $aParts);
}
public static function StripSpaces(string $sValue) : string public static function StripSpaces(string $sValue) : string
{ {
return static::Trim( return static::Trim(
@ -421,19 +411,14 @@ abstract class Utils
public static function GetAccountNameFromEmail(string $sEmail) : string public static function GetAccountNameFromEmail(string $sEmail) : string
{ {
$sResult = ''; $iPos = \strrpos($sEmail, '@');
if (\strlen($sEmail)) { return (false === $iPos) ? $sEmail : \substr($sEmail, 0, $iPos);
$iPos = \strrpos($sEmail, '@');
$sResult = (false === $iPos) ? $sEmail : \substr($sEmail, 0, $iPos);
}
return $sResult;
} }
public static function GetDomainFromEmail(string $sEmail) : string public static function GetDomainFromEmail(string $sEmail) : string
{ {
$aParts = \explode('@', $sEmail); $iPos = \strrpos($sEmail, '@');
return isset($aParts[1]) ? \array_pop($aParts) : ''; return (false === $iPos) ? '' : \substr($sEmail, $iPos + 1);
} }
public static function GetClearDomainName(string $sDomain) : string public static function GetClearDomainName(string $sDomain) : string
@ -693,17 +678,16 @@ abstract class Utils
/** /**
* Converts xn--du8h.snappymail.eu to 📧.snappymail.eu * Converts xn--du8h.snappymail.eu to 📧.snappymail.eu
*/ */
public static function IdnToUtf8(string $sStr, bool $bLowerCase = false) : string public static function IdnToUtf8(string $sStr) : string
{ {
if (\strlen($sStr) && \preg_match('/(^|\.|@)xn--/i', $sStr)) { if (\preg_match('/(^|\.|@)xn--/i', $sStr)) {
try try
{ {
$sStr = \SnappyMail\IDN::anyToUtf8($sStr); $sStr = \SnappyMail\IDN::anyToUtf8($sStr);
} }
catch (\Throwable $oException) {} catch (\Throwable $oException) {}
} }
return $sStr;
return $bLowerCase ? static::StrMailDomainToLower($sStr) : $sStr;
} }
/** /**

View file

@ -102,21 +102,26 @@ class Domain implements \JsonSerializable
$this->aliasName = \strtolower(\idn_to_ascii($sAliasName)); $this->aliasName = \strtolower(\idn_to_ascii($sAliasName));
} }
public function ValidateWhiteList(string $sEmail, string $sLogin = '') : bool public function ValidateWhiteList(string $sEmail, string $sLogin) : bool
{ {
$sW = \trim($this->whiteList); $sW = \trim($this->whiteList);
if (\strlen($sW)) if ($sW) {
{ $sEmail = \mb_strtolower($sEmail);
$sEmail = \MailSo\Base\Utils::IdnToUtf8($sEmail, true); $sLogin = \mb_strtolower($sLogin);
$sLogin = \MailSo\Base\Utils::IdnToUtf8($sLogin, true); $sUserPart = \MailSo\Base\Utils::GetAccountNameFromEmail($sLogin ?: $sEmail);
$sItem = \strtok($sW, " ;,\n");
$sW = \preg_replace('/([^\s]+)@[^\s]*/', '$1', $sW); while (false !== $sItem) {
$sW = ' '.\trim(\preg_replace('/[\s;,\r\n\t]+/', ' ', $sW)).' '; $sItem = \mb_strtolower(\idn_to_ascii(\trim($sItem)));
if ($sItem && (
$sUserPart = \MailSo\Base\Utils::GetAccountNameFromEmail(\strlen($sLogin) ? $sLogin : $sEmail); $sLogin === $sItem || $sEmail === $sItem
return false !== \stripos($sW, ' '.$sUserPart.' '); || $sUserPart === $sItem || \str_starts_with($sItem, "{$sUserPart}@")
)) {
return true;
}
$sItem = \strtok(" ;,\n");
}
return false;
} }
return true; return true;
} }