fixes #3706 URL validation regression
authorJon Goldberg <jon@megaphonetech.com>
Thu, 30 Jun 2022 19:45:14 +0000 (15:45 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Thu, 30 Jun 2022 19:52:57 +0000 (15:52 -0400)
CRM/Utils/Rule.php
tests/phpunit/CRM/Utils/RuleTest.php

index 611f2281043a65394f3d7210ffeaae3434880d07..746d578a33f639e533ec6b0988295aa6200f29f2 100644 (file)
@@ -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);
   }
 
   /**
index 098a0fad5f3655184c00169bd1806ab6296cb91f..4cc2d8c836a191a8ae2314844b5a45a72a6746d2 100644 (file)
@@ -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));
+  }
+
 }