fix issue where saving a subuser with more permissions than you would strip their permissions

This commit is contained in:
Sky Mulley
2026-10-06 01:17:42 +01:00
parent 7c29f38e01
commit 90fc8c7005
5 changed files with 153 additions and 8 deletions
@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers;
use Illuminate\Contracts\Container\BindingResolutionException;
use Pterodactyl\Enum\Permissions;
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
class DeleteSubuserRequest extends SubuserRequest
{
@@ -12,4 +14,24 @@ class DeleteSubuserRequest extends SubuserRequest
{
return Permissions::UserDelete->value;
}
/**
* @throws BindingResolutionException
*/
public function authorize(): bool
{
if (! parent::authorize()) {
return false;
}
if ($this->requesterHasFullAccess()) {
return true;
}
$exceeding = array_diff($this->subuser()->permissions, $this->requesterPermissions(), [Permissions::WebsocketConnect->value]);
throw_if(count($exceeding) > 0, HttpForbiddenException::class, 'Cannot remove a subuser that has permissions your account does not actively possess.');
return true;
}
}
@@ -85,20 +85,42 @@ abstract class SubuserRequest extends ClientApiRequest implements ClientPermissi
*/
protected function validatePermissionsCanBeAssigned(array $permissions): void
{
$user = $this->user();
$server = $this->parameter('server', Server::class);
// If we are a root admin or the server owner, no need to perform these checks.
if ($user->root_admin || $user->id === $server->owner_id) {
if ($this->requesterHasFullAccess()) {
return;
}
// Otherwise, get the current subuser's permission set, and ensure that the
// permissions they are trying to assign are not _more_ than the ones they
// already have.
$allowed = $this->requesterPermissions();
$target = $this->attributes->get('subuser');
if ($target instanceof Subuser) {
$allowed = array_merge($allowed, $target->permissions);
}
throw_if(count(array_diff($permissions, $allowed)) > 0, HttpForbiddenException::class, 'Cannot assign permissions to a subuser that your account does not actively possess.');
}
/**
* Whether the requester is a root admin or the server owner, who may manage any subuser.
*/
protected function requesterHasFullAccess(): bool
{
$user = $this->user();
return $user->root_admin || $user->id === $this->parameter('server', Server::class)->owner_id;
}
/**
* The permissions the requester holds on this server as a subuser.
*
* @return list<string>
*
* @throws BindingResolutionException
*/
protected function requesterPermissions(): array
{
$service = $this->container->make(GetUserPermissionsService::class);
throw_if(count(array_diff($permissions, $service->handle($server, $user))) > 0, HttpForbiddenException::class, 'Cannot assign permissions to a subuser that your account does not actively possess.');
return array_values($service->handle($this->parameter('server', Server::class), $this->user()));
}
/** @return list<string> */
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers;
use Illuminate\Contracts\Container\BindingResolutionException;
use Pterodactyl\Enum\Permissions;
class UpdateSubuserRequest extends SubuserRequest
@@ -23,4 +24,26 @@ class UpdateSubuserRequest extends SubuserRequest
'permissions.*' => ['string'],
];
}
/**
* @return list<string>
*
* @throws BindingResolutionException
*/
public function permissions(): array
{
$requested = parent::permissions();
if ($this->requesterHasFullAccess()) {
return $requested;
}
$held = $this->requesterPermissions();
$current = $this->subuser()->permissions;
return array_values(array_unique(array_merge(
array_intersect($requested, $held),
array_diff($current, $held),
[Permissions::WebsocketConnect->value],
)));
}
}
@@ -32,3 +32,29 @@ test('correct subuser is deleted from server', function (?string $prefix) {
'without a numeric prefix' => [null],
'with a numeric prefix' => ['18180000'],
]);
test('subuser cannot delete a subuser holding permissions they do not have', function () {
Bus::fake([RevokeSftpAccessJob::class]);
[$user, $server] = $this->generateTestAccount([Permissions::UserDelete->value, Permissions::ControlStart->value]);
/** @var Subuser $target */
$target = Subuser::factory()->for(User::factory()->create())->for($server)->create(['permissions' => [
Permissions::ControlStart->value,
Permissions::ControlConsole->value,
Permissions::WebsocketConnect->value,
]]);
$this->actingAs($user)->deleteJson($this->link($server)."/users/{$target->user->uuid}")->assertForbidden();
expect(Subuser::query()->whereKey($target->id)->exists())->toBeTrue();
Bus::assertNotDispatched(RevokeSftpAccessJob::class);
});
test('subuser can delete a subuser whose permissions are a subset of their own', function () {
Bus::fake([RevokeSftpAccessJob::class]);
[$user, $server] = $this->generateTestAccount([Permissions::UserDelete->value, Permissions::ControlStart->value, Permissions::ControlConsole->value]);
/** @var Subuser $target */
$target = Subuser::factory()->for(User::factory()->create())->for($server)->create(['permissions' => [
Permissions::ControlStart->value,
Permissions::WebsocketConnect->value,
]]);
$this->actingAs($user)->deleteJson($this->link($server)."/users/{$target->user->uuid}")->assertNoContent();
expect(Subuser::query()->whereKey($target->id)->exists())->toBeFalse();
});
@@ -54,3 +54,55 @@ test('cannot update subuser for different server', function () {
[$user2] = $this->generateTestAccount(['foo.bar']);
$this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/users/{$user2->uuid}", [])->assertNotFound();
});
test('subuser cannot strip permissions from a subuser that they do not hold themselves', function () {
Bus::fake([RevokeSftpAccessJob::class]);
[$user, $server] = $this->generateTestAccount([Permissions::UserUpdate->value, Permissions::ControlStart->value]);
/** @var Subuser $subuser */
$subuser = Subuser::factory()->for(User::factory()->create())->for($server)->create(['permissions' => [
Permissions::ControlConsole->value,
Permissions::FileUpdate->value,
Permissions::ControlStart->value,
Permissions::WebsocketConnect->value,
]]);
$this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/users/{$subuser->user->uuid}", ['permissions' => [Permissions::ControlStart->value]])->assertOk();
expect($subuser->refresh()->permissions)->toEqualCanonicalizing([
Permissions::ControlConsole->value,
Permissions::FileUpdate->value,
Permissions::ControlStart->value,
Permissions::WebsocketConnect->value,
]);
});
test('subuser can change the permissions they hold while preserving the ones they do not', function () {
Bus::fake([RevokeSftpAccessJob::class]);
[$user, $server] = $this->generateTestAccount([Permissions::UserUpdate->value, Permissions::ControlStart->value, Permissions::ControlStop->value]);
/** @var Subuser $subuser */
$subuser = Subuser::factory()->for(User::factory()->create())->for($server)->create(['permissions' => [
Permissions::ControlConsole->value,
Permissions::ControlStart->value,
Permissions::WebsocketConnect->value,
]]);
$this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/users/{$subuser->user->uuid}", ['permissions' => [
Permissions::ControlConsole->value,
Permissions::ControlStop->value,
Permissions::WebsocketConnect->value,
]])->assertOk();
expect($subuser->refresh()->permissions)->toEqualCanonicalizing([
Permissions::ControlConsole->value,
Permissions::ControlStop->value,
Permissions::WebsocketConnect->value,
]);
$this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/users/{$subuser->user->uuid}", ['permissions' => [
Permissions::ControlStop->value,
Permissions::FileRead->value,
]])->assertForbidden();
expect($subuser->refresh()->permissions)->toEqualCanonicalizing([
Permissions::ControlConsole->value,
Permissions::ControlStop->value,
Permissions::WebsocketConnect->value,
]);
});