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);
}
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
{
return static::Trim(
@ -421,19 +411,14 @@ abstract class Utils
public static function GetAccountNameFromEmail(string $sEmail) : string
{
$sResult = '';
if (\strlen($sEmail)) {
$iPos = \strrpos($sEmail, '@');
$sResult = (false === $iPos) ? $sEmail : \substr($sEmail, 0, $iPos);
}
return $sResult;
$iPos = \strrpos($sEmail, '@');
return (false === $iPos) ? $sEmail : \substr($sEmail, 0, $iPos);
}
public static function GetDomainFromEmail(string $sEmail) : string
{
$aParts = \explode('@', $sEmail);
return isset($aParts[1]) ? \array_pop($aParts) : '';
$iPos = \strrpos($sEmail, '@');
return (false === $iPos) ? '' : \substr($sEmail, $iPos + 1);
}
public static function GetClearDomainName(string $sDomain) : string
@ -693,17 +678,16 @@ abstract class Utils
/**
* 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
{
$sStr = \SnappyMail\IDN::anyToUtf8($sStr);
}
catch (\Throwable $oException) {}
}
return $bLowerCase ? static::StrMailDomainToLower($sStr) : $sStr;
return $sStr;
}
/**

View file

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