From 6e6aabedf20575cbc9a17b08e8c85aa87c47b6fe Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 8 Apr 2015 09:31:11 +0200 Subject: [PATCH 1/3] Don't trim filter expressions Leading and trailing whitespaces may be significant for comparison. refs #8777 --- library/Icinga/Data/Filter/FilterExpression.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Icinga/Data/Filter/FilterExpression.php b/library/Icinga/Data/Filter/FilterExpression.php index 5b580fa38..8ef1e5076 100644 --- a/library/Icinga/Data/Filter/FilterExpression.php +++ b/library/Icinga/Data/Filter/FilterExpression.php @@ -12,7 +12,6 @@ class FilterExpression extends Filter public function __construct($column, $sign, $expression) { $column = trim($column); - $expression = is_array($expression) ? array_map('trim', $expression) : trim($expression); $this->column = $column; $this->sign = $sign; $this->expression = $expression; From a18510792738440b589e6f9f9b2c97d2001e1448 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 8 Apr 2015 10:14:42 +0200 Subject: [PATCH 2/3] Trim off leading whitespaces from filter values when searching Because whitespaces may be used when searching for entities using the query string format "column = value" we have to trim off leading whitespaces from filter values. This loses the possibility for search for entities with leading whitespaces of course. refs #8777 --- library/Icinga/Web/Widget/FilterEditor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Web/Widget/FilterEditor.php b/library/Icinga/Web/Widget/FilterEditor.php index 2575ce264..735fd14d3 100644 --- a/library/Icinga/Web/Widget/FilterEditor.php +++ b/library/Icinga/Web/Widget/FilterEditor.php @@ -201,13 +201,13 @@ class FilterEditor extends AbstractWidget if ($searchCol === null) { throw new Exception('Cannot search here'); } + $search = ltrim($search); $filter = $this->mergeRootExpression($filter, $searchCol, '=', "*$search*"); } else { list($k, $v) = preg_split('/=/', $search); - $filter = $this->mergeRootExpression($filter, $k, '=', $v); + $filter = $this->mergeRootExpression($filter, trim($k), '=', ltrim($v)); } - $url = $this->url()->setQueryString( $filter->toQueryString() )->addParams($preserve); From 09acddb3b7c8d4adc647e79b16cf294adbf07725 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 8 Apr 2015 10:18:42 +0200 Subject: [PATCH 3/3] Fix filter tests not taking whitespaces into account refs #8777 --- .../library/Icinga/Data/Filter/FilterTest.php | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/test/php/library/Icinga/Data/Filter/FilterTest.php b/test/php/library/Icinga/Data/Filter/FilterTest.php index e14decb20..8080d47f7 100644 --- a/test/php/library/Icinga/Data/Filter/FilterTest.php +++ b/test/php/library/Icinga/Data/Filter/FilterTest.php @@ -198,16 +198,53 @@ class FilterTest extends BaseTestCase $this->assertNotEquals((string) $c, (string) $d); } - public function testLeadingAndTrailingWhitespacesSanitizing() + public function testLeadingAndTrailingWhitespaces() { - $columnHasWhitespaces = Filter::where(' host ', 'localhost'); - $expressionHasWhitespaces = Filter::where('host', ' localhost '); - $bothHaveWhitespaces = Filter::fromQueryString(' host = localhost '); - $withArray = Filter::where(' host ', array(' no match ', ' localhost ')); - $this->assertTrue($columnHasWhitespaces->matches($this->sampleData[0])); - $this->assertTrue($expressionHasWhitespaces->matches($this->sampleData[0])); - $this->assertTrue($bothHaveWhitespaces->matches($this->sampleData[0])); - $this->assertTrue($withArray->matches($this->sampleData[0])); + $columnWithWhitespaces = Filter::where(' host ', 'localhost'); + $this->assertTrue($columnWithWhitespaces->matches((object) array( + 'host' => 'localhost' + )), + 'Filter doesn\'t remove leading and trailing whitespaces from columns' + ); + $expressionWithLeadingWhitespaces = Filter::where('host', ' localhost'); + $this->assertTrue($expressionWithLeadingWhitespaces->matches((object) array( + 'host' => ' localhost' + )), + 'Filter doesn\'t take leading whitespaces of expressions into account' + ); + $this->assertFalse($expressionWithLeadingWhitespaces->matches((object) array( + 'host' => ' localhost ' + )), + 'Filter doesn\'t take trailing whitespaces of expressions into account' + ); + $expressionWithTrailingWhitespaces = Filter::where('host', 'localhost '); + $this->assertTrue($expressionWithTrailingWhitespaces->matches((object) array( + 'host' => 'localhost ' + )), + 'Filter doesn\'t take trailing whitespaces of expressions into account' + ); + $this->assertFalse($expressionWithTrailingWhitespaces->matches((object) array( + 'host' => ' localhost ' + )), + 'Filter doesn\'t take leading whitespaces of expressions into account' + ); + $expressionWithLeadingAndTrailingWhitespaces = Filter::where('host', ' localhost '); + $this->assertTrue($expressionWithLeadingAndTrailingWhitespaces->matches((object) array( + 'host' => ' localhost ' + )), + 'Filter doesn\'t take leading and trailing whitespaces of expressions into account' + ); + $this->assertFalse($expressionWithLeadingAndTrailingWhitespaces->matches((object) array( + 'host' => ' localhost ' + )), + 'Filter doesn\'t take leading and trailing whitespaces of expressions into account' + ); + $queryStringWithWhitespaces = Filter::fromQueryString(' host = localhost '); + $this->assertTrue($queryStringWithWhitespaces->matches((object) array( + 'host' => ' localhost ' + )), + 'Filter doesn\'t take leading and trailing whitespaces of expressions in query strings into account' + ); } private function row($idx)