Skip to content

Commit d7043e5

Browse files
Merge pull request #59092 from nextcloud/jtr/refactor-setup-central-db-gen
2 parents f54b0aa + 70c0b60 commit d7043e5

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

lib/private/Setup/AbstractDatabase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ public function initialize(array $config): void {
7575
$this->tablePrefix = $dbTablePrefix;
7676
}
7777

78+
/**
79+
* Generate a strong random password suitable for database user accounts.
80+
*
81+
* Guarantees at least 2 uppercase, 2 lowercase, 2 digit, and 2 symbol
82+
* characters are present, with symbols filtered to exclude characters
83+
* that are problematic in SQL string contexts (", \, ', `).
84+
*
85+
* @return string A 30-character random password
86+
*/
87+
protected function generateDbPassword(): string {
88+
$safeSymbols = str_replace(['\"', '\\', '\'', '`'], '', ISecureRandom::CHAR_SYMBOLS);
89+
90+
$password = $this->random->generate(22, ISecureRandom::CHAR_ALPHANUMERIC . $safeSymbols)
91+
. $this->random->generate(2, ISecureRandom::CHAR_UPPER)
92+
. $this->random->generate(2, ISecureRandom::CHAR_LOWER)
93+
. $this->random->generate(2, ISecureRandom::CHAR_DIGITS)
94+
. $this->random->generate(2, $safeSymbols);
95+
96+
return str_shuffle($password);
97+
}
98+
7899
/**
79100
* @param array $configOverwrite
80101
* @return \OC\DB\Connection

lib/private/Setup/MySQL.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use OC\DB\ConnectionAdapter;
1414
use OC\DB\MySqlTools;
1515
use OCP\IDBConnection;
16-
use OCP\Security\ISecureRandom;
1716

1817
class MySQL extends AbstractDatabase {
1918
public string $dbprettyname = 'MySQL/MariaDB';
@@ -127,14 +126,8 @@ private function createSpecificUser(string $username, IDBConnection $connection)
127126
$rootUser = $this->dbUser;
128127
$rootPassword = $this->dbPassword;
129128

130-
//create a random password so we don't need to store the admin password in the config file
131-
$saveSymbols = str_replace(['\"', '\\', '\'', '`'], '', ISecureRandom::CHAR_SYMBOLS);
132-
$password = $this->random->generate(22, ISecureRandom::CHAR_ALPHANUMERIC . $saveSymbols)
133-
. $this->random->generate(2, ISecureRandom::CHAR_UPPER)
134-
. $this->random->generate(2, ISecureRandom::CHAR_LOWER)
135-
. $this->random->generate(2, ISecureRandom::CHAR_DIGITS)
136-
. $this->random->generate(2, $saveSymbols);
137-
$this->dbPassword = str_shuffle($password);
129+
// Create a random password so we don't need to store the admin password in the config file
130+
$this->dbPassword = $this->generateDbPassword();
138131

139132
try {
140133
//user already specified in config

lib/private/Setup/PostgreSQL.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use OC\DatabaseSetupException;
1212
use OC\DB\Connection;
1313
use OC\DB\QueryBuilder\Literal;
14-
use OCP\Security\ISecureRandom;
15-
use OCP\Server;
1614

1715
class PostgreSQL extends AbstractDatabase {
1816
public $dbprettyname = 'PostgreSQL';
@@ -48,8 +46,9 @@ public function setupDatabase(): void {
4846

4947
//add prefix to the postgresql user name to prevent collisions
5048
$this->dbUser = 'oc_admin';
51-
//create a new password so we don't need to store the admin config in the config file
52-
$this->dbPassword = Server::get(ISecureRandom::class)->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
49+
50+
// Create a new password so we don't need to store the admin config in the config file
51+
$this->dbPassword = $this->generateDbPassword();
5352

5453
$this->createDBUser($connection);
5554
}

0 commit comments

Comments
 (0)