2021-01-20 05:20:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
|
|
|
|
|
|
|
|
use Mockery;
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\Subuser;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
|
|
|
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
|
|
|
|
|
|
|
class SubuserAuthorizationTest extends ClientApiIntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test that mismatched subusers are not accessible to a server.
|
|
|
|
*
|
|
|
|
* @dataProvider methodDataProvider
|
|
|
|
*/
|
|
|
|
public function testUserCannotAccessResourceBelongingToOtherServers(string $method)
|
|
|
|
{
|
|
|
|
// Generic subuser, the specific resource we're trying to access.
|
|
|
|
/** @var \Pterodactyl\Models\User $internal */
|
2021-01-23 20:09:16 +00:00
|
|
|
$internal = User::factory()->create();
|
2021-01-20 05:20:55 +00:00
|
|
|
|
|
|
|
// The API $user is the owner of $server1.
|
|
|
|
[$user, $server1] = $this->generateTestAccount();
|
|
|
|
// Will be a subuser of $server2.
|
|
|
|
$server2 = $this->createServerModel();
|
|
|
|
// And as no access to $server3.
|
|
|
|
$server3 = $this->createServerModel();
|
|
|
|
|
|
|
|
// Set the API $user as a subuser of server 2, but with no permissions
|
|
|
|
// to do anything with the subusers for that server.
|
2021-01-23 20:09:16 +00:00
|
|
|
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);
|
2021-01-20 05:20:55 +00:00
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);
|
|
|
|
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);
|
|
|
|
Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);
|
2021-01-20 05:20:55 +00:00
|
|
|
|
|
|
|
$this->instance(DaemonServerRepository::class, $mock = Mockery::mock(DaemonServerRepository::class));
|
|
|
|
if ($method === 'DELETE') {
|
|
|
|
$mock->expects('setServer->revokeUserJTI')->with($internal->id)->andReturnUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This route is acceptable since they're accessing a subuser on their own server.
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->actingAs($user)->json($method, $this->link($server1, '/users/' . $internal->uuid))->assertStatus($method === 'POST' ? 422 : ($method === 'DELETE' ? 204 : 200));
|
2021-01-20 05:20:55 +00:00
|
|
|
|
|
|
|
// This route can be revealed since the subuser belongs to the correct server, but
|
|
|
|
// errors out with a 403 since $user does not have the right permissions for this.
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->actingAs($user)->json($method, $this->link($server2, '/users/' . $internal->uuid))->assertForbidden();
|
|
|
|
$this->actingAs($user)->json($method, $this->link($server3, '/users/' . $internal->uuid))->assertNotFound();
|
2021-01-20 05:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function methodDataProvider(): array
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
return [['GET'], ['POST'], ['DELETE']];
|
2021-01-20 05:20:55 +00:00
|
|
|
}
|
|
|
|
}
|