Skip to content

Commit 05d859c

Browse files
committed
Ensure password used when creating a database is valid; closes pterodactyl#1852
1 parent 1ebe376 commit 05d859c

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ This file is a running track of new features and fixes to each version of the pa
33

44
This project follows [Semantic Versioning](http://semver.org) guidelines.
55

6+
## v0.7.17 (Derelict Dermodactylus)
7+
### Fixed
8+
* Fixes database passwords not being generated with the proper requirements for some MySQL setups.
9+
610
## v0.7.16 (Derelict Dermodactylus)
711
### Fixed
812
* Fixed the /api/application/servers endpoint erroring when including subusers or egg

app/Helpers/Utilities.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Pterodactyl\Helpers;
4+
5+
use Exception;
6+
use Illuminate\Support\Facades\Log;
7+
8+
class Utilities
9+
{
10+
/**
11+
* Generates a random string and injects special characters into it, in addition to
12+
* the randomness of the alpha-numeric default response.
13+
*
14+
* @param int $length
15+
* @return string
16+
*/
17+
public static function randomStringWithSpecialCharacters(int $length = 16): string
18+
{
19+
$string = str_random($length);
20+
// Given a random string of characters, randomly loop through the characters and replace some
21+
// with special characters to avoid issues with MySQL password requirements on some servers.
22+
try {
23+
for ($i = 0; $i < random_int(2, 6); $i++) {
24+
$character = ['!', '@', '=', '.', '+', '^'][random_int(0, 5)];
25+
26+
$string = substr_replace($string, $character, random_int(0, $length - 1), 1);
27+
}
28+
} catch (Exception $exception) {
29+
// Just log the error and hope for the best at this point.
30+
Log::error($exception);
31+
}
32+
33+
return $string;
34+
}
35+
}

app/Services/Databases/DatabaseManagementService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Services\Databases;
44

55
use Pterodactyl\Models\Database;
6+
use Pterodactyl\Helpers\Utilities;
67
use Illuminate\Database\DatabaseManager;
78
use Illuminate\Contracts\Encryption\Encrypter;
89
use Pterodactyl\Extensions\DynamicDatabaseConnection;
@@ -69,7 +70,9 @@ public function create($server, array $data)
6970
$data['server_id'] = $server;
7071
$data['database'] = sprintf('s%d_%s', $server, $data['database']);
7172
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
72-
$data['password'] = $this->encrypter->encrypt(str_random(24));
73+
$data['password'] = $this->encrypter->encrypt(
74+
Utilities::randomStringWithSpecialCharacters(24)
75+
);
7376

7477
$this->database->beginTransaction();
7578
try {

app/Services/Databases/DatabasePasswordService.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Pterodactyl\Services\Databases;
44

5-
use Exception;
65
use Pterodactyl\Models\Database;
7-
use Illuminate\Support\Facades\Log;
6+
use Pterodactyl\Helpers\Utilities;
87
use Illuminate\Database\ConnectionInterface;
98
use Illuminate\Contracts\Encryption\Encrypter;
109
use Pterodactyl\Extensions\DynamicDatabaseConnection;
@@ -62,19 +61,7 @@ public function __construct(
6261
*/
6362
public function handle(Database $database): string
6463
{
65-
$password = str_random(24);
66-
// Given a random string of characters, randomly loop through the characters and replace some
67-
// with special characters to avoid issues with MySQL password requirements on some servers.
68-
try {
69-
for ($i = 0; $i < random_int(2, 6); $i++) {
70-
$character = ['!', '@', '=', '.', '+', '^'][random_int(0, 5)];
71-
72-
$password = substr_replace($password, $character, random_int(0, 23), 1);
73-
}
74-
} catch (Exception $exception) {
75-
// Just log the error and hope for the best at this point.
76-
Log::error($exception);
77-
}
64+
$password = Utilities::randomStringWithSpecialCharacters(24);
7865

7966
$this->connection->transaction(function () use ($database, $password) {
8067
$this->dynamic->set('dynamic', $database->database_host_id);

0 commit comments

Comments
 (0)