From 1395a5360274a6c6c0b4084d22da53466998c954 Mon Sep 17 00:00:00 2001 From: Andrew Summers <18727110+summersab@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:29:33 -0500 Subject: [PATCH 1/3] Refactor `OC\Server::getSecureRandom` Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com> --- core/Command/Maintenance/Install.php | 3 ++- lib/private/Cache/File.php | 2 +- lib/private/DB/Connection.php | 3 ++- lib/private/Security/SecureRandom.php | 2 +- lib/private/Setup.php | 2 +- lib/private/Setup/PostgreSQL.php | 2 +- lib/private/Share20/ProviderFactory.php | 7 ++++--- lib/private/legacy/OC_Util.php | 5 +++-- lib/public/Security/ISecureRandom.php | 4 +++- tests/lib/DB/MigratorTest.php | 3 ++- tests/lib/TestCase.php | 2 +- 11 files changed, 21 insertions(+), 14 deletions(-) diff --git a/core/Command/Maintenance/Install.php b/core/Command/Maintenance/Install.php index 643bb54c0d605..ac5b3ddfbe01f 100644 --- a/core/Command/Maintenance/Install.php +++ b/core/Command/Maintenance/Install.php @@ -36,6 +36,7 @@ use OC\Setup; use OC\SystemConfig; use OCP\Defaults; +use OCP\Security\ISecureRandom; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; @@ -80,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $server->getL10N('lib'), $server->query(Defaults::class), $server->get(LoggerInterface::class), - $server->getSecureRandom(), + $server->get(ISecureRandom::class), \OC::$server->query(Installer::class) ); $sysInfo = $setupHelper->getSystemInfo(true); diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php index 72fc95a802b19..9b0493da38b08 100644 --- a/lib/private/Cache/File.php +++ b/lib/private/Cache/File.php @@ -105,7 +105,7 @@ public function set($key, $value, $ttl = 0) { $storage = $this->getStorage(); $result = false; // unique id to avoid chunk collision, just in case - $uniqueId = \OC::$server->getSecureRandom()->generate( + $uniqueId = \OC::$server->get(ISecureRandom::class)->generate( 16, ISecureRandom::CHAR_ALPHANUMERIC ); diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index 85c6a72dfdbd5..6150e4b204566 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -52,6 +52,7 @@ use OCP\IRequestId; use OCP\PreConditionNotMetException; use OCP\Profiler\IProfiler; +use OCP\Security\ISecureRandom; use OC\DB\QueryBuilder\QueryBuilder; use OC\SystemConfig; use Psr\Log\LoggerInterface; @@ -592,7 +593,7 @@ public function migrateToSchema(Schema $toSchema, bool $dryRun = false) { private function getMigrator() { // TODO properly inject those dependencies - $random = \OC::$server->getSecureRandom(); + $random = \OC::$server->get(ISecureRandom::class); $platform = $this->getDatabasePlatform(); $config = \OC::$server->getConfig(); $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); diff --git a/lib/private/Security/SecureRandom.php b/lib/private/Security/SecureRandom.php index cbd1dc8db6dc9..dcf50f00629f2 100644 --- a/lib/private/Security/SecureRandom.php +++ b/lib/private/Security/SecureRandom.php @@ -35,7 +35,7 @@ * use a fallback. * * Usage: - * \OC::$server->getSecureRandom()->generate(10); + * \OC::$server->get(ISecureRandom::class)->generate(10); * @package OC\Security */ class SecureRandom implements ISecureRandom { diff --git a/lib/private/Setup.php b/lib/private/Setup.php index 0993fe54f4708..5c7cd10a2a7ed 100644 --- a/lib/private/Setup.php +++ b/lib/private/Setup.php @@ -510,7 +510,7 @@ public static function updateHtaccess() { \OC::$server->getL10N('lib'), \OCP\Server::get(Defaults::class), \OC::$server->get(LoggerInterface::class), - \OC::$server->getSecureRandom(), + \OC::$server->get(ISecureRandom::class), \OCP\Server::get(Installer::class) ); diff --git a/lib/private/Setup/PostgreSQL.php b/lib/private/Setup/PostgreSQL.php index 490cbba69a94c..d400bbf7e8bc0 100644 --- a/lib/private/Setup/PostgreSQL.php +++ b/lib/private/Setup/PostgreSQL.php @@ -69,7 +69,7 @@ public function setupDatabase($username) { //add prefix to the postgresql user name to prevent collisions $this->dbUser = 'oc_' . strtolower($username); //create a new password so we don't need to store the admin config in the config file - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, ISecureRandom::CHAR_ALPHANUMERIC); + $this->dbPassword = \OC::$server->get(ISecureRandom::class)->generate(30, ISecureRandom::CHAR_ALPHANUMERIC); $this->createDBUser($connection); diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php index 8c01d6609155c..03284b6d78d05 100644 --- a/lib/private/Share20/ProviderFactory.php +++ b/lib/private/Share20/ProviderFactory.php @@ -44,6 +44,7 @@ use OCP\Defaults; use OCP\EventDispatcher\IEventDispatcher; use OCP\IServerContainer; +use OCP\Security\ISecureRandom; use OCP\Share\IManager; use OCP\Share\IProviderFactory; use OCP\Share\IShare; @@ -146,7 +147,7 @@ protected function federatedShareProvider() { $this->serverContainer->get(LoggerInterface::class), ); $tokenHandler = new TokenHandler( - $this->serverContainer->getSecureRandom() + $this->serverContainer->get(ISecureRandom::class) ); $this->federatedProvider = new FederatedShareProvider( @@ -188,7 +189,7 @@ protected function getShareByMailProvider() { $this->shareByMailProvider = new ShareByMailProvider( $this->serverContainer->getConfig(), $this->serverContainer->getDatabaseConnection(), - $this->serverContainer->getSecureRandom(), + $this->serverContainer->get(ISecureRandom::class), $this->serverContainer->getUserManager(), $this->serverContainer->getLazyRootFolder(), $this->serverContainer->getL10N('sharebymail'), @@ -230,7 +231,7 @@ protected function getShareByCircleProvider() { if ($this->shareByCircleProvider === null) { $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider( $this->serverContainer->getDatabaseConnection(), - $this->serverContainer->getSecureRandom(), + $this->serverContainer->get(ISecureRandom::class), $this->serverContainer->getUserManager(), $this->serverContainer->getLazyRootFolder(), $this->serverContainer->getL10N('circles'), diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index 9d62c46137e61..a0575c4bef3e4 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -71,6 +71,7 @@ use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUser; +use OCP\Security\ISecureRandom; use OCP\Share\IManager; use Psr\Log\LoggerInterface; @@ -518,7 +519,7 @@ public static function checkServer(\OC\SystemConfig $config) { \OC::$server->getL10N('lib'), \OC::$server->get(\OCP\Defaults::class), \OC::$server->get(LoggerInterface::class), - \OC::$server->getSecureRandom(), + \OC::$server->get(ISecureRandom::class), \OC::$server->get(\OC\Installer::class) ); @@ -845,7 +846,7 @@ public static function getInstanceId() { $id = \OC::$server->getSystemConfig()->getValue('instanceid', null); if (is_null($id)) { // We need to guarantee at least one letter in instanceid so it can be used as the session_name - $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); + $id = 'oc' . \OC::$server->get(ISecureRandom::class)->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); \OC::$server->getSystemConfig()->setValue('instanceid', $id); } return $id; diff --git a/lib/public/Security/ISecureRandom.php b/lib/public/Security/ISecureRandom.php index 3634ebf99f7f0..6cb6854a03369 100644 --- a/lib/public/Security/ISecureRandom.php +++ b/lib/public/Security/ISecureRandom.php @@ -28,13 +28,15 @@ */ namespace OCP\Security; +use OCP\Security\ISecureRandom; + /** * Class SecureRandom provides a wrapper around the random_int function to generate * secure random strings. For PHP 7 the native CSPRNG is used, older versions do * use a fallback. * * Usage: - * \OC::$server->getSecureRandom()->generate(10); + * \OC::$server->get(ISecureRandom::class)->generate(10); * * @since 8.0.0 */ diff --git a/tests/lib/DB/MigratorTest.php b/tests/lib/DB/MigratorTest.php index 4d7d9cab19fa9..06bb005cc44b6 100644 --- a/tests/lib/DB/MigratorTest.php +++ b/tests/lib/DB/MigratorTest.php @@ -24,6 +24,7 @@ use OC\DB\SQLiteMigrator; use OCP\DB\Types; use OCP\IConfig; +use OCP\Security\ISecureRandom; /** * Class MigratorTest @@ -61,7 +62,7 @@ protected function setUp(): void { private function getMigrator(): Migrator { $platform = $this->connection->getDatabasePlatform(); - $random = \OC::$server->getSecureRandom(); + $random = \OC::$server->get(ISecureRandom::class); $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); if ($platform instanceof SqlitePlatform) { return new SQLiteMigrator($this->connection, $this->config, $dispatcher); diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index f5fc9a6e8f2c4..130deaf11a712 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -255,7 +255,7 @@ protected static function invokePrivate($object, $methodName, array $parameters * @return string */ protected static function getUniqueID($prefix = '', $length = 13) { - return $prefix . \OC::$server->getSecureRandom()->generate( + return $prefix . \OC::$server->get(ISecureRandom::class)->generate( $length, // Do not use dots and slashes as we use the value for file names ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER From fa55a74033f8fbabd9052139e44caf4164f313f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Thu, 30 May 2024 14:57:53 +0200 Subject: [PATCH 2/3] chore: refactor remove unused php imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com> Signed-off-by: John Molakvoæ --- core/Command/Maintenance/Install.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/Command/Maintenance/Install.php b/core/Command/Maintenance/Install.php index 56bf67016ec00..bfe955f99795c 100644 --- a/core/Command/Maintenance/Install.php +++ b/core/Command/Maintenance/Install.php @@ -15,9 +15,6 @@ use OC\Migration\ConsoleOutput; use OC\Setup; use OC\SystemConfig; -use OCP\Defaults; -use OCP\Security\ISecureRandom; -use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; From 928f22052f3fb52c78c91e5c993702ab673c0dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Thu, 30 May 2024 18:36:23 +0200 Subject: [PATCH 3/3] chore: remove unnecessary php import during refactor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- lib/public/Security/ISecureRandom.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/public/Security/ISecureRandom.php b/lib/public/Security/ISecureRandom.php index a8ce92daad82b..188236dd3f9b5 100644 --- a/lib/public/Security/ISecureRandom.php +++ b/lib/public/Security/ISecureRandom.php @@ -8,8 +8,6 @@ */ namespace OCP\Security; -use OCP\Security\ISecureRandom; - /** * Class SecureRandom provides a wrapper around the random_int function to generate * secure random strings. For PHP 7 the native CSPRNG is used, older versions do