Skip to content

Commit eb81e1e

Browse files
committed
Support special characters in database password, closes pterodactyl#1508
1 parent e7e41d8 commit eb81e1e

File tree

6 files changed

+49
-52
lines changed

6 files changed

+49
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ that they are the owner of, as well as all servers they're a subuser of.
2929
* Insurgency egg now uses the proper dedicated server ID.
3030
* Teamspeak egg updated with improved installation process and grabbing latest versions.
3131
* OOM killer disabled by default on all new servers.
32+
* Passwords generated for MySQL now include special characters and are 24 characters in length.
3233

3334
## v0.7.14 (Derelict Dermodactylus)
3435
### Fixed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ public function newDatabase(StoreServerDatabaseRequest $request, $server)
589589
* @param int $server
590590
* @return \Illuminate\Http\RedirectResponse
591591
*
592-
* @throws \Exception
593-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
592+
* @throws \Throwable
594593
*/
595594
public function resetDatabasePassword(Request $request, $server)
596595
{
@@ -599,7 +598,7 @@ public function resetDatabasePassword(Request $request, $server)
599598
['id', '=', $request->input('database')],
600599
]);
601600

602-
$this->databasePasswordService->handle($database, str_random(24));
601+
$this->databasePasswordService->handle($database);
603602

604603
return response('', 204);
605604
}

app/Http/Controllers/Api/Application/Servers/DatabaseController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ public function view(GetServerDatabaseRequest $request): array
8787
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\ServerDatabaseWriteRequest $request
8888
* @return \Illuminate\Http\Response
8989
*
90-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
91-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
90+
* @throws \Throwable
9291
*/
9392
public function resetPassword(ServerDatabaseWriteRequest $request): Response
9493
{
95-
$this->databasePasswordService->handle($request->getModel(Database::class), str_random(24));
94+
$this->databasePasswordService->handle($request->getModel(Database::class));
9695

9796
return response('', 204);
9897
}

app/Http/Controllers/Server/DatabaseController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,13 @@ public function store(StoreServerDatabaseRequest $request): RedirectResponse
135135
* @return \Illuminate\Http\JsonResponse
136136
*
137137
* @throws \Illuminate\Auth\Access\AuthorizationException
138-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
139-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
138+
* @throws \Throwable
140139
*/
141140
public function update(Request $request): JsonResponse
142141
{
143142
$this->authorize('reset-db-password', $request->attributes->get('server'));
144143

145-
$password = str_random(24);
146-
$this->passwordService->handle($request->attributes->get('database'), $password);
144+
$password = $this->passwordService->handle($request->attributes->get('database'));
147145

148146
return response()->json(['password' => $password]);
149147
}

app/Services/Databases/DatabasePasswordService.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Pterodactyl\Services\Databases;
44

5+
use Exception;
56
use Pterodactyl\Models\Database;
7+
use Illuminate\Support\Facades\Log;
68
use Illuminate\Database\ConnectionInterface;
79
use Illuminate\Contracts\Encryption\Encrypter;
810
use Pterodactyl\Extensions\DynamicDatabaseConnection;
@@ -54,33 +56,39 @@ public function __construct(
5456
* Updates a password for a given database.
5557
*
5658
* @param \Pterodactyl\Models\Database|int $database
57-
* @param string $password
58-
* @return bool
59+
* @return string
5960
*
60-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
61-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
61+
* @throws \Throwable
6262
*/
63-
public function handle($database, string $password): bool
63+
public function handle(Database $database): string
6464
{
65-
if (! $database instanceof Database) {
66-
$database = $this->repository->find($database);
67-
}
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)];
6871

69-
$this->dynamic->set('dynamic', $database->database_host_id);
70-
$this->connection->beginTransaction();
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+
}
7178

72-
$updated = $this->repository->withoutFreshModel()->update($database->id, [
73-
'password' => $this->encrypter->encrypt($password),
74-
]);
79+
$this->connection->transaction(function () use ($database, $password) {
80+
$this->dynamic->set('dynamic', $database->database_host_id);
7581

76-
$this->repository->dropUser($database->username, $database->remote);
77-
$this->repository->createUser($database->username, $database->remote, $password);
78-
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
79-
$this->repository->flush();
82+
$this->repository->withoutFreshModel()->update($database->id, [
83+
'password' => $this->encrypter->encrypt($password),
84+
]);
8085

81-
unset($password);
82-
$this->connection->commit();
86+
$this->repository->dropUser($database->username, $database->remote);
87+
$this->repository->createUser($database->username, $database->remote, $password);
88+
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
89+
$this->repository->flush();
90+
});
8391

84-
return $updated;
92+
return $password;
8593
}
8694
}

tests/Unit/Services/Databases/DatabasePasswordServiceTest.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,35 @@ public function setUp()
4848

4949
/**
5050
* Test that a password can be updated.
51-
*
52-
* @dataProvider useModelDataProvider
5351
*/
54-
public function testPasswordIsChanged(bool $useModel)
52+
public function testPasswordIsChanged()
5553
{
5654
$model = factory(Database::class)->make();
5755

58-
if (! $useModel) {
59-
$this->repository->shouldReceive('find')->with(1234)->once()->andReturn($model);
60-
}
56+
$this->connection->expects('transaction')->with(m::on(function ($closure) {
57+
return is_null($closure());
58+
}));
6159

6260
$this->dynamic->shouldReceive('set')->with('dynamic', $model->database_host_id)->once()->andReturnNull();
63-
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
64-
$this->encrypter->shouldReceive('encrypt')->with('test123')->once()->andReturn('enc123');
61+
62+
$this->encrypter->expects('encrypt')->with(m::on(function ($string) {
63+
preg_match_all('/[!@+=^-]/', $string, $matches, PREG_SET_ORDER);
64+
$this->assertTrue(count($matches) >= 2 && count($matches) <= 6, "Failed asserting that [{$string}] contains 2 to 6 special characters.");
65+
$this->assertTrue(strlen($string) === 24, "Failed asserting that [{$string}] is 24 characters in length.");
66+
67+
return true;
68+
}))->andReturn('enc123');
6569

6670
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
6771
$this->repository->shouldReceive('update')->with($model->id, ['password' => 'enc123'])->once()->andReturn(true);
6872

6973
$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturn(true);
70-
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, 'test123')->once()->andReturn(true);
74+
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, m::any())->once()->andReturn(true);
7175
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturn(true);
7276
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturn(true);
73-
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
7477

75-
$response = $this->getService()->handle($useModel ? $model : 1234, 'test123');
78+
$response = $this->getService()->handle($model);
7679
$this->assertNotEmpty($response);
77-
$this->assertTrue($response);
78-
}
79-
80-
/**
81-
* Data provider to determine if a model should be passed or an int.
82-
*
83-
* @return array
84-
*/
85-
public function useModelDataProvider(): array
86-
{
87-
return [[false], [true]];
8880
}
8981

9082
/**

0 commit comments

Comments
 (0)