Skip to content

Commit 9b654d2

Browse files
committed
Fix bug with client API denying access to routes, closes pterodactyl#1366
1 parent b5c13f1 commit 9b654d2

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

app/Http/Controllers/Api/Client/Servers/CommandController.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Psr\Http\Message\ResponseInterface;
88
use GuzzleHttp\Exception\ClientException;
99
use GuzzleHttp\Exception\RequestException;
10-
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
1110
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1211
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
1312
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
@@ -16,11 +15,6 @@
1615

1716
class CommandController extends ClientApiController
1817
{
19-
/**
20-
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
21-
*/
22-
private $keyProviderService;
23-
2418
/**
2519
* @var \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface
2620
*/
@@ -30,13 +24,11 @@ class CommandController extends ClientApiController
3024
* CommandController constructor.
3125
*
3226
* @param \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface $repository
33-
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
3427
*/
35-
public function __construct(CommandRepositoryInterface $repository, DaemonKeyProviderService $keyProviderService)
28+
public function __construct(CommandRepositoryInterface $repository)
3629
{
3730
parent::__construct();
3831

39-
$this->keyProviderService = $keyProviderService;
4032
$this->repository = $repository;
4133
}
4234

@@ -46,14 +38,12 @@ public function __construct(CommandRepositoryInterface $repository, DaemonKeyPro
4638
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest $request
4739
* @return \Illuminate\Http\Response
4840
*
49-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
50-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5141
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
5242
*/
5343
public function index(SendCommandRequest $request): Response
5444
{
5545
$server = $request->getModel(Server::class);
56-
$token = $this->keyProviderService->handle($server, $request->user());
46+
$token = $request->attributes->get('server_token');
5747

5848
try {
5949
$this->repository->setServer($server)

app/Http/Controllers/Api/Client/Servers/PowerController.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
use Illuminate\Http\Response;
66
use Pterodactyl\Models\Server;
7-
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
87
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
98
use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest;
109
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
1110

1211
class PowerController extends ClientApiController
1312
{
14-
/**
15-
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
16-
*/
17-
private $keyProviderService;
18-
1913
/**
2014
* @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface
2115
*/
@@ -24,14 +18,12 @@ class PowerController extends ClientApiController
2418
/**
2519
* PowerController constructor.
2620
*
27-
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
2821
* @param \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface $repository
2922
*/
30-
public function __construct(DaemonKeyProviderService $keyProviderService, PowerRepositoryInterface $repository)
23+
public function __construct(PowerRepositoryInterface $repository)
3124
{
3225
parent::__construct();
3326

34-
$this->keyProviderService = $keyProviderService;
3527
$this->repository = $repository;
3628
}
3729

@@ -41,14 +33,12 @@ public function __construct(DaemonKeyProviderService $keyProviderService, PowerR
4133
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest $request
4234
* @return \Illuminate\Http\Response
4335
*
44-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
45-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4636
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
4737
*/
4838
public function index(SendPowerRequest $request): Response
4939
{
5040
$server = $request->getModel(Server::class);
51-
$token = $this->keyProviderService->handle($server, $request->user());
41+
$token = $request->attributes->get('server_token');
5242

5343
$this->repository->setServer($server)->setToken($token)->sendSignal($request->input('signal'));
5444

app/Http/Middleware/Api/Client/AuthenticateClientAccess.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,57 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7+
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
8+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
9+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
710
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
811

912
class AuthenticateClientAccess
1013
{
14+
/**
15+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
16+
*/
17+
private $keyProviderService;
18+
19+
/**
20+
* AuthenticateClientAccess constructor.
21+
*
22+
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
23+
*/
24+
public function __construct(DaemonKeyProviderService $keyProviderService)
25+
{
26+
$this->keyProviderService = $keyProviderService;
27+
}
28+
1129
/**
1230
* Authenticate that the currently authenticated user has permission
13-
* to access the specified server.
31+
* to access the specified server. This only checks that the user is an
32+
* admin, owner, or a subuser. You'll need to do more specific checks in
33+
* the API calls to determine if they can perform different actions.
1434
*
1535
* @param \Illuminate\Http\Request $request
1636
* @param \Closure $next
1737
* @return mixed
38+
*
39+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
1840
*/
1941
public function handle(Request $request, Closure $next)
2042
{
2143
if (is_null($request->user())) {
22-
throw new AccessDeniedHttpException('This account does not have permission to access this resource.');
44+
throw new AccessDeniedHttpException('A request must be made using an authenticated client.');
2345
}
2446

47+
/** @var \Pterodactyl\Models\Server $server */
48+
$server = $request->route()->parameter('server');
49+
50+
try {
51+
$token = $this->keyProviderService->handle($server, $request->user());
52+
} catch (RecordNotFoundException $exception) {
53+
throw new NotFoundHttpException('The requested server could not be located.');
54+
}
55+
56+
$request->attributes->set('server_token', $token);
57+
2558
return $next($request);
2659
}
2760
}

app/Http/Requests/Api/Client/Servers/GetServerRequest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Client\Servers;
44

5-
use Pterodactyl\Models\Server;
65
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
76

87
class GetServerRequest extends ClientApiRequest
@@ -18,14 +17,4 @@ public function authorize(): bool
1817
{
1918
return true;
2019
}
21-
22-
/**
23-
* Determine if the user should even know that this server exists.
24-
*
25-
* @return bool
26-
*/
27-
public function resourceExists(): bool
28-
{
29-
return $this->user()->can('view-server', $this->getModel(Server::class));
30-
}
3120
}

0 commit comments

Comments
 (0)