Skip to content

Commit b068148

Browse files
Merge pull request #60 from Flowpack/task/change-sanitation
TASK: Use more fine-grained sanitation
2 parents 02630e2 + 0902359 commit b068148

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

Classes/Controller/SuggestController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
1515
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
1616
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
17+
use Flowpack\SearchPlugin\Utility\Sanitation;
1718
use Neos\Cache\Frontend\VariableFrontend;
1819
use Neos\Flow\Annotations as Flow;
1920
use Neos\Flow\Mvc\Controller\ActionController;
@@ -114,8 +115,8 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi
114115
$term = strtolower($term);
115116

116117
// The suggest function only works well with one word
117-
// and the term is trimmed to alnum characters to avoid errors
118-
$suggestTerm = preg_replace('/[[:^alnum:]]/', '', explode(' ', $term)[0]);
118+
// special search characters are escaped
119+
$suggestTerm = Sanitation::sanitizeSearchInput(explode(' ', $term)[0]);
119120

120121
if (!$this->elasticSearchQueryTemplateCache->has($cacheKey)) {
121122
$contentContext = $this->createContentContext('live', $dimensionCombination ? json_decode($dimensionCombination, true) : []);

Classes/EelHelper/SuggestionIndexHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
use Flowpack\SearchPlugin\Exception;
17+
use Flowpack\SearchPlugin\Utility\Sanitation;
1718
use Neos\Eel\ProtectedContextAwareInterface;
1819
use Neos\Flow\Annotations as Flow;
1920

@@ -47,8 +48,9 @@ protected function prepareInput($input): ?array
4748
{
4849
$process = static function (?string $input) {
4950
$input = preg_replace("/\r|\n/", '', $input);
50-
return array_values(array_filter(explode(' ', preg_replace("/[^[:alnum:][:space:]]/u", ' ', strip_tags($input)))));
51+
return array_values(array_filter(explode(' ', Sanitation::sanitizeSearchInput(strip_tags($input)))));
5152
};
53+
5254
if (\is_string($input)) {
5355
return $process($input);
5456
} elseif (\is_array($input)) {

Classes/Utility/Sanitation.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\SearchPlugin\Utility;
5+
6+
/*
7+
* This file is part of the Flowpack.SearchPlugin package.
8+
*
9+
* (c) Contributors of the Flowpack Team - flowpack.org
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
16+
class Sanitation
17+
{
18+
19+
public static function sanitizeSearchInput(string $input): string
20+
{
21+
return str_replace(['=', '>', '<', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\', '/'], ['', '', '', '(', '\)', '\{', '\}', '[', '\]', '\^', '\"', '\~', '\*', '\?', '\:', '\\\\', '\/'], $input);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)