Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Remote;
4
5
use phpseclib3\Crypt\EC;
6
use Pterodactyl\Models\Node;
7
use Pterodactyl\Models\User;
8
use Pterodactyl\Models\Server;
9
use Pterodactyl\Models\Permission;
10
use Pterodactyl\Models\UserSSHKey;
11
use Pterodactyl\Tests\Integration\IntegrationTestCase;
12
13
class SftpAuthenticationControllerTest extends IntegrationTestCase
14
{
15
protected User $user;
16
17
protected Server $server;
18
19
/**
20
* Sets up the tests.
21
*/
22
public function setUp(): void
23
{
24
parent::setUp();
25
26
[$user, $server] = $this->generateTestAccount();
27
28
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
29
30
$this->user = $user;
31
$this->server = $server;
32
33
$this->setAuthorization();
34
}
35
36
/**
37
* Test that a public key is validated correctly.
38
*/
39
public function testPublicKeyIsValidatedCorrectly()
40
{
41
$key = UserSSHKey::factory()->for($this->user)->create();
42
43
$this->postJson('/api/remote/sftp/auth', [])
44
->assertUnprocessable()
45
->assertJsonPath('errors.0.meta.source_field', 'username')
46
->assertJsonPath('errors.0.meta.rule', 'required')
47
->assertJsonPath('errors.1.meta.source_field', 'password')
48
->assertJsonPath('errors.1.meta.rule', 'required');
49
50
$data = [
51
'type' => 'public_key',
52
'username' => $this->getUsername(),
53
'password' => $key->public_key,
54
];
55
56
$this->postJson('/api/remote/sftp/auth', $data)
57
->assertOk()
58
->assertJsonPath('server', $this->server->uuid)
59
->assertJsonPath('permissions', ['*']);
60
61
$key->delete();
62
$this->postJson('/api/remote/sftp/auth', $data)->assertForbidden();
63
$this->postJson('/api/remote/sftp/auth', array_merge($data, ['type' => null]))->assertForbidden();
64
}
65
66
/**
67
* Test that an account password is validated correctly.
68
*/
69
public function testPasswordIsValidatedCorrectly()
70
{
71
$this->postJson('/api/remote/sftp/auth', [
72
'username' => $this->getUsername(),
73
'password' => '',
74
])
75
->assertUnprocessable()
76
->assertJsonPath('errors.0.meta.source_field', 'password')
77
->assertJsonPath('errors.0.meta.rule', 'required');
78
79
$this->postJson('/api/remote/sftp/auth', [
80
'username' => $this->getUsername(),
81
'password' => 'wrong password',
82
])
83
->assertForbidden();
84
85
$this->user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
86
87
$this->postJson('/api/remote/sftp/auth', [
88
'username' => $this->getUsername(),
89
'password' => 'foobar',
90
])
91
->assertOk();
92
}
93
94
/**
95
* Test that providing an invalid key and/or invalid username triggers the throttle on
96
* the endpoint.
97
*/
98
#[\PHPUnit\Framework\Attributes\DataProvider('authorizationTypeDataProvider')]
99
public function testUserIsThrottledIfInvalidCredentialsAreProvided()
100
{
101
for ($i = 0; $i <= 10; ++$i) {
102
$this->postJson('/api/remote/sftp/auth', [
103
'type' => 'public_key',
104
'username' => $i % 2 === 0 ? $this->user->username : $this->getUsername(),
105
'password' => 'invalid key',
106
])
107
->assertStatus($i === 10 ? 429 : 403);
108
}
109
}
110
111
/**
112
* Test that the user is not throttled so long as a valid public key is provided, even
113
* if it doesn't actually exist in the database for the user.
114
*/
115
public function testUserIsNotThrottledIfNoPublicKeyMatches()
116
{
117
for ($i = 0; $i <= 10; ++$i) {
118
$this->postJson('/api/remote/sftp/auth', [
119
'type' => 'public_key',
120
'username' => $this->getUsername(),
121
'password' => EC::createKey('Ed25519')->getPublicKey()->toString('OpenSSH'),
122
])
123
->assertForbidden();
124
}
125
}
126
127
/**
128
* Test that a request is rejected if the credentials are valid but the username indicates
129
* a server on a different node.
130
*/
131
#[\PHPUnit\Framework\Attributes\DataProvider('authorizationTypeDataProvider')]
132
public function testRequestIsRejectedIfServerBelongsToDifferentNode(string $type)
133
{
134
$node2 = $this->createServerModel()->node;
135
136
$this->setAuthorization($node2);
137
138
$password = $type === 'public_key'
139
? UserSSHKey::factory()->for($this->user)->create()->public_key
140
: 'foobar';
141
142
$this->postJson('/api/remote/sftp/auth', [
143
'type' => 'public_key',
144
'username' => $this->getUsername(),
145
'password' => $password,
146
])
147
->assertForbidden();
148
}
149
150
public function testRequestIsDeniedIfUserLacksSftpPermission()
151
{
152
[$user, $server] = $this->generateTestAccount([Permission::ACTION_FILE_READ]);
153
154
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
155
156
$this->setAuthorization($server->node);
157
158
$this->postJson('/api/remote/sftp/auth', [
159
'username' => $user->username . '.' . $server->uuidShort,
160
'password' => 'foobar',
161
])
162
->assertForbidden()
163
->assertJsonPath('errors.0.detail', 'You do not have permission to access SFTP for this server.');
164
}
165
166
#[\PHPUnit\Framework\Attributes\DataProvider('serverStateDataProvider')]
167
public function testInvalidServerStateReturnsConflictError(string $status)
168
{
169
$this->server->update(['status' => $status]);
170
171
$this->postJson('/api/remote/sftp/auth', ['username' => $this->getUsername(), 'password' => 'foobar'])
172
->assertStatus(409);
173
}
174
175
/**
176
* Test that permissions are returned for the user account correctly.
177
*/
178
public function testUserPermissionsAreReturnedCorrectly()
179
{
180
[$user, $server] = $this->generateTestAccount([Permission::ACTION_FILE_READ, Permission::ACTION_FILE_SFTP]);
181
182
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
183
184
$this->setAuthorization($server->node);
185
186
$data = [
187
'username' => $user->username . '.' . $server->uuidShort,
188
'password' => 'foobar',
189
];
190
191
$this->postJson('/api/remote/sftp/auth', $data)
192
->assertOk()
193
->assertJsonPath('permissions', [Permission::ACTION_FILE_READ, Permission::ACTION_FILE_SFTP]);
194
195
$user->update(['root_admin' => true]);
196
197
$this->postJson('/api/remote/sftp/auth', $data)
198
->assertOk()
199
->assertJsonPath('permissions.0', '*');
200
201
$this->setAuthorization();
202
$data['username'] = $user->username . '.' . $this->server->uuidShort;
203
204
$this->post('/api/remote/sftp/auth', $data)
205
->assertOk()
206
->assertJsonPath('permissions.0', '*');
207
208
$user->update(['root_admin' => false]);
209
$this->post('/api/remote/sftp/auth', $data)->assertForbidden();
210
}
211
212
public static function authorizationTypeDataProvider(): array
213
{
214
return [
215
'password auth' => ['password'],
216
'public key auth' => ['public_key'],
217
];
218
}
219
220
public static function serverStateDataProvider(): array
221
{
222
return [
223
'installing' => [Server::STATUS_INSTALLING],
224
'suspended' => [Server::STATUS_SUSPENDED],
225
'restoring a backup' => [Server::STATUS_RESTORING_BACKUP],
226
];
227
}
228
229
/**
230
* Returns the username for connecting to SFTP.
231
*/
232
protected function getUsername(bool $long = false): string
233
{
234
return $this->user->username . '.' . ($long ? $this->server->uuid : $this->server->uuidShort);
235
}
236
237
/**
238
* Sets the authorization header for the rest of the test.
239
*/
240
protected function setAuthorization(?Node $node = null): void
241
{
242
$node = $node ?? $this->server->node;
243
244
$this->withHeader('Authorization', 'Bearer ' . $node->daemon_token_id . '.' . decrypt($node->daemon_token));
245
}
246
}
247
248