From 5df85a46f18e93df899803e43f524c1df258f4c6 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 20 Sep 2017 08:45:33 +1000 Subject: [PATCH] Move to using CRM_Utils_Rule and CRM_Utils_Type::validate as per Sean's comments --- CRM/Admin/Form/Extensions.php | 13 +------- CRM/Utils/Rule.php | 11 +++++++ CRM/Utils/Type.php | 6 ++++ .../phpunit/CRM/Admin/Form/ExtensionTest.php | 33 ------------------- tests/phpunit/CRM/Utils/RuleTest.php | 20 +++++++++++ 5 files changed, 38 insertions(+), 45 deletions(-) delete mode 100644 tests/phpunit/CRM/Admin/Form/ExtensionTest.php diff --git a/CRM/Admin/Form/Extensions.php b/CRM/Admin/Form/Extensions.php index ffb07c1dc5..7f273cb163 100644 --- a/CRM/Admin/Form/Extensions.php +++ b/CRM/Admin/Form/Extensions.php @@ -36,17 +36,6 @@ */ class CRM_Admin_Form_Extensions extends CRM_Admin_Form { - /** - * @param string $key Extension Key to check - * @return bool - */ - public static function checkExtesnionKeyIsValid($key = NULL) { - if (!empty($key) && !preg_match('/^[0-9a-zA-Z._-]+$/', $key)) { - return FALSE; - } - return TRUE; - } - /** * Form pre-processing. */ @@ -56,7 +45,7 @@ class CRM_Admin_Form_Extensions extends CRM_Admin_Form { $this->_key = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, 0 ); - if (!self::checkExtesnionKeyIsValid($this->_key)) { + if (!CRM_Utils_Type::validate($this->_key, 'ExtensionKey')) { throw new CRM_Core_Exception('Extension Key does not match expected standard'); } $session = CRM_Core_Session::singleton(); diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 897aa5dbbd..c9a92fa967 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -911,4 +911,15 @@ class CRM_Utils_Rule { } } + /** + * @param string $key Extension Key to check + * @return bool + */ + public static function checkExtesnionKeyIsValid($key = NULL) { + if (!empty($key) && !preg_match('/^[0-9a-zA-Z._-]+$/', $key)) { + return FALSE; + } + return TRUE; + } + } diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index 43b920f8be..fbe1c934e7 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -466,6 +466,12 @@ class CRM_Utils_Type { } break; + case 'ExtensionKey': + if (CRM_Utils_Rule::checkExtesnionKeyIsValid($data)) { + return $data; + } + break; + default: CRM_Core_Error::fatal("Cannot recognize $type for $data"); break; diff --git a/tests/phpunit/CRM/Admin/Form/ExtensionTest.php b/tests/phpunit/CRM/Admin/Form/ExtensionTest.php deleted file mode 100644 index 469c3c4787..0000000000 --- a/tests/phpunit/CRM/Admin/Form/ExtensionTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertFalse(CRM_Admin_Form_Extensions::checkExtesnionKeyIsValid($key)); - } - else { - $this->assertTrue(CRM_Admin_Form_Extensions::checkExtesnionKeyIsValid($key)); - } - } - -} diff --git a/tests/phpunit/CRM/Utils/RuleTest.php b/tests/phpunit/CRM/Utils/RuleTest.php index b6abcfa22f..9db05dbaa3 100644 --- a/tests/phpunit/CRM/Utils/RuleTest.php +++ b/tests/phpunit/CRM/Utils/RuleTest.php @@ -112,4 +112,24 @@ class CRM_Utils_RuleTest extends CiviUnitTestCase { ); } + /** + * @return array + */ + public function extenionKeyTests() { + $keys = array(); + $keys[] = array('org.civicrm.multisite', TRUE); + $keys[] = array('au.org.contribute2016', TRUE); + $keys[] = array('%3Csvg%20onload=alert(0)%3E', FALSE); + return $keys; + } + + /** + * @param $key + * @param $expectedResult + * @dataProvider extenionKeyTests + */ + public function testExtenionKeyValid($key, $expectedResult) { + $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtesnionKeyIsValid($key)); + } + } -- 2.25.1