-
Notifications
You must be signed in to change notification settings - Fork 28
feat: Hash user IDs by default for improved privacy #1335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? 8000 Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ js | |
.vscode | ||
.php_cs.cache | ||
.php-cs-fixer.cache | ||
.phpunit.result.cache |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Guests\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\IDBConnection; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
/** | ||
* Add a column for the email so we know the originally invited email as well | ||
*/ | ||
class Version4002Date20250501195008 extends SimpleMigrationStep { | ||
public function __construct( | ||
protected IDBConnection $db, | ||
) { | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$table = $schema->getTable('guests_users'); | ||
if ($table->hasColumn('email')) { | ||
return null; | ||
} | ||
|
||
$table->addColumn('email', 'string', [ | ||
'notnull' => false, | ||
'length' => 64, | ||
'default' => '', | ||
]); | ||
return $schema; | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
*/ | ||
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
$query = $this->db->getQueryBuilder(); | ||
$query->update('guests_users') | ||
->set('email', 'uid_lower'); | ||
$query->executeStatement(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,14 @@ public function deleteUser($uid): bool { | |
return $result ? true : false; | ||
} | ||
|
||
public function setInitialEmail(string $uid, string $email): bool { | ||
$query = $this->dbConn->getQueryBuilder(); | ||
$query->update('guests_users') | ||
->set('email', $query->createNamedParameter($email)) | ||
->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); | ||
return (bool)$query->executeStatement(); | ||
} | ||
|
||
/** | ||
* Set password | ||
* | ||
|
@@ -248,6 +256,35 @@ public function getDisplayNames($search = '', $limit = null, $offset = null): ar | |
} | ||
} | ||
|
||
/** | ||
* Get a list of all users with their email and display name | ||
* | ||
* @return array<string, array{email: string, displayname: string}> | ||
*/ | ||
public function getAllGuestAccounts(?int $limit = null, ?int $offset = null): array { | ||
if (!$this->allowListing) { | ||
return []; | ||
} | ||
|
||
$query = $this->dbConn->getQueryBuilder(); | ||
$query->select('uid', 'email', 'displayname') | ||
->from('guests_users') | ||
->orderBy('uid_lower', 'ASC') | ||
->setMaxResults($limit) | ||
->setFirstResult($offset); | ||
|
||
$result = $query->execute(); | ||
$users = []; | ||
while ($row = $result->fetch()) { | ||
$users[(string)$row['uid']] = [ | ||
'email' => (string)$row['email'], | ||
'displayname' => (string)$row['displayname'], | ||
]; | ||
Comment on lines
+279
to
+282
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a generator may make sense here: Do all guests fit in memory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getDisplayNames() above is basically doing the same thing. Just had to do an array as value so we can have email + displayname |
||
} | ||
|
||
return $users; | ||
} | ||
|
||
/** | ||
* Check if the password is correct | ||
* | ||
|
@@ -257,7 +294,7 @@ public function getDisplayNames($search = '', $limit = null, $offset = null): ar | |
* returns the user id or false | ||
*/ | ||
public function checkPassword(string $loginName, string $password) { | ||
if (strpos($loginName, '@') === false) { | ||
if (!$this->potentialGuestUserId($loginName)) { | ||
return false; | ||
} | ||
|
||
|
@@ -294,9 +331,13 @@ public function checkPassword(string $loginName, string $password) { | |
* @return bool true if user was found, false otherwise | ||
*/ | ||
private function loadUser($uid): bool { | ||
if (isset($this->cache[$uid]) && $this->cache[$uid] === false) { | ||
return false; | ||
} | ||
|
||
// guests $uid could be NULL or '' | ||
// or is not an email anyway | ||
if (strpos($uid, '@') === false) { | ||
if (!$this->potentialGuestUserId($uid)) { | ||
$this->cache[$uid] = false; | ||
return false; | ||
} | ||
|
@@ -416,7 +457,7 @@ public function getBackendName(): string { | |
} | ||
|
||
public function getRealUID(string $uid): string { | ||
if (strpos($uid, '@') === false) { | ||
if (!$this->potentialGuestUserId($uid)) { | ||
throw new \RuntimeException($uid . ' does not exist'); | ||
} | ||
|
||
|
@@ -430,4 +471,16 @@ public function getRealUID(string $uid): string { | |
|
||
return $this->cache[$uid]['uid']; | ||
} | ||
|
||
/** | ||
* Guest app user ids are: | ||
* - either email addresses so they need to contain an @ | ||
* - lowercase sha256 hashes of email addresses, 64 characters of a-f and 0-9 | ||
* | ||
* @param string $userId | ||
* @return bool | ||
*/ | ||
protected function potentialGuestUserId(string $userId): bool { | ||
return str_contains($userId, '@') || preg_match('/^[a-f0-9]{64}$/', $userId); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that be mb_strtolower like in UserBackend?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mb is not allowed in user ids nor in emails, so not sure it's required?