Skip to content

Commit a4f03f5

Browse files
committed
Handle missing daemon keys better and fix subuser missing key errors
1 parent 18e394e commit a4f03f5

File tree

6 files changed

+74
-49
lines changed

6 files changed

+74
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
88
* Fixes application API keys being created as a client API key.
99
* Search term is now passed through when using paginated result sets.
1010
* Reduces the number of SQL queries executed when rendering the server listing to increase performance.
11+
* Fixes exceptions being thrown for non-existent subuser permissions.
1112

1213
### Changed
1314
* Databases are now properly paginated when viewing a database host.
1415
* No more loading daemon keys for every server model being loaded, some of us value our databases.
16+
* Changed behavior of the subuser middleware to add a daemon access key if one is missing from the database for some reason.
1517

1618
## v0.7.4-h1 (Derelict Dermodactylus)
1719
### Fixed

app/Http/Middleware/Server/AuthenticateAsSubuser.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Closure;
1313
use Illuminate\Http\Request;
14-
use Illuminate\Contracts\Session\Session;
1514
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
1615
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1716
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@@ -23,21 +22,14 @@ class AuthenticateAsSubuser
2322
*/
2423
private $keyProviderService;
2524

26-
/**
27-
* @var \Illuminate\Contracts\Session\Session
28-
*/
29-
private $session;
30-
3125
/**
3226
* SubuserAccessAuthenticate constructor.
3327
*
3428
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
35-
* @param \Illuminate\Contracts\Session\Session $session
3629
*/
37-
public function __construct(DaemonKeyProviderService $keyProviderService, Session $session)
30+
public function __construct(DaemonKeyProviderService $keyProviderService)
3831
{
3932
$this->keyProviderService = $keyProviderService;
40-
$this->session = $session;
4133
}
4234

4335
/**
@@ -60,7 +52,6 @@ public function handle(Request $request, Closure $next)
6052
throw new AccessDeniedHttpException('This account does not have permission to access this server.');
6153
}
6254

63-
$this->session->now('server_data.token', $token);
6455
$request->attributes->set('server_token', $token);
6556

6657
return $next($request);

app/Services/DaemonKeys/DaemonKeyProviderService.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Pterodactyl\Models\User;
2929
use Pterodactyl\Models\Server;
3030
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
31+
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
3132
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
3233

3334
class DaemonKeyProviderService
@@ -47,21 +48,29 @@ class DaemonKeyProviderService
4748
*/
4849
private $repository;
4950

51+
/**
52+
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
53+
*/
54+
private $subuserRepository;
55+
5056
/**
5157
* GetDaemonKeyService constructor.
5258
*
5359
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
5460
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
5561
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService $keyUpdateService
62+
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository
5663
*/
5764
public function __construct(
5865
DaemonKeyCreationService $keyCreationService,
5966
DaemonKeyRepositoryInterface $repository,
60-
DaemonKeyUpdateService $keyUpdateService
67+
DaemonKeyUpdateService $keyUpdateService,
68+
SubuserRepositoryInterface $subuserRepository
6169
) {
6270
$this->keyCreationService = $keyCreationService;
6371
$this->keyUpdateService = $keyUpdateService;
6472
$this->repository = $repository;
73+
$this->subuserRepository = $subuserRepository;
6574
}
6675

6776
/**
@@ -89,10 +98,18 @@ public function handle(Server $server, User $user, $updateIfExpired = true): str
8998
return $this->keyCreationService->handle($server->id, $user->id);
9099
}
91100

92-
// If they aren't the admin or owner of the server, they shouldn't get access.
93-
// Subusers should always have an entry created when they are, so if there is
94-
// no record, it should fail.
95-
throw $exception;
101+
// Check if user is a subuser for this server. Ideally they should always have
102+
// a record associated with them in the database, but we should still handle
103+
// that potentiality here.
104+
//
105+
// If no subuser is found, a RecordNotFoundException will be thrown, thus handling
106+
// the parent error as well.
107+
$subuser = $this->subuserRepository->findFirstWhere([
108+
['user_id', '=', $user->id],
109+
['server_id', '=', $server->id],
110+
]);
111+
112+
return $this->keyCreationService->handle($subuser->server_id, $subuser->user_id);
96113
}
97114

98115
if (! $updateIfExpired || Carbon::now()->diffInSeconds($key->expires_at, false) > 0) {

app/Transformers/Daemon/ApiKeyTransformer.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
<?php
2-
/*
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* The above copyright notice and this permission notice shall be included in all
14-
* copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
* SOFTWARE.
23-
*/
242

253
namespace Pterodactyl\Transformers\Daemon;
264

@@ -83,8 +61,8 @@ public function transform(DaemonKey $key)
8361
$daemonPermissions = ['s:console'];
8462

