diff options
author | Peter Zhang <peter@innocraft.com> | 2021-10-15 06:57:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-15 06:57:37 +0300 |
commit | 71c98f36ec238b6d688065963f196c387850da53 (patch) | |
tree | a0214b395b94c40160c176269993a664f0ad85b9 /plugins/CustomDimensions | |
parent | a78166523e8d2662c33eba928665c3a173ef7a55 (diff) |
Add Regex check on customer dimensions (#18158)
Diffstat (limited to 'plugins/CustomDimensions')
-rw-r--r-- | plugins/CustomDimensions/Dimension/Extraction.php | 27 | ||||
-rw-r--r-- | plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php | 14 |
2 files changed, 32 insertions, 9 deletions
diff --git a/plugins/CustomDimensions/Dimension/Extraction.php b/plugins/CustomDimensions/Dimension/Extraction.php index 040992fd1a..48b574a98f 100644 --- a/plugins/CustomDimensions/Dimension/Extraction.php +++ b/plugins/CustomDimensions/Dimension/Extraction.php @@ -16,6 +16,7 @@ use Piwik\Url; use Piwik\UrlHelper; use Piwik\Piwik; use Exception; +use Piwik\Validators\Regex; class Extraction { @@ -50,6 +51,10 @@ class Extraction throw new Exception("You need to group exactly one part of the regular expression inside round brackets, eg 'index_(.+).html'"); } } + + //validate regex pattern + $validator = new Regex(); + $validator->validate($this->formatPattern()); } public static function getSupportedDimensions() @@ -104,6 +109,20 @@ class Extraction return null; } + $regex = $this->formatPattern(); + + if (preg_match($regex, (string) $value, $matches)) { + // we could improve performance here I reckon by combining all patterns of all configs see eg http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html + + if (array_key_exists(1, $matches)) { + return $matches[1]; + } + } + } + + // format pattern to matomo format + private function formatPattern () { + $pattern = $this->pattern; if ($this->dimension === 'urlparam') { $pattern = '\?.*' . $pattern . '=([^&]*)'; @@ -114,12 +133,6 @@ class Extraction $regex .= 'i'; } - if (preg_match($regex, (string) $value, $matches)) { - // we could improve performance here I reckon by combining all patterns of all configs see eg http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html - - if (array_key_exists(1, $matches)) { - return $matches[1]; - } - } + return $regex; } }
\ No newline at end of file diff --git a/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php b/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php index eb9f73a75f..aea34880dd 100644 --- a/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php +++ b/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\CustomDimensions\tests\Integration\Dimension; +use Piwik\Piwik; use Piwik\Plugins\CustomDimensions\Dimension\Extraction; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; @@ -185,6 +186,14 @@ class ExtractionTest extends IntegrationTestCase $this->buildExtraction('anyInvalid', '/ref(.+)')->check(); } + public function test_check_shouldFailWhenInvalidRegGiven() + { + $check = '/foo(*)/'; + $this->expectException(\Exception::class); + $this->expectExceptionMessage(Piwik::translate('General_ValidatorErrorNoValidRegex', array($check))); + $this->buildExtraction('url', $check)->check(); + } + /** * @dataProvider getInvalidPatterns */ @@ -199,8 +208,9 @@ class ExtractionTest extends IntegrationTestCase public function test_check_shouldNotFailWhenValidCombinationsAreGiven() { $this->expectNotToPerformAssertions(); - $this->buildExtraction('url', 'index_(+).html')->check(); - $this->buildExtraction('action_name', 'index_(+).html')->check(); + $this->buildExtraction('urlparam', 'index')->check(); // does not have to contain brackets + $this->buildExtraction('url', 'index_(.+).html')->check(); + $this->buildExtraction('action_name', 'index_(.+).html')->check(); $this->buildExtraction('url', '')->check(); // empty value is allowed $this->buildExtraction('urlparam', 'index')->check(); // does not have to contain brackets } |