Skip to content

Commit 66e464a

Browse files
authored
Merge pull request #30245 from nextcloud/backport/30156/stable23
[stable23] Only wildcard search if enumeration is allowed
2 parents 86c0b22 + dd2c1c6 commit 66e464a

File tree

16 files changed

+114
-22
lines changed

16 files changed

+114
-22
lines changed

apps/dav/lib/CardDAV/AddressBookImpl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public function getDisplayName() {
107107
* - 'escape_like_param' - If set to false wildcards _ and % are not escaped
108108
* - 'limit' - Set a numeric limit for the search results
109109
* - 'offset' - Set the offset for the limited search results
110+
* - 'wildcard' - Whether the search should use wildcards
111+
* @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options
110112
* @return array an array of contacts which are arrays of key-value-pairs
111113
* example result:
112114
* [

apps/dav/lib/CardDAV/CardDavBackend.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,8 @@ public function updateShares(IShareable $shareable, $add, $remove) {
10241024
* - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are
10251025
* - 'limit' - Set a numeric limit for the search results
10261026
* - 'offset' - Set the offset for the limited search results
1027+
* - 'wildcard' - Whether the search should use wildcards
1028+
* @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options
10271029
* @return array an array of contacts which are arrays of key-value-pairs
10281030
*/
10291031
public function search($addressBookId, $pattern, $searchProperties, $options = []): array {
@@ -1055,13 +1057,15 @@ public function searchPrincipalUri(string $principalUri,
10551057
* @param string $pattern
10561058
* @param array $searchProperties
10571059
* @param array $options
1060+
* @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options
10581061
* @return array
10591062
*/
10601063
private function searchByAddressBookIds(array $addressBookIds,
10611064
string $pattern,
10621065
array $searchProperties,
10631066
array $options = []): array {
10641067
$escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false;
1068+
$useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false;
10651069

10661070
$query2 = $this->db->getQueryBuilder();
10671071

@@ -1103,7 +1107,9 @@ private function searchByAddressBookIds(array $addressBookIds,
11031107

11041108
// No need for like when the pattern is empty
11051109
if ('' !== $pattern) {
1106-
if (!$escapePattern) {
1110+
if (!$useWildcards) {
1111+
$query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern)));
1112+
} elseif (!$escapePattern) {
11071113
$query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern)));
11081114
} else {
11091115
$query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')));

apps/federatedfilesharing/lib/Notifier.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ protected function getDisplayNameFromContact($federatedCloudId) {
255255
}
256256
}
257257

258-
$addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
258+
$addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD'], [
259+
'limit' => 1,
260+
'enumeration' => false,
261+
'fullmatch' => false,
262+
'strict_search' => true,
263+
]);
259264
foreach ($addressBookEntries as $entry) {
260265
if (isset($entry['CLOUD'])) {
261266
foreach ($entry['CLOUD'] as $cloudID) {

apps/files/lib/Activity/Provider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,12 @@ protected function getDisplayNameFromAddressBook(string $search): string {
560560
return $this->displayNames[$search];
561561
}
562562

563-
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD']);
563+
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
564+
'limit' => 1,
565+
'enumeration' => false,
566+
'fullmatch' => false,
567+
'strict_search' => true,
568+
]);
564569
foreach ($addressBookContacts as $contact) {
565570
if (isset($contact['isLocalSystemBook'])) {
566571
continue;

apps/files_sharing/lib/Activity/Providers/Base.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ protected function getDisplayNameFromAddressBook(string $search): string {
203203
return $this->displayNames[$search];
204204
}
205205

206-
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD']);
206+
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
207+
'limit' => 1,
208+
'enumeration' => false,
209+
'fullmatch' => false,
210+
'strict_search' => true,
211+
]);
207212
foreach ($addressBookContacts as $contact) {
208213
if (isset($contact['isLocalSystemBook'])) {
209214
continue;

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,12 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
334334
* @return string
335335
*/
336336
private function getDisplayNameFromAddressBook(string $query, string $property): string {
337-
// FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
338-
$result = \OC::$server->getContactsManager()->search($query, [$property]);
337+
// FIXME: If we inject the contacts manager it gets initialized before any address books are registered
338+
$result = \OC::$server->getContactsManager()->search($query, [$property], [
339+
'limit' => 1,
340+
'enumeration' => false,
341+
'strict_search' => true,
342+
]);
339343
foreach ($result as $r) {
340344
foreach ($r[$property] as $value) {
341345
if ($value === $query && $r['FN']) {

apps/files_sharing/tests/Controller/ShareAPIControllerTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,7 +4417,11 @@ public function testFormatShare(array $expects, \OCP\Share\IShare $share, array
44174417

44184418
$cm->method('search')
44194419
->willReturnMap([
4420-
['user@server.com', ['CLOUD'], [],
4420+
['user@server.com', ['CLOUD'], [
4421+
'limit' => 1,
4422+
'enumeration' => false,
4423+
'strict_search' => true,
4424+
],
44214425
[
44224426
[
44234427
'CLOUD' => [
@@ -4427,7 +4431,11 @@ public function testFormatShare(array $expects, \OCP\Share\IShare $share, array
44274431
],
44284432
],
44294433
],
4430-
['user@server.com', ['EMAIL'], [],
4434+
['user@server.com', ['EMAIL'], [
4435+
'limit' => 1,
4436+
'enumeration' => false,
4437+
'strict_search' => true,
4438+
],
44314439
[
44324440
[
44334441
'EMAIL' => [

apps/sharebymail/lib/Activity.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ protected function generateUserParameter($uid) {
362362
* @return string
363363
*/
364364
protected function getContactName($email) {
365-
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL']);
365+
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL'], [
366+
'limit' => 1,
367+
'enumeration' => false,
368+
'fullmatch' => false,
369+
'strict_search' => true,
370+
]);
366371

367372
foreach ($addressBookContacts as $contact) {
368373
if (isset($contact['isLocalSystemBook'])) {

lib/private/Collaboration/Collaborators/MailPlugin.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ public function __construct(IManager $contactsManager,
8686
}
8787

8888
/**
89-
* @param $search
90-
* @param $limit
91-
* @param $offset
92-
* @param ISearchResult $searchResult
93-
* @return bool
94-
* @since 13.0.0
89+
* {@inheritdoc}
9590
*/
9691
public function search($search, $limit, $offset, ISearchResult $searchResult) {
9792
$currentUserId = $this->userSession->getUser()->getUID();
@@ -101,7 +96,16 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
10196
$emailType = new SearchResultType('emails');
10297

10398
// Search in contacts
104-
$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN'], ['limit' => $limit, 'offset' => $offset]);
99+
$addressBookContacts = $this->contactsManager->search(
100+
$search,
101+
['EMAIL', 'FN'],
102+
[
103+
'limit' => $limit,
104+
'offset' => $offset,
105+
'enumeration' => (bool) $this->shareeEnumeration,
106+
'fullmatch' => (bool) $this->shareeEnumerationFullMatch,
107+
]
108+
);
105109
$lowerSearch = strtolower($search);
106110
foreach ($addressBookContacts as $contact) {
107111
if (isset($contact['EMAIL'])) {

lib/private/Collaboration/Collaborators/RemotePlugin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
6767
$resultType = new SearchResultType('remotes');
6868

6969
// Search in contacts
70-
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN'], ['limit' => $limit, 'offset' => $offset]);
70+
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN'], [
71+
'limit' => $limit,
72+
'offset' => $offset,
73+
'enumeration' => false,
74+
'fullmatch' => false,
75+
]);
7176
foreach ($addressBookContacts as $contact) {
7277
if (isset($contact['isLocalSystemBook'])) {
7378
continue;

0 commit comments

Comments
 (0)