diff --git a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php index 832e426e2..adfa4be19 100644 --- a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php +++ b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php @@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; use Carbon\Carbon; use Illuminate\Http\Request; +use Pterodactyl\Models\Task; use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\Schedule; @@ -11,6 +12,7 @@ use Illuminate\Http\JsonResponse; use Pterodactyl\Facades\Activity; use Pterodactyl\Helpers\Utilities; use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Exceptions\Http\HttpForbiddenException; use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Services\Schedules\ProcessScheduleService; use Pterodactyl\Transformers\Api\Client\ScheduleTransformer; @@ -143,6 +145,8 @@ class ScheduleController extends ClientApiController */ public function execute(TriggerScheduleRequest $request, Server $server, Schedule $schedule): JsonResponse { + $this->authorizeTasks($request, $server, $schedule); + $this->service->handle($schedule, true); Activity::event('server:schedule.execute')->subject($schedule)->property('name', $schedule->name)->log(); @@ -150,6 +154,17 @@ class ScheduleController extends ClientApiController return new JsonResponse([], JsonResponse::HTTP_ACCEPTED); } + private function authorizeTasks(TriggerScheduleRequest $request, Server $server, Schedule $schedule): void + { + foreach ($schedule->tasks as $task) { + $permission = Task::permissionForAction($task->action, $task->payload); + + if (is_null($permission) || !$request->user()->can($permission, $server)) { + throw new HttpForbiddenException('You do not have permission to perform this action.'); + } + } + } + /** * Deletes a schedule and it's associated tasks. */ diff --git a/app/Http/Requests/Api/Client/Servers/Schedules/StoreTaskRequest.php b/app/Http/Requests/Api/Client/Servers/Schedules/StoreTaskRequest.php index 5ceb7c8e0..264bf0e42 100644 --- a/app/Http/Requests/Api/Client/Servers/Schedules/StoreTaskRequest.php +++ b/app/Http/Requests/Api/Client/Servers/Schedules/StoreTaskRequest.php @@ -2,25 +2,47 @@ namespace Pterodactyl\Http\Requests\Api\Client\Servers\Schedules; +use Pterodactyl\Models\Task; +use Pterodactyl\Models\Server; +use Illuminate\Validation\Rule; use Pterodactyl\Models\Permission; class StoreTaskRequest extends ViewScheduleRequest { /** - * Determine if the user is allowed to create a new task for this schedule. We simply - * check if they can modify a schedule to determine if they're able to do this. There - * are no task specific permissions. + * Determine if the user is allowed to create or update a task for this schedule. */ public function permission(): string { return Permission::ACTION_SCHEDULE_UPDATE; } + public function authorize(): bool + { + if (!parent::authorize()) { + return false; + } + + $permission = Task::permissionForAction((string) $this->input('action'), $this->input('payload')); + if (is_null($permission)) { + return true; + } + + $server = $this->route()->parameter('server'); + + return $server instanceof Server && $this->user()->can($permission, $server); + } + public function rules(): array { return [ 'action' => 'required|in:command,power,backup', - 'payload' => 'required_unless:action,backup|string|nullable', + 'payload' => [ + 'required_unless:action,backup', + 'string', + 'nullable', + Rule::when($this->input('action') === Task::ACTION_POWER, [Rule::in(Task::POWER_ACTIONS)]), + ], 'time_offset' => 'required|numeric|min:0|max:900', 'sequence_id' => 'sometimes|required|numeric|min:1', 'continue_on_failure' => 'sometimes|required|boolean', diff --git a/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php b/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php index e071388cf..76b224b9a 100644 --- a/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php +++ b/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php @@ -2,7 +2,7 @@ namespace Pterodactyl\Http\Requests\Api\Client\Servers; -use Pterodactyl\Models\Permission; +use Pterodactyl\Models\Task; use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest; class SendPowerRequest extends ClientApiRequest @@ -12,17 +12,7 @@ class SendPowerRequest extends ClientApiRequest */ public function permission(): string { - switch ($this->input('signal')) { - case 'start': - return Permission::ACTION_CONTROL_START; - case 'stop': - case 'kill': - return Permission::ACTION_CONTROL_STOP; - case 'restart': - return Permission::ACTION_CONTROL_RESTART; - } - - return '__invalid'; + return Task::permissionForAction(Task::ACTION_POWER, $this->input('signal')) ?? '__invalid'; } /** @@ -31,7 +21,7 @@ class SendPowerRequest extends ClientApiRequest public function rules(): array { return [ - 'signal' => 'required|string|in:start,stop,restart,kill', + 'signal' => 'required|string|in:' . implode(',', Task::POWER_ACTIONS), ]; } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 50dab16eb..cc098d7b5 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -41,6 +41,7 @@ class Task extends Model public const ACTION_POWER = 'power'; public const ACTION_COMMAND = 'command'; public const ACTION_BACKUP = 'backup'; + public const POWER_ACTIONS = ['start', 'stop', 'restart', 'kill']; /** * The table associated with the model. @@ -96,6 +97,28 @@ class Task extends Model 'continue_on_failure' => 'boolean', ]; + public static function permissionForAction(string $action, ?string $payload = null): ?string + { + switch ($action) { + case self::ACTION_COMMAND: + return Permission::ACTION_CONTROL_CONSOLE; + case self::ACTION_BACKUP: + return Permission::ACTION_BACKUP_CREATE; + case self::ACTION_POWER: + switch (trim((string) $payload)) { + case 'start': + return Permission::ACTION_CONTROL_START; + case 'stop': + case 'kill': + return Permission::ACTION_CONTROL_STOP; + case 'restart': + return Permission::ACTION_CONTROL_RESTART; + } + } + + return null; + } + public function getRouteKeyName(): string { return $this->getKeyName(); diff --git a/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php index eafcf3c05..0f43ac965 100644 --- a/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php +++ b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php @@ -8,6 +8,7 @@ use Pterodactyl\Models\Schedule; use Pterodactyl\Models\Permission; use Illuminate\Support\Facades\Bus; use Pterodactyl\Jobs\Schedule\RunTaskJob; +use Pterodactyl\Repositories\Wings\DaemonCommandRepository; use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; class ExecuteScheduleTest extends ClientApiIntegrationTestCase @@ -63,8 +64,27 @@ class ExecuteScheduleTest extends ClientApiIntegrationTestCase $this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden(); } + public function testSubuserCannotExecuteScheduleWithoutTaskActionPermission() + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]); + + $mock = $this->mock(DaemonCommandRepository::class); + $mock->shouldNotReceive('setServer'); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + Task::factory()->create([ + 'schedule_id' => $schedule->id, + 'sequence_id' => 1, + 'action' => 'command', + 'payload' => 'say Test', + ]); + + $this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden(); + } + public static function permissionsDataProvider(): array { - return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]]; + return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE, Permission::ACTION_CONTROL_CONSOLE]]]; } } diff --git a/tests/Integration/Api/Client/Server/ScheduleTask/CreateServerScheduleTaskTest.php b/tests/Integration/Api/Client/Server/ScheduleTask/CreateServerScheduleTaskTest.php index c2d047a2b..f7810814c 100644 --- a/tests/Integration/Api/Client/Server/ScheduleTask/CreateServerScheduleTaskTest.php +++ b/tests/Integration/Api/Client/Server/ScheduleTask/CreateServerScheduleTaskTest.php @@ -112,6 +112,65 @@ class CreateServerScheduleTaskTest extends ClientApiIntegrationTestCase ->assertJsonPath('errors.0.detail', 'A backup task cannot be created when the server\'s backup limit is set to 0.'); } + #[\PHPUnit\Framework\Attributes\DataProvider('missingActionPermissionDataProvider')] + public function testTaskCannotBeCreatedWithoutActionPermission(string $action, ?string $payload) + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]); + $server->forceFill(['backup_limit' => 1])->save(); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ + 'action' => $action, + 'payload' => $payload, + 'time_offset' => 0, + ])->assertForbidden(); + } + + #[\PHPUnit\Framework\Attributes\DataProvider('actionPermissionDataProvider')] + public function testTaskCanBeCreatedWithActionPermission(string $action, ?string $payload, string $permission) + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE, $permission]); + $server->forceFill(['backup_limit' => 1])->save(); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + + $response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ + 'action' => $action, + 'payload' => $payload, + 'time_offset' => 0, + ]); + + $response->assertOk(); + $this->assertDatabaseHas('tasks', [ + 'schedule_id' => $schedule->id, + 'action' => $action, + 'payload' => $payload ?? '', + ]); + } + + public function testPowerTaskRequiresValidPayload() + { + [$user, $server] = $this->generateTestAccount([ + Permission::ACTION_SCHEDULE_UPDATE, + Permission::ACTION_CONTROL_START, + ]); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ + 'action' => 'power', + 'payload' => 'invalid', + 'time_offset' => 0, + ]) + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) + ->assertJsonPath('errors.0.meta.rule', 'in') + ->assertJsonPath('errors.0.meta.source_field', 'payload'); + } + /** * Test that an error is returned if the user attempts to create an additional task that * would put the schedule over the task limit. @@ -171,6 +230,30 @@ class CreateServerScheduleTaskTest extends ClientApiIntegrationTestCase public static function permissionsDataProvider(): array { - return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]]; + return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE, Permission::ACTION_CONTROL_CONSOLE]]]; + } + + public static function missingActionPermissionDataProvider(): array + { + return [ + ['command', 'say Test'], + ['power', 'start'], + ['power', 'stop'], + ['power', 'restart'], + ['power', 'kill'], + ['backup', null], + ]; + } + + public static function actionPermissionDataProvider(): array + { + return [ + ['command', 'say Test', Permission::ACTION_CONTROL_CONSOLE], + ['power', 'start', Permission::ACTION_CONTROL_START], + ['power', 'stop', Permission::ACTION_CONTROL_STOP], + ['power', 'restart', Permission::ACTION_CONTROL_RESTART], + ['power', 'kill', Permission::ACTION_CONTROL_STOP], + ['backup', null, Permission::ACTION_BACKUP_CREATE], + ]; } } diff --git a/tests/Integration/Api/Client/Server/ScheduleTask/UpdateServerScheduleTaskTest.php b/tests/Integration/Api/Client/Server/ScheduleTask/UpdateServerScheduleTaskTest.php new file mode 100644 index 000000000..284697bae --- /dev/null +++ b/tests/Integration/Api/Client/Server/ScheduleTask/UpdateServerScheduleTaskTest.php @@ -0,0 +1,89 @@ +generateTestAccount([ + Permission::ACTION_SCHEDULE_UPDATE, + Permission::ACTION_CONTROL_CONSOLE, + ]); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + /** @var Task $task */ + $task = Task::factory()->create([ + 'schedule_id' => $schedule->id, + 'action' => 'power', + 'payload' => 'start', + ]); + + $response = $this->actingAs($user)->postJson($this->link($task), [ + 'action' => 'command', + 'payload' => 'say Test', + 'time_offset' => 10, + ]); + + $response->assertOk(); + $task->refresh(); + + $this->assertSame('command', $task->action); + $this->assertSame('say Test', $task->payload); + $this->assertSame(10, $task->time_offset); + } + + public function testTaskCannotBeUpdatedWithoutActionPermission() + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + /** @var Task $task */ + $task = Task::factory()->create([ + 'schedule_id' => $schedule->id, + 'action' => 'power', + 'payload' => 'start', + ]); + + $this->actingAs($user)->postJson($this->link($task), [ + 'action' => 'command', + 'payload' => 'say Test', + 'time_offset' => 10, + ])->assertForbidden(); + + $task->refresh(); + + $this->assertSame('power', $task->action); + $this->assertSame('start', $task->payload); + } + + public function testPowerTaskRequiresValidPayload() + { + [$user, $server] = $this->generateTestAccount([ + Permission::ACTION_SCHEDULE_UPDATE, + Permission::ACTION_CONTROL_START, + ]); + + /** @var Schedule $schedule */ + $schedule = Schedule::factory()->create(['server_id' => $server->id]); + /** @var Task $task */ + $task = Task::factory()->create(['schedule_id' => $schedule->id]); + + $this->actingAs($user)->postJson($this->link($task), [ + 'action' => 'power', + 'payload' => 'invalid', + 'time_offset' => 0, + ]) + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) + ->assertJsonPath('errors.0.meta.rule', 'in') + ->assertJsonPath('errors.0.meta.source_field', 'payload'); + } +}