Skip to content

Commit 03a497f

Browse files
committed
Use a post request to delete SSH keys, some hashes use slashes which cause 404 errors; closes pterodactyl#4100
1 parent 5143faa commit 03a497f

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,22 @@ public function store(StoreSSHKeyRequest $request): array
4545
/**
4646
* Deletes an SSH key from the user's account.
4747
*/
48-
public function delete(ClientApiRequest $request, string $identifier): JsonResponse
48+
public function delete(ClientApiRequest $request): JsonResponse
4949
{
50-
$key = $request->user()->sshKeys()->where('fingerprint', $identifier)->firstOrFail();
50+
$this->validate($request, ['fingerprint' => ['required', 'string']]);
5151

52-
$key->delete();
52+
$key = $request->user()->sshKeys()
53+
->where('fingerprint', $request->input('fingerprint'))
54+
->first();
5355

54-
Activity::event('user:ssh-key.delete')
55-
->subject($key)
56-
->property('fingerprint', $key->fingerprint)
57-
->log();
56+
if (!is_null($key)) {
57+
$key->delete();
58+
59+
Activity::event('user:ssh-key.delete')
60+
->subject($key)
61+
->property('fingerprint', $key->fingerprint)
62+
->log();
63+
}
5864

5965
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
6066
}

resources/scripts/api/account/ssh-keys.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ const createSSHKey = async (name: string, publicKey: string): Promise<SSHKey> =>
2323
};
2424

2525
const deleteSSHKey = async (fingerprint: string): Promise<void> =>
26-
await http.delete(`/api/client/account/ssh-keys/${fingerprint}`);
26+
await http.post('/api/client/account/ssh-keys/remove', { fingerprint });
2727

2828
export { useSSHKeys, createSSHKey, deleteSSHKey };

routes/api-client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
Route::prefix('/ssh-keys')->group(function () {
4040
Route::get('/', [Client\SSHKeyController::class, 'index']);
4141
Route::post('/', [Client\SSHKeyController::class, 'store']);
42-
Route::delete('/{identifier}', [Client\SSHKeyController::class, 'delete']);
42+
Route::post('/remove', [Client\SSHKeyController::class, 'delete']);
4343
});
4444
});
4545

tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Pterodactyl\Models\Schedule;
1515
use Illuminate\Support\Collection;
1616
use Pterodactyl\Models\Allocation;
17-
use Pterodactyl\Models\UserSSHKey;
1817
use Pterodactyl\Models\DatabaseHost;
1918
use Pterodactyl\Tests\Integration\TestResponse;
2019
use Pterodactyl\Tests\Integration\IntegrationTestCase;
@@ -60,7 +59,6 @@ protected function createTestResponse($response)
6059
*/
6160
protected function link($model, $append = null): string
6261
{
63-
$link = '';
6462
switch (get_class($model)) {
6563
case Server::class:
6664
$link = "/api/client/servers/{$model->uuid}";
@@ -77,9 +75,6 @@ protected function link($model, $append = null): string
7775
case Backup::class:
7876
$link = "/api/client/servers/{$model->server->uuid}/backups/{$model->uuid}";
7977
break;
80-
case UserSSHKey::class:
81-
$link = "/api/client/account/ssh-keys/$model->fingerprint";
82-
break;
8378
default:
8479
throw new InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
8580
}

tests/Integration/Api/Client/SSHKeyControllerTest.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ public function testSSHKeyCanBeDeleted()
4040
$key = UserSSHKey::factory()->for($user)->create();
4141
$key2 = UserSSHKey::factory()->for($user2)->create();
4242

43+
$endpoint = '/api/client/account/ssh-keys/remove';
44+
4345
$this->actingAs($user);
44-
$this->deleteJson($this->link($key))->assertNoContent();
46+
$this->postJson($endpoint)
47+
->assertUnprocessable()
48+
->assertJsonPath('errors.0.meta', ['source_field' => 'fingerprint', 'rule' => 'required']);
49+
50+
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
4551

4652
$this->assertSoftDeleted($key);
4753
$this->assertNotSoftDeleted($key2);
4854

49-
$this->deleteJson($this->link($key))->assertNotFound();
50-
$this->deleteJson($this->link($key2))->assertNotFound();
55+
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
56+
$this->postJson($endpoint, ['fingerprint' => $key2->fingerprint])->assertNoContent();
5157

5258
$this->assertNotSoftDeleted($key2);
5359
}

0 commit comments

Comments
 (0)