Improve key handling

This commit is contained in:
robert dennis
2026-06-28 17:52:22 -04:00
parent ace8effd07
commit ea3b492dd5
6 changed files with 132 additions and 12 deletions
@@ -8,6 +8,7 @@ use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Permission;
use Pterodactyl\Models\UserSSHKey;
use phpseclib3\Crypt\PublicKeyLoader;
use Pterodactyl\Http\Controllers\Controller;
use phpseclib3\Exception\NoKeyLoadedException;
@@ -55,10 +56,14 @@ class SftpAuthenticationController extends Controller
}
} else {
$key = null;
try {
$key = PublicKeyLoader::loadPublicKey(trim($request->input('password')));
} catch (NoKeyLoadedException) {
// do nothing
$publicKey = trim($request->input('password'));
if (UserSSHKey::isSupportedPublicKeyMaterial($publicKey)) {
try {
$key = PublicKeyLoader::loadPublicKey($publicKey);
} catch (NoKeyLoadedException) {
// do nothing
}
}
if (!$key || !$user->sshKeys()->where('fingerprint', $key->getFingerprint('sha256'))->exists()) {
@@ -33,8 +33,20 @@ class StoreSSHKeyRequest extends ClientApiRequest
public function withValidator(Validator $validator): void
{
$validator->after(function () {
$publicKey = $this->input('public_key');
if (!is_string($publicKey)) {
return;
}
$publicKey = trim($publicKey);
if (!UserSSHKey::isSupportedPublicKeyMaterial($publicKey)) {
$this->validator->errors()->add('public_key', 'The public key provided is not valid.');
return;
}
try {
$this->key = PublicKeyLoader::loadPublicKey($this->input('public_key'));
$this->key = PublicKeyLoader::loadPublicKey($publicKey);
} catch (NoKeyLoadedException $exception) {
$this->validator->errors()->add('public_key', 'The public key provided is not valid.');
+32 -1
View File
@@ -44,6 +44,20 @@ class UserSSHKey extends Model
use SoftDeletes;
public const RESOURCE_NAME = 'ssh_key';
public const PUBLIC_KEY_MAX_LENGTH = 16384;
protected const PUBLIC_KEY_PREFIXES = [
'ssh-rsa ',
'ssh-ed25519 ',
'ecdsa-sha2-',
'sk-ssh-ed25519@openssh.com ',
'sk-ecdsa-sha2-nistp256@openssh.com ',
'-----BEGIN PUBLIC KEY-----',
'-----BEGIN RSA PUBLIC KEY-----',
'-----BEGIN EC PUBLIC KEY-----',
'-----BEGIN DSA PUBLIC KEY-----',
'---- BEGIN SSH2 PUBLIC KEY ----',
];
protected $table = 'user_ssh_keys';
@@ -56,9 +70,26 @@ class UserSSHKey extends Model
public static array $validationRules = [
'name' => ['required', 'string'],
'fingerprint' => ['required', 'string'],
'public_key' => ['required', 'string'],
'public_key' => ['required', 'string', 'max:' . self::PUBLIC_KEY_MAX_LENGTH],
];
public static function isSupportedPublicKeyMaterial(string $value): bool
{
$value = trim($value);
if ($value === '' || strlen($value) > self::PUBLIC_KEY_MAX_LENGTH || str_contains($value, "\0")) {
return false;
}
foreach (self::PUBLIC_KEY_PREFIXES as $prefix) {
if (str_starts_with($value, $prefix)) {
return true;
}
}
return false;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>
*/
Generated
+6 -6
View File
@@ -3547,16 +3547,16 @@
},
{
"name": "phpseclib/phpseclib",
"version": "3.0.47",
"version": "3.0.55",
"source": {
"type": "git",
"url": "https://github.com/phpseclib/phpseclib.git",
"reference": "9d6ca36a6c2dd434765b1071b2644a1c683b385d"
"reference": "db9744e6d47e742b1f974e965ad49bdd041105af"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/9d6ca36a6c2dd434765b1071b2644a1c683b385d",
"reference": "9d6ca36a6c2dd434765b1071b2644a1c683b385d",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/db9744e6d47e742b1f974e965ad49bdd041105af",
"reference": "db9744e6d47e742b1f974e965ad49bdd041105af",
"shasum": ""
},
"require": {
@@ -3637,7 +3637,7 @@
],
"support": {
"issues": "https://github.com/phpseclib/phpseclib/issues",
"source": "https://github.com/phpseclib/phpseclib/tree/3.0.47"
"source": "https://github.com/phpseclib/phpseclib/tree/3.0.55"
},
"funding": [
{
@@ -3653,7 +3653,7 @@
"type": "tidelift"
}
],
"time": "2025-10-06T01:07:24+00:00"
"time": "2026-06-14T23:24:10+00:00"
},
{
"name": "pragmarx/google2fa",
@@ -110,6 +110,20 @@ class SSHKeyControllerTest extends ClientApiIntegrationTestCase
->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');
}
public function testCertificateCannotBeStoredAsSSHKey()
{
$user = User::factory()->create();
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
'name' => 'Name',
'public_key' => $this->makeCertificate(),
])
->assertUnprocessable()
->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');
$this->assertEquals(0, $user->sshKeys()->count());
}
public function testPublicKeyCanBeStored()
{
$user = User::factory()->create();
@@ -141,4 +155,26 @@ class SSHKeyControllerTest extends ClientApiIntegrationTestCase
$this->assertEquals(1, $user->sshKeys()->count());
}
protected function makeCertificate(): string
{
return <<<'PEM'
-----BEGIN CERTIFICATE-----
MIIClzCCAX+gAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDDAR0ZXN0
MB4XDTI2MDYyODIxMzQzMFoXDTI2MDYyOTIxMzQzMFowDzENMAsGA1UEAwwEdGVz
dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALZNPzyWTEinfefSMYI8
jPtiDpQ3n4xnqobumxAMDSQd7Wkbi9MyWfV3tr7PIVzblC4aH5iLIy5dOhUWqyBd
LwYbmdGfeghnP261CrYw4npoBO+k1CoAtfjxuv5Mkz9zs4/BtknyqKxteLxLglJI
VTTl/IdGVdacvBSkfystMkK3AjvIwNseWLe2fcwMSs1k0yN/p/6NUYsO4BBkybaM
JF7s3s29nKZDwPn8HxYD/5cnStSI0nhcltYF5O7/6DiH4x5lvT4Z9D+aHppDMTur
yxAkMTSTZqMkE5iOtk6XrnEaXDVOci1fYFYIO0yKExnbf2DkB//W9f1wMYbO40Zc
jMkCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAAbbdgyP9X3kAgMdMo2yMX6jJC9Kl
NTio30d+NPCXsziA6elS2wWK7LhAVx0WRCho3KNJC2j3sKGOSXwf7HlG8yX4QPng
oMf91+yM95yhJZxVGelKfBGg34Wu1e8l0FcuCchmseR8QtwwuwScDXnYQV7PyRiW
OJ5KEg6opQivmfI4gJWDNbe6ALwHxR0TZeRITQ+tU0r/JCFwbjMX2pFNAl2sjE9x
iiZwIndK2bAsME622kuPgfx/osJ/8zuQhBeRsiLfUT44j2RJNRj99gXRfKAA0vyG
LaKfKLpXnF7mm6UuShG/HRt07bxu6Ayan/SJnv3E5ZkinR5lX0upcmM/gQ==
-----END CERTIFICATE-----
PEM;
}
}
@@ -124,6 +124,20 @@ class SftpAuthenticationControllerTest extends IntegrationTestCase
}
}
public function testCertificatePublicKeyAuthenticationIsRejectedAsInvalidKey()
{
$certificate = $this->makeCertificate();
for ($i = 0; $i <= 5; ++$i) {
$this->postJson('/api/remote/sftp/auth', [
'type' => 'public_key',
'username' => $this->getUsername(),
'password' => $certificate,
])
->assertStatus($i === 5 ? 429 : 403);
}
}
/**
* Test that a request is rejected if the credentials are valid but the username indicates
* a server on a different node.
@@ -243,4 +257,26 @@ class SftpAuthenticationControllerTest extends IntegrationTestCase
$this->withHeader('Authorization', 'Bearer ' . $node->daemon_token_id . '.' . decrypt($node->daemon_token));
}
protected function makeCertificate(): string
{
return <<<'PEM'
-----BEGIN CERTIFICATE-----
MIIClzCCAX+gAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDDAR0ZXN0
MB4XDTI2MDYyODIxMzQzMFoXDTI2MDYyOTIxMzQzMFowDzENMAsGA1UEAwwEdGVz
dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALZNPzyWTEinfefSMYI8
jPtiDpQ3n4xnqobumxAMDSQd7Wkbi9MyWfV3tr7PIVzblC4aH5iLIy5dOhUWqyBd
LwYbmdGfeghnP261CrYw4npoBO+k1CoAtfjxuv5Mkz9zs4/BtknyqKxteLxLglJI
VTTl/IdGVdacvBSkfystMkK3AjvIwNseWLe2fcwMSs1k0yN/p/6NUYsO4BBkybaM
JF7s3s29nKZDwPn8HxYD/5cnStSI0nhcltYF5O7/6DiH4x5lvT4Z9D+aHppDMTur
yxAkMTSTZqMkE5iOtk6XrnEaXDVOci1fYFYIO0yKExnbf2DkB//W9f1wMYbO40Zc
jMkCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAAbbdgyP9X3kAgMdMo2yMX6jJC9Kl
NTio30d+NPCXsziA6elS2wWK7LhAVx0WRCho3KNJC2j3sKGOSXwf7HlG8yX4QPng
oMf91+yM95yhJZxVGelKfBGg34Wu1e8l0FcuCchmseR8QtwwuwScDXnYQV7PyRiW
OJ5KEg6opQivmfI4gJWDNbe6ALwHxR0TZeRITQ+tU0r/JCFwbjMX2pFNAl2sjE9x
iiZwIndK2bAsME622kuPgfx/osJ/8zuQhBeRsiLfUT44j2RJNRj99gXRfKAA0vyG
LaKfKLpXnF7mm6UuShG/HRt07bxu6Ayan/SJnv3E5ZkinR5lX0upcmM/gQ==
-----END CERTIFICATE-----
PEM;
}
}