-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have cases in which app developers forget to delete user data when a user is being deleted. This could be problematic as future users with the same username could then access this data. This mitigates this risk a bit by not allowing reusing of usernames. That said, we still need to fix occurrences of these issues by deleting missed data. Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
- Loading branch information
1 parent
903b99b
commit 2d2274d
Showing
10 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OC\Core\Migrations; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version22000Date20210525173326 extends SimpleMigrationStep { | ||
/** | ||
* @param IOutput $output | ||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$table = $schema->createTable('previously_used_userids'); | ||
|
||
$table->addColumn('user_id_hash', \OCP\DB\Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 128, | ||
]); | ||
|
||
$table->setPrimaryKey(['user_id_hash'], 'uid_hash_idx'); | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2021 Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\User; | ||
|
||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\User\Events\UserDeletedEvent; | ||
|
||
class UserDeletedListener implements IEventListener { | ||
/** @var UsernameDuplicationPreventionManager */ | ||
private $usernameDuplicationPreventionManager; | ||
|
||
public function __construct(UsernameDuplicationPreventionManager $usernameDuplicationPreventionManager) { | ||
$this->usernameDuplicationPreventionManager = $usernameDuplicationPreventionManager; | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof UserDeletedEvent)) { | ||
return; | ||
} | ||
|
||
$user = $event->getUser(); | ||
$this->usernameDuplicationPreventionManager->markUsed($user->getUID()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @author Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\User; | ||
|
||
use OCP\IDBConnection; | ||
|
||
class UsernameDuplicationPreventionManager { | ||
/** @var IDBConnection */ | ||
private $dbConnection; | ||
private const TABLE_NAME = 'previously_used_userids'; | ||
private const HASHED_USER_ID_NAME = 'user_id_hash'; | ||
|
||
public function __construct(IDBConnection $connection) { | ||
$this->dbConnection = $connection; | ||
} | ||
|
||
private function calculateUserNameHash(string $username) : string { | ||
return hash('sha512', $username); | ||
} | ||
|
||
public function markUsed(string $userName) : void { | ||
$queryBuilder = $this->dbConnection->getQueryBuilder(); | ||
$queryBuilder->insert(self::TABLE_NAME) | ||
->values([ | ||
self::HASHED_USER_ID_NAME => $queryBuilder->createNamedParameter($this->calculateUserNameHash($userName)), | ||
]) | ||
->executeStatement(); | ||
} | ||
|
||
public function wasUsed(string $userName) : bool { | ||
$queryBuilder = $this->dbConnection->getQueryBuilder(); | ||
$result = $queryBuilder->select($queryBuilder->func()->count()) | ||
->from(self::TABLE_NAME) | ||
->where( | ||
$queryBuilder->expr()->eq( | ||
self::HASHED_USER_ID_NAME, | ||
$queryBuilder->expr()->literal($this->calculateUserNameHash($userName)) | ||
) | ||
) | ||
->executeQuery(); | ||
return (int)$result->fetchOne() !== 0; | ||
} | ||
|
||
public function cleanUp(): void { | ||
$qb = $this->dbConnection->getQueryBuilder(); | ||
|
||
$qb | ||
->delete(self::TABLE_NAME) | ||
->executeStatement(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @author Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace Test\User; | ||
|
||
use OC\User\UserDeletedListener; | ||
use OC\User\UsernameDuplicationPreventionManager; | ||
use OCP\IUser; | ||
use OCP\User\Events\UserDeletedEvent; | ||
use Test\TestCase; | ||
|
||
class UserDeletedListenerTest extends TestCase { | ||
public function testHandle() { | ||
/** @var UsernameDuplicationPreventionManager|PHPUnit_Framework_MockObject_MockObject $usernameDuplicationPreventionManager */ | ||
$usernameDuplicationPreventionManager = $this->createMock(UsernameDuplicationPreventionManager::class); | ||
$usernameDuplicationPreventionManager | ||
->expects($this->once()) | ||
->method('markUsed') | ||
->with($this->equalTo('ThisIsTheUsername')); | ||
/** @var IUser|PHPUnit_Framework_MockObject_MockObject $mockUser */ | ||
$mockUser = $this->createMock(IUser::class); | ||
$mockUser | ||
->expects($this->once()) | ||
->method('getUID') | ||
->willReturn('ThisIsTheUsername'); | ||
|
||
$listener = new UserDeletedListener($usernameDuplicationPreventionManager); | ||
$listener->handle(new UserDeletedEvent($mockUser)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/lib/User/UsernameDuplicationPreventionManagerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @author Lukas Reschke <lukas@statuscode.ch> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace Test\User; | ||
|
||
use OC\User\UsernameDuplicationPreventionManager; | ||
use Test\TestCase; | ||
|
||
/** | ||
* @group DB | ||
*/ | ||
class UsernameDuplicationPreventionManagerTest extends TestCase { | ||
/** @var UsernameDuplicationPreventionManager */ | ||
private $usernameDuplicationPreventionManager; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->usernameDuplicationPreventionManager = \OC::$server->get(UsernameDuplicationPreventionManager::class); | ||
} | ||
|
||
protected function tearDown(): void { | ||
parent::tearDown(); | ||
|
||
$this->usernameDuplicationPreventionManager->cleanUp(); | ||
} | ||
|
||
public function testNotMarkedAsDeleted() { | ||
$return = $this->usernameDuplicationPreventionManager->wasUsed('not_deleted_user'); | ||
$this->assertFalse($return); | ||
} | ||
|
||
public function testMarkedAsDeleted() { | ||
$this->usernameDuplicationPreventionManager->markUsed('deleted_user'); | ||
$return = $this->usernameDuplicationPreventionManager->wasUsed('deleted_user'); | ||
$this->assertTrue($return); | ||
} | ||
} |