Skip to content

Commit

Permalink
Merge pull request #36099 from nextcloud/backport/34924/stable25
Browse files Browse the repository at this point in the history
[stable25] escape path prefix when doing cache jail search
  • Loading branch information
PVince81 authored Jan 19, 2023
2 parents 4f6f5de + 5c743ac commit 0fedfd6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/Wrapper/CacheJail.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected function addJailFilterQuery(ISearchOperator $filter): ISearchOperator
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR,
[
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
new SearchComparison(ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE, 'path', $this->getGetUnjailedRoot() . '/%'),
new SearchComparison(ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE, 'path', SearchComparison::escapeLikeParameter($this->getGetUnjailedRoot()) . '/%'),
],
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@

namespace OC\Files\Search\QueryOptimizer;

use OC\Files\Search\SearchComparison;
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;

class PathPrefixOptimizer extends QueryOptimizerStep {
public function escapeLikeParameter(string $param): string {
return addcslashes($param, '\\_%');
}

public function processOperator(ISearchOperator &$operator) {
// normally the `path = "$prefix"` search query part of the prefix filter would be generated as an `path_hash = md5($prefix)` sql query
// since the `path_hash` sql column usually provides much faster querying that selecting on the `path` sql column
Expand All @@ -43,11 +40,11 @@ public function processOperator(ISearchOperator &$operator) {
$b = $operator->getArguments()[1];
if ($a instanceof ISearchComparison && $b instanceof ISearchComparison && $a->getField() === 'path' && $b->getField() === 'path') {
if ($a->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $b->getType() === ISearchComparison::COMPARE_EQUAL
&& $a->getValue() === $this->escapeLikeParameter($b->getValue()) . '/%') {
&& $a->getValue() === SearchComparison::escapeLikeParameter($b->getValue()) . '/%') {
$b->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false);
}
if ($b->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $a->getType() === ISearchComparison::COMPARE_EQUAL
&& $b->getValue() === $this->escapeLikeParameter($a->getValue()) . '/%') {
&& $b->getValue() === SearchComparison::escapeLikeParameter($a->getValue()) . '/%') {
$a->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Files/Search/SearchComparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public function getQueryHint(string $name, $default) {
public function setQueryHint(string $name, $value): void {
$this->hints[$name] = $value;
}

public static function escapeLikeParameter(string $param): string {
return addcslashes($param, '\\_%');
}
}

0 comments on commit 0fedfd6

Please sign in to comment.