Skip to content

Commit a936406

Browse files
committed
Store keys in standard format; query with fingerprint not public key
1 parent b563f13 commit a936406

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

app/Http/Controllers/Api/Client/SSHKeyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function store(StoreSSHKeyRequest $request): array
2727
{
2828
$model = $request->user()->sshKeys()->create([
2929
'name' => $request->input('name'),
30-
'public_key' => $request->input('public_key'),
30+
'public_key' => $request->getPublicKey(),
3131
'fingerprint' => $request->getKeyFingerprint(),
3232
]);
3333

app/Http/Controllers/Api/Remote/SftpAuthenticationController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Pterodactyl\Models\Server;
88
use Illuminate\Http\JsonResponse;
99
use Pterodactyl\Models\Permission;
10+
use phpseclib3\Crypt\PublicKeyLoader;
1011
use Pterodactyl\Http\Controllers\Controller;
12+
use phpseclib3\Exception\NoKeyLoadedException;
1113
use Illuminate\Foundation\Auth\ThrottlesLogins;
1214
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
1315
use Pterodactyl\Services\Servers\GetUserPermissionsService;
@@ -52,7 +54,14 @@ public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
5254
$this->reject($request);
5355
}
5456
} else {
55-
if (!$user->sshKeys()->where('public_key', trim($request->input('password')))->exists()) {
57+
$key = null;
58+
try {
59+
$key = PublicKeyLoader::loadPublicKey(trim($request->input('password')));
60+
} catch (NoKeyLoadedException $exception) {
61+
// do nothing
62+
}
63+
64+
if (!$key || !$user->sshKeys()->where('fingerprint', $key->getFingerprint('sha256'))->exists()) {
5665
$this->reject($request, false);
5766
}
5867
}
@@ -61,7 +70,6 @@ public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
6170

6271
return new JsonResponse([
6372
'server' => $server->uuid,
64-
'public_keys' => $user->sshKeys->map(fn ($value) => $value->public_key)->toArray(),
6573
'permissions' => $permissions ?? ['*'],
6674
]);
6775
}

app/Http/Requests/Api/Client/Account/StoreSSHKeyRequest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public function withValidator(Validator $validator): void
5757
});
5858
}
5959

60+
/**
61+
* Returns the public key but formatted in a consistent manner.
62+
*/
63+
public function getPublicKey(): string
64+
{
65+
return $this->key->toString('PKCS8');
66+
}
67+
6068
/**
6169
* Returns the SHA256 fingerprint of the key provided.
6270
*/

0 commit comments

Comments
 (0)