Skip to content

Commit

Permalink
fix cachjail searching for root
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Jan 25, 2021
1 parent 7bc9f4c commit 4044fe7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/private/Files/Cache/QuerySearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ private function getOperatorFieldAndValue(ISearchComparison $operator) {
$field = 'tag.category';
} elseif ($field === 'fileid') {
$field = 'file.fileid';
} elseif ($field === 'path' && $type === ISearchComparison::COMPARE_EQUAL) {
$field = 'path_hash';
$value = md5((string)$value);
}
return [$field, $value, $type];
}
Expand Down
17 changes: 14 additions & 3 deletions lib/private/Files/Cache/Wrapper/CacheJail.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ public function search($pattern) {
$query = $this->getQueryBuilder();
$query->selectFileCache()
->whereStorageId()
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')))
->andWhere($query->expr()->orX(
$query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')),
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getRoot()))),
))
->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern)));

$result = $query->execute();
Expand All @@ -263,7 +266,10 @@ public function searchByMime($mimetype) {
$query = $this->getQueryBuilder();
$query->selectFileCache()
->whereStorageId()
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')));
->andWhere($query->expr()->orX(
$query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')),
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getRoot()))),
));

if (strpos($mimetype, '/')) {
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
Expand All @@ -287,9 +293,14 @@ public function searchQuery(ISearchQuery $query) {
'path',
$this->getRoot() . '/%'
);
$rootFilter = new SearchComparison(
ISearchComparison::COMPARE_EQUAL,
'path',
$this->getRoot()
);
$operation = new SearchBinaryOperator(
ISearchBinaryOperator::OPERATOR_AND,
[$prefixFilter, $query->getSearchOperation()]
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [$prefixFilter, $rootFilter]) , $query->getSearchOperation()]
);
$simpleQuery = new SearchQuery($operation, 0, 0, $query->getOrder(), $query->getUser());
$results = $this->getCache()->searchQuery($simpleQuery);
Expand Down
19 changes: 17 additions & 2 deletions tests/lib/Files/Cache/Wrapper/CacheJailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function setUp(): void {
}

public function testSearchOutsideJail() {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
Expand All @@ -49,9 +50,18 @@ public function testSearchOutsideJail() {
$result = $this->cache->search('%foobar%');
$this->assertCount(1, $result);
$this->assertEquals('foobar', $result[0]['path']);

$result = $this->cache->search('%foo%');
$this->assertCount(2, $result);
usort($result, function ($a, $b) {
return $a['path'] <=> $b['path'];
});
$this->assertEquals('', $result[0]['path']);
$this->assertEquals('foobar', $result[1]['path']);
}

public function testSearchMimeOutsideJail() {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
Expand All @@ -67,6 +77,7 @@ public function testSearchMimeOutsideJail() {
}

public function testSearchQueryOutsideJail() {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
Expand All @@ -76,11 +87,15 @@ public function testSearchQueryOutsideJail() {

$user = new User('foo', null, $this->createMock(EventDispatcherInterface::class));
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), 10, 0, [], $user);
$this->assertCount(2, $this->sourceCache->searchQuery($query));
$result = $this->cache->searchQuery($query);

$result = $this->cache->search('%foobar%');
$this->assertCount(1, $result);
$this->assertEquals('foobar', $result[0]['path']);

$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user);
$result = $this->cache->searchQuery($query);
$this->assertCount(1, $result);
$this->assertEquals('', $result[0]['path']);
}

public function testClearKeepEntriesOutsideJail() {
Expand Down

0 comments on commit 4044fe7

Please sign in to comment.