8563
foreach ($permissions as $permission) {
86-
if (! is_null($mappings[$permission])) {
87-
$daemonPermissions[] = $mappings[$permission];
64+
if (! is_null(array_get($mappings, $permission))) {
65+
$daemonPermissions[] = array_get($mappings, $permission);
8866
}
8967
}
9068

tests/Unit/Http/Middleware/Server/AuthenticateAsSubuserTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Mockery as m;
66
use Pterodactyl\Models\Server;
7-
use Illuminate\Contracts\Session\Session;
87
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
98
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
109
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
@@ -17,11 +16,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase
1716
*/
1817
private $keyProviderService;
1918

20-
/**
21-
* @var \Illuminate\Contracts\Session\Session|\Mockery\Mock
22-
*/
23-
private $session;
24-
2519
/**
2620
* Setup tests.
2721
*/
@@ -30,7 +24,6 @@ public function setUp()
3024
parent::setUp();
3125

3226
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
33-
$this->session = m::mock(Session::class);
3427
}
3528

3629
/**
@@ -43,7 +36,6 @@ public function testSuccessfulMiddleware()
4336
$this->setRequestAttribute('server', $model);
4437

4538
$this->keyProviderService->shouldReceive('handle')->with($model, $user)->once()->andReturn('abc123');
46-
$this->session->shouldReceive('now')->with('server_data.token', 'abc123')->once()->andReturnNull();
4739

4840
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
4941
$this->assertRequestHasAttribute('server_token');
@@ -74,6 +66,6 @@ public function testExceptionIsThrownIfNoTokenIsFound()
7466
*/
7567
public function getMiddleware(): AuthenticateAsSubuser
7668
{
77-
return new AuthenticateAsSubuser($this->keyProviderService, $this->session);
69+
return new AuthenticateAsSubuser($this->keyProviderService);
7870
}
7971
}

tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use Tests\TestCase;
1515
use Pterodactyl\Models\User;
1616
use Pterodactyl\Models\Server;
17+
use Pterodactyl\Models\Subuser;
1718
use Pterodactyl\Models\DaemonKey;
1819
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
1920
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
2021
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
2122
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
23+
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
2224
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
2325

2426
class DaemonKeyProviderServiceTest extends TestCase
@@ -38,6 +40,11 @@ class DaemonKeyProviderServiceTest extends TestCase
3840
*/
3941
private $repository;
4042

43+
/**
44+
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
45+
*/
46+
private $subuserRepository;
47+
4148
/**
4249
* Setup tests.
4350
*/
@@ -49,6 +56,7 @@ public function setUp()
4956
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
5057
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
5158
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
59+
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
5260
}
5361

5462
/**
@@ -154,6 +162,33 @@ public function testMissingKeyIsCreatedIfUserIsServerOwner()
154162
$this->assertEquals($key->secret, $response);
155163
}
156164

165+
/**
166+
* Test that a missing key is created for a subuser.
167+
*/
168+
public function testMissingKeyIsCreatedForSubuser()
169+
{
170+
$user = factory(User::class)->make(['root_admin' => 0]);
171+
$server = factory(Server::class)->make();
172+
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
173+
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
174+
175+
$this->repository->shouldReceive('findFirstWhere')->with([
176+
['user_id', '=', $user->id],
177+
['server_id', '=', $server->id],
178+
])->once()->andThrow(new RecordNotFoundException);
179+
180+
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
181+
['user_id', '=', $user->id],
182+
['server_id', '=', $server->id],
183+
])->andReturn($subuser);
184+
185+
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
186+
187+
$response = $this->getService()->handle($server, $user, false);
188+
$this->assertNotEmpty($response);
189+
$this->assertEquals($key->secret, $response);
190+
}
191+
157192
/**
158193
* Test that an exception is thrown if the user should not get a key.
159194
*
@@ -169,6 +204,11 @@ public function testExceptionIsThrownIfUserDoesNotDeserveKey()
169204
['server_id', '=', $server->id],
170205
])->once()->andThrow(new RecordNotFoundException);
171206

207+
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
208+
['user_id', '=', $user->id],
209+
['server_id', '=', $server->id],
210+
])->andThrow(new RecordNotFoundException);
211+
172212
$this->getService()->handle($server, $user, false);
173213
}
174214

@@ -179,6 +219,11 @@ public function testExceptionIsThrownIfUserDoesNotDeserveKey()
179219
*/
180220
private function getService(): DaemonKeyProviderService
181221
{
182-
return new DaemonKeyProviderService($this->keyCreationService, $this->repository, $this->keyUpdateService);
222+
return new DaemonKeyProviderService(
223+
$this->keyCreationService,
224+
$this->repository,
225+
$this->keyUpdateService,
226+
$this->subuserRepository
227+
);
183228
}
184229
}

0 commit comments

Comments
 (0)