Skip to content

Commit 51f8b9c

Browse files
authored
Merge pull request #35097 from nextcloud/bugfix/noid/improve-email-results
Improve email results for sharing
2 parents 8295c7b + 269df90 commit 51f8b9c

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

lib/private/Collaboration/Collaborators/MailPlugin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
101101
return false;
102102
}
103103

104+
// Extract the email address from "Foo Bar <foo.bar@example.tld>" and then search with "foo.bar@example.tld" instead
105+
$result = preg_match('/<([^@]+@.+)>$/', $search, $matches);
106+
if ($result && filter_var($matches[1], FILTER_VALIDATE_EMAIL)) {
107+
return $this->search($matches[1], $limit, $offset, $searchResult);
108+
}
109+
104110
$currentUserId = $this->userSession->getUser()->getUID();
105111

106112
$result = $userResults = ['wide' => [], 'exact' => []];

lib/private/Federation/CloudIdManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public function resolveCloudId(string $cloudId): ICloudId {
125125
if ($lastValidAtPos !== false) {
126126
$user = substr($id, 0, $lastValidAtPos);
127127
$remote = substr($id, $lastValidAtPos + 1);
128+
129+
$this->userManager->validateUserId($user);
130+
128131
if (!empty($user) && !empty($remote)) {
129132
return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
130133
}

lib/private/User/Manager.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
use OCP\IUser;
4545
use OCP\IUserBackend;
4646
use OCP\IUserManager;
47+
use OCP\L10N\IFactory;
48+
use OCP\Server;
4749
use OCP\Support\Subscription\IAssertion;
4850
use OCP\User\Backend\IGetRealUIDBackend;
4951
use OCP\User\Backend\ISearchKnownUsersBackend;
@@ -427,31 +429,7 @@ public function createUser($uid, $password) {
427429
public function createUserFromBackend($uid, $password, UserInterface $backend) {
428430
$l = \OC::$server->getL10N('lib');
429431

430-
// Check the name for bad characters
431-
// Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
432-
if (preg_match('/[^a-zA-Z0-9 _.@\-\']/', $uid)) {
433-
throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:'
434-
. ' "a-z", "A-Z", "0-9", and "_.@-\'"'));
435-
}
436-
437-
// No empty username
438-
if (trim($uid) === '') {
439-
throw new \InvalidArgumentException($l->t('A valid username must be provided'));
440-
}
441-
442-
// No whitespace at the beginning or at the end
443-
if (trim($uid) !== $uid) {
444-
throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end'));
445-
}
446-
447-
// Username only consists of 1 or 2 dots (directory traversal)
448-
if ($uid === '.' || $uid === '..') {
449-
throw new \InvalidArgumentException($l->t('Username must not consist of dots only'));
450-
}
451-
452-
if (!$this->verifyUid($uid)) {
453-
throw new \InvalidArgumentException($l->t('Username is invalid because files already exist for this user'));
454-
}
432+
$this->validateUserId($uid, true);
455433

456434
// No empty password
457435
if (trim($password) === '') {
@@ -726,7 +704,43 @@ public function getByEmail($email) {
726704
}));
727705
}
728706

729-
private function verifyUid(string $uid): bool {
707+
/**
708+
* @param string $uid
709+
* @param bool $checkDataDirectory
710+
* @throws \InvalidArgumentException Message is an already translated string with a reason why the id is not valid
711+
* @since 26.0.0
712+
*/
713+
public function validateUserId(string $uid, bool $checkDataDirectory = false): void {
714+
$l = Server::get(IFactory::class)->get('lib');
715+
716+
// Check the name for bad characters
717+
// Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
718+
if (preg_match('/[^a-zA-Z0-9 _.@\-\']/', $uid)) {
719+
throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:'
720+
. ' "a-z", "A-Z", "0-9", and "_.@-\'"'));
721+
}
722+
723+
// No empty username
724+
if (trim($uid) === '') {
725+
throw new \InvalidArgumentException($l->t('A valid username must be provided'));
726+
}
727+
728+
// No whitespace at the beginning or at the end
729+
if (trim($uid) !== $uid) {
730+
throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end'));
731+
}
732+
733+
// Username only consists of 1 or 2 dots (directory traversal)
734+
if ($uid === '.' || $uid === '..') {
735+
throw new \InvalidArgumentException($l->t('Username must not consist of dots only'));
736+
}
737+
738+
if (!$this->verifyUid($uid, $checkDataDirectory)) {
739+
throw new \InvalidArgumentException($l->t('Username is invalid because files already exist for this user'));
740+
}
741+
}
742+
743+
private function verifyUid(string $uid, bool $checkDataDirectory = false): bool {
730744
$appdata = 'appdata_' . $this->config->getSystemValueString('instanceid');
731745

732746
if (\in_array($uid, [
@@ -740,6 +754,10 @@ private function verifyUid(string $uid): bool {
740754
return false;
741755
}
742756

757+
if (!$checkDataDirectory) {
758+
return true;
759+
}
760+
743761
$dataDirectory = $this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data');
744762

745763
return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid);

lib/public/IUserManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,12 @@ public function callForSeenUsers(\Closure $callback);
212212
* @since 9.1.0
213213
*/
214214
public function getByEmail($email);
215+
216+
/**
217+
* @param string $uid The user ID to validate
218+
* @param bool $checkDataDirectory Whether it should be checked if files for the ID exist inside the data directory
219+
* @throws \InvalidArgumentException Message is an already translated string with a reason why the ID is not valid
220+
* @since 26.0.0
221+
*/
222+
public function validateUserId(string $uid, bool $checkDataDirectory = false): void;
215223
}

0 commit comments

Comments
 (0)