mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
118 lines
6.5 KiB
PHP
118 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Tests\Pest\Integration\Api\Remote\SftpAuthenticationControllerTest;
|
|
|
|
use phpseclib3\Crypt\EC;
|
|
use Pterodactyl\Enum\Permissions;
|
|
use Pterodactyl\Models\Node;
|
|
use Pterodactyl\Models\Server;
|
|
use Pterodactyl\Models\UserSSHKey;
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use function pterodactylTestCase;
|
|
|
|
uses(IntegrationTestCase::class);
|
|
/**
|
|
* Sets up the tests.
|
|
*/
|
|
beforeEach(function () {
|
|
[$user, $server] = $this->generateTestAccount();
|
|
$user->update(['password' => 'foobar']);
|
|
$this->user = $user;
|
|
$this->server = $server;
|
|
setAuthorization();
|
|
});
|
|
dataset('authorizationTypeDataProvider', function () {
|
|
return ['password auth' => ['password'], 'public key auth' => ['public_key']];
|
|
});
|
|
dataset('serverStateDataProvider', function () {
|
|
return ['installing' => [Server::STATUS_INSTALLING], 'suspended' => [Server::STATUS_SUSPENDED], 'restoring a backup' => [Server::STATUS_RESTORING_BACKUP]];
|
|
});
|
|
test('public key is validated correctly', function () {
|
|
$key = UserSSHKey::factory()->for($this->user)->create();
|
|
$this->postJson('/api/remote/sftp/auth', [])->assertUnprocessable()->assertJsonPath('errors.0.meta.source_field', 'username')->assertJsonPath('errors.0.meta.rule', 'required')->assertJsonPath('errors.1.meta.source_field', 'password')->assertJsonPath('errors.1.meta.rule', 'required');
|
|
$data = ['type' => 'public_key', 'username' => getUsername(), 'password' => $key->public_key];
|
|
$this->postJson('/api/remote/sftp/auth', $data)->assertOk()->assertJsonPath('server', $this->server->uuid)->assertJsonPath('permissions', ['*']);
|
|
$key->delete();
|
|
$this->postJson('/api/remote/sftp/auth', $data)->assertForbidden();
|
|
$this->postJson('/api/remote/sftp/auth', array_merge($data, ['type' => null]))->assertForbidden();
|
|
});
|
|
test('password is validated correctly', function () {
|
|
$this->postJson('/api/remote/sftp/auth', ['username' => getUsername(), 'password' => ''])->assertUnprocessable()->assertJsonPath('errors.0.meta.source_field', 'password')->assertJsonPath('errors.0.meta.rule', 'required');
|
|
$this->postJson('/api/remote/sftp/auth', ['username' => getUsername(), 'password' => 'wrong password'])->assertForbidden();
|
|
$this->user->update(['password' => 'foobar']);
|
|
$this->postJson('/api/remote/sftp/auth', ['username' => getUsername(), 'password' => 'foobar'])->assertOk();
|
|
});
|
|
test('user is throttled if invalid credentials are provided', function () {
|
|
// Requests alternate between two usernames, so each throttle key is hit
|
|
// every other request and the lockout fires once the first key has
|
|
// recorded the configured number of attempts.
|
|
$limit = (int) config('auth.lockout.attempts') * 2;
|
|
for ($i = 0; $i <= $limit; $i++) {
|
|
$this->postJson('/api/remote/sftp/auth', ['type' => 'public_key', 'username' => $i % 2 === 0 ? $this->user->username : getUsername(), 'password' => 'invalid key'])->assertStatus($i === $limit ? 429 : 403);
|
|
}
|
|
});
|
|
test('user is not throttled if no public key matches', function () {
|
|
for ($i = 0; $i <= 10; $i++) {
|
|
$this->postJson('/api/remote/sftp/auth', ['type' => 'public_key', 'username' => getUsername(), 'password' => EC::createKey('Ed25519')->getPublicKey()->toString('OpenSSH')])->assertForbidden();
|
|
}
|
|
});
|
|
test('an x509 certificate offered as a public key is rejected and throttled', function (): void {
|
|
$certificate = file_get_contents(base_path('tests/Fixtures/keys/x509-certificate.pem'));
|
|
$limit = (int) config('auth.lockout.attempts');
|
|
for ($i = 0; $i <= $limit; $i++) {
|
|
$this->postJson('/api/remote/sftp/auth', ['type' => 'public_key', 'username' => getUsername(), 'password' => $certificate])->assertStatus($i === $limit ? 429 : 403);
|
|
}
|
|
});
|
|
test('request is rejected if server belongs to different node', function (string $type) {
|
|
$node2 = $this->createServerModel()->node;
|
|
setAuthorization($node2);
|
|
$password = $type === 'public_key' ? UserSSHKey::factory()->for($this->user)->create()->public_key : 'foobar';
|
|
$this->postJson('/api/remote/sftp/auth', ['type' => $type, 'username' => getUsername(), 'password' => $password])->assertForbidden();
|
|
})->with('authorizationTypeDataProvider');
|
|
test('request is denied if user lacks sftp permission', function () {
|
|
[$user, $server] = $this->generateTestAccount([Permissions::FileRead->value]);
|
|
$user->update(['password' => 'foobar']);
|
|
setAuthorization($server->node);
|
|
$this->postJson('/api/remote/sftp/auth', ['username' => $user->username.'.'.$server->uuidShort, 'password' => 'foobar'])->assertForbidden()->assertJsonPath('errors.0.detail', 'You do not have permission to access SFTP for this server.');
|
|
});
|
|
test('invalid server state returns conflict error', function (string $status) {
|
|
$this->server->update(['status' => $status]);
|
|
$this->postJson('/api/remote/sftp/auth', ['username' => getUsername(), 'password' => 'foobar'])->assertStatus(409);
|
|
})->with('serverStateDataProvider');
|
|
test('user permissions are returned correctly', function () {
|
|
[$user, $server] = $this->generateTestAccount([Permissions::FileRead->value, Permissions::FileSftp->value]);
|
|
$user->update(['password' => 'foobar']);
|
|
setAuthorization($server->node);
|
|
$data = ['username' => $user->username.'.'.$server->uuidShort, 'password' => 'foobar'];
|
|
$this->postJson('/api/remote/sftp/auth', $data)->assertOk()->assertJsonPath('permissions', [Permissions::FileRead->value, Permissions::FileSftp->value]);
|
|
$user->update(['root_admin' => true]);
|
|
$this->postJson('/api/remote/sftp/auth', $data)->assertOk()->assertJsonPath('permissions.0', '*');
|
|
setAuthorization();
|
|
$data['username'] = $user->username.'.'.$this->server->uuidShort;
|
|
$this->post('/api/remote/sftp/auth', $data)->assertOk()->assertJsonPath('permissions.0', '*');
|
|
$user->update(['root_admin' => false]);
|
|
$this->post('/api/remote/sftp/auth', $data)->assertForbidden();
|
|
});
|
|
/**
|
|
* Returns the username for connecting to SFTP.
|
|
*/
|
|
function getUsername(bool $long = false): string
|
|
{
|
|
return (function () use ($long) {
|
|
return $this->user->username.'.'.($long ? $this->server->uuid : $this->server->uuidShort);
|
|
})->call(pterodactylTestCase());
|
|
}
|
|
/**
|
|
* Sets the authorization header for the rest of the test.
|
|
*/
|
|
function setAuthorization(?Node $node = null): void
|
|
{
|
|
(function () use ($node) {
|
|
$node = $node ?? $this->server->node;
|
|
$this->withHeader('Authorization', 'Bearer '.$node->daemon_token_id.'.'.decrypt($node->daemon_token));
|
|
})->call(pterodactylTestCase());
|
|
}
|