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 19, 2021
1 parent ceef59b commit 052644e
Show file tree
Hide file tree
Showing 3 changed files with 29 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
14 changes: 12 additions & 2 deletions tests/lib/Files/Cache/Wrapper/CacheJailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CacheJailTest extends CacheTest {
protected function setUp(): void {
parent::setUp();
$this->storage->mkdir('foo');
$this->storage->getScanner()->scan('');
$this->sourceCache = $this->cache;
$this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo');
}
Expand All @@ -49,6 +50,11 @@ 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);
$this->assertEquals('', $result[0]['path']);
$this->assertEquals('foobar', $result[1]['path']);
}

public function testSearchMimeOutsideJail() {
Expand Down Expand Up @@ -76,11 +82,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 052644e

Please sign in to comment.