diff options
author | Vincent Petry <vincent@nextcloud.com> | 2022-09-15 14:07:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-15 14:07:40 +0200 |
commit | ec75b7c571cae8a605874438f87b8a5aaa162ecc (patch) | |
tree | 505cda7feb1b3c6aa0c4a851b76c4206a89aa0e1 /apps/dav/lib | |
parent | 54ea4830e3461cf5e3cbd3f814207f9239879b56 (diff) | |
parent | 3236a8e2af9383edc258b7fe31cb9291fac4df26 (diff) | |
download | nextcloud-server-ec75b7c571cae8a605874438f87b8a5aaa162ecc.tar.gz nextcloud-server-ec75b7c571cae8a605874438f87b8a5aaa162ecc.zip |
Merge pull request #33964 from nextcloud/search-limit-operators
add a limit to the amount of operators a client can add to a search query
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/Files/FileSearchBackend.php | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/apps/dav/lib/Files/FileSearchBackend.php b/apps/dav/lib/Files/FileSearchBackend.php index 7ee82779849..c819fa6afc6 100644 --- a/apps/dav/lib/Files/FileSearchBackend.php +++ b/apps/dav/lib/Files/FileSearchBackend.php @@ -55,6 +55,8 @@ use SearchDAV\Query\Order; use SearchDAV\Query\Query; class FileSearchBackend implements ISearchBackend { + const OPERATOR_LIMIT = 100; + /** @var CachingTree */ private $tree; @@ -315,6 +317,11 @@ class FileSearchBackend implements ISearchBackend { } } + $operatorCount = $this->countSearchOperators($query->where); + if ($operatorCount > self::OPERATOR_LIMIT) { + throw new \InvalidArgumentException('Invalid search query, maximum operator limit of ' . self::OPERATOR_LIMIT . ' exceeded, got ' . $operatorCount . ' operators'); + } + return new SearchQuery( $this->transformSearchOperation($query->where), (int)$limit->maxResults, @@ -325,6 +332,26 @@ class FileSearchBackend implements ISearchBackend { ); } + private function countSearchOperators(Operator $operator): int { + switch ($operator->type) { + case Operator::OPERATION_AND: + case Operator::OPERATION_OR: + case Operator::OPERATION_NOT: + /** @var Operator[] $arguments */ + $arguments = $operator->arguments; + return array_sum(array_map([$this, 'countSearchOperators'], $arguments)); + case Operator::OPERATION_EQUAL: + case Operator::OPERATION_GREATER_OR_EQUAL_THAN: + case Operator::OPERATION_GREATER_THAN: + case Operator::OPERATION_LESS_OR_EQUAL_THAN: + case Operator::OPERATION_LESS_THAN: + case Operator::OPERATION_IS_LIKE: + case Operator::OPERATION_IS_COLLECTION: + default: + return 1; + } + } + /** * @param Order $order * @return ISearchOrder |