From c79f24a50bf2926cdfc0228f438bb20d620eb3e8 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Thu, 30 Jun 2022 15:45:14 -0400 Subject: [PATCH] fixes #3706 URL validation regression --- CRM/Utils/Rule.php | 6 +++++- tests/phpunit/CRM/Utils/RuleTest.php | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 611f228104..746d578a33 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -221,7 +221,11 @@ class CRM_Utils_Rule { // allow relative URL's (CRM-15598) $url = 'http://' . $_SERVER['HTTP_HOST'] . $url; } - return (bool) filter_var(self::idnToAsci($url), FILTER_VALIDATE_URL); + // Convert URLs with Unicode to ASCII + if (strlen($url) != strlen(utf8_decode($url))) { + $url = self::idnToAsci($url); + } + return (bool) filter_var($url, FILTER_VALIDATE_URL); } /** diff --git a/tests/phpunit/CRM/Utils/RuleTest.php b/tests/phpunit/CRM/Utils/RuleTest.php index 098a0fad5f..4cc2d8c836 100644 --- a/tests/phpunit/CRM/Utils/RuleTest.php +++ b/tests/phpunit/CRM/Utils/RuleTest.php @@ -345,7 +345,7 @@ class CRM_Utils_RuleTest extends CiviUnitTestCase { } /** - * Test CVV rule + * Test Email rule * * @param string $email * @param bool $expected expected outcome of the rule validation @@ -370,4 +370,25 @@ class CRM_Utils_RuleTest extends CiviUnitTestCase { return $cases; } + public static function urls(): array { + $urls = []; + $urls[] = ['https://mysite.org/index.php/apps/files/?dir=/Talk/Test%20Folder1/Test%20Folder%202&fileid=597195', TRUE]; + $urls[] = ['http://täst.de', TRUE]; + $urls[] = ['https://الاردن.jo', TRUE]; + $urls[] = ['I didn\'t say Simon Says', FALSE]; + return $urls; + } + + /** + * Test URL rule + * + * @param string $url + * @param bool $expected expected outcome of the rule validation + * + * @dataProvider urls + */ + public function testUrlRule(string $url, bool $expected): void { + $this->assertEquals($expected, CRM_Utils_Rule::url($url)); + } + } -- 2.25.1