diff --git a/app/Http/Controllers/Admin/NodeAutoDeployController.php b/app/Http/Controllers/Admin/NodeAutoDeployController.php index 2be1a9111..3ae34d257 100644 --- a/app/Http/Controllers/Admin/NodeAutoDeployController.php +++ b/app/Http/Controllers/Admin/NodeAutoDeployController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use Pterodactyl\Models\Node; use Pterodactyl\Models\ApiKey; use Illuminate\Http\JsonResponse; +use Pterodactyl\Services\Acl\Api\AdminAcl; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; use Pterodactyl\Services\Api\KeyCreationService; @@ -23,7 +24,7 @@ class NodeAutoDeployController extends Controller /** * Generates a new API key for the logged-in user with only permission to read - * nodes, and returns that as the deployment key for a node. + * node configurations, and returns that as the deployment key for a node. * * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ @@ -31,18 +32,20 @@ class NodeAutoDeployController extends Controller { $key = ApiKey::query() ->where('user_id', $request->user()->id) + ->where('node_id', $node->id) ->where('key_type', ApiKey::TYPE_APPLICATION) - ->where('r_nodes', 1) + ->where('r_nodes', AdminAcl::READ_CONFIGURATION) ->first(); // We couldn't find a key that exists for this user with only permission for - // reading nodes. Go ahead and create it now. + // reading node configurations. Go ahead and create it now. if (!$key) { $key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([ 'user_id' => $request->user()->id, + 'node_id' => $node->id, 'memo' => 'Automatically generated node deployment key.', 'allowed_ips' => [], - ], ['r_nodes' => 1]); + ], ['r_nodes' => AdminAcl::READ_CONFIGURATION]); } return new JsonResponse([ diff --git a/app/Http/Requests/Admin/Api/StoreApplicationApiKeyRequest.php b/app/Http/Requests/Admin/Api/StoreApplicationApiKeyRequest.php index f451239e6..e5a5d1046 100644 --- a/app/Http/Requests/Admin/Api/StoreApplicationApiKeyRequest.php +++ b/app/Http/Requests/Admin/Api/StoreApplicationApiKeyRequest.php @@ -16,9 +16,13 @@ class StoreApplicationApiKeyRequest extends AdminFormRequest { $modelRules = ApiKey::getRules(); - return collect(AdminAcl::getResourceList())->mapWithKeys(function ($resource) use ($modelRules) { + $rules = collect(AdminAcl::getResourceList())->mapWithKeys(function ($resource) use ($modelRules) { return [AdminAcl::COLUMN_IDENTIFIER . $resource => $modelRules['r_' . $resource]]; })->merge(['memo' => $modelRules['memo']])->toArray(); + + $rules['r_' . AdminAcl::RESOURCE_NODES] = ['integer', 'in:0,1,3']; + + return $rules; } public function attributes(): array diff --git a/app/Http/Requests/Api/Application/Nodes/GetNodeConfigurationRequest.php b/app/Http/Requests/Api/Application/Nodes/GetNodeConfigurationRequest.php index e632698de..f7485f741 100644 --- a/app/Http/Requests/Api/Application/Nodes/GetNodeConfigurationRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/GetNodeConfigurationRequest.php @@ -2,9 +2,25 @@ namespace Pterodactyl\Http\Requests\Api\Application\Nodes; +use Pterodactyl\Models\Node; +use Pterodactyl\Models\ApiKey; use Pterodactyl\Services\Acl\Api\AdminAcl; class GetNodeConfigurationRequest extends GetNodesRequest { protected int $permission = AdminAcl::WRITE; + + public function authorize(): bool + { + $token = $this->user()->currentAccessToken(); + if ($token instanceof ApiKey && AdminAcl::check($token, $this->resource, AdminAcl::READ_CONFIGURATION)) { + $node = $this->parameter('node', Node::class); + + if ($token->node_id === $node->id) { + return true; + } + } + + return parent::authorize(); + } } diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 45ea6a1be..4505c3fcb 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; * * @property int $id * @property int $user_id + * @property int|null $node_id * @property int $key_type * @property string $identifier * @property string $token @@ -103,6 +104,7 @@ class ApiKey extends Model implements HasAbilities protected $casts = [ 'allowed_ips' => 'array', 'user_id' => 'int', + 'node_id' => 'int', 'last_used_at' => 'datetime', 'expires_at' => 'datetime', self::CREATED_AT => 'datetime', @@ -147,6 +149,7 @@ class ApiKey extends Model implements HasAbilities 'memo' => 'required|nullable|string|max:500', 'allowed_ips' => 'nullable|array', 'allowed_ips.*' => 'string', + 'node_id' => 'nullable|exists:nodes,id', 'last_used_at' => 'nullable|date', 'expires_at' => 'nullable|date', 'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3', @@ -156,7 +159,7 @@ class ApiKey extends Model implements HasAbilities 'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3', - 'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3', + 'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:7', 'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3', ]; diff --git a/app/Services/Acl/Api/AdminAcl.php b/app/Services/Acl/Api/AdminAcl.php index cee076e17..dfb43601c 100644 --- a/app/Services/Acl/Api/AdminAcl.php +++ b/app/Services/Acl/Api/AdminAcl.php @@ -19,6 +19,7 @@ class AdminAcl public const NONE = 0; public const READ = 1; public const WRITE = 2; + public const READ_CONFIGURATION = 4; /** * Resources that are available on the API and can contain a permissions diff --git a/database/migrations/2026_06_23_205500_add_node_id_to_api_keys_table.php b/database/migrations/2026_06_23_205500_add_node_id_to_api_keys_table.php new file mode 100644 index 000000000..59c1da398 --- /dev/null +++ b/database/migrations/2026_06_23_205500_add_node_id_to_api_keys_table.php @@ -0,0 +1,29 @@ +unsignedInteger('node_id')->nullable()->after('user_id'); + $table->foreign('node_id')->references('id')->on('nodes')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('api_keys', function (Blueprint $table) { + $table->dropForeign(['node_id']); + $table->dropColumn('node_id'); + }); + } +}; diff --git a/database/schema/mysql-schema.sql b/database/schema/mysql-schema.sql index bb03afc28..e53a1d5a7 100644 --- a/database/schema/mysql-schema.sql +++ b/database/schema/mysql-schema.sql @@ -63,6 +63,7 @@ DROP TABLE IF EXISTS `api_keys`; CREATE TABLE `api_keys` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(10) unsigned NOT NULL, + `node_id` int(10) unsigned DEFAULT NULL, `key_type` tinyint(3) unsigned NOT NULL DEFAULT 0, `identifier` char(16) DEFAULT NULL, `token` text NOT NULL, @@ -84,6 +85,8 @@ CREATE TABLE `api_keys` ( PRIMARY KEY (`id`), UNIQUE KEY `api_keys_identifier_unique` (`identifier`), KEY `api_keys_user_id_foreign` (`user_id`), + KEY `api_keys_node_id_foreign` (`node_id`), + CONSTRAINT `api_keys_node_id_foreign` FOREIGN KEY (`node_id`) REFERENCES `nodes` (`id`) ON DELETE CASCADE, CONSTRAINT `api_keys_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; @@ -876,3 +879,4 @@ INSERT INTO `migrations` VALUES (191,'2022_08_16_230204_add_installed_at_column_ INSERT INTO `migrations` VALUES (192,'2022_12_12_213937_update_mail_settings_to_new_format',1); INSERT INTO `migrations` VALUES (193,'2023_01_24_210051_add_uuid_column_to_failed_jobs_table',1); INSERT INTO `migrations` VALUES (194,'2023_02_23_191004_add_expires_at_column_to_api_keys_table',1); +INSERT INTO `migrations` VALUES (195,'2026_06_23_205500_add_node_id_to_api_keys_table',1); diff --git a/tests/Integration/Api/Application/Nodes/NodeConfigurationControllerTest.php b/tests/Integration/Api/Application/Nodes/NodeConfigurationControllerTest.php new file mode 100644 index 000000000..180f18779 --- /dev/null +++ b/tests/Integration/Api/Application/Nodes/NodeConfigurationControllerTest.php @@ -0,0 +1,68 @@ +for(Location::factory())->create(); + $this->createNewDefaultApiKey($this->getApiUser(), [ + 'node_id' => $node->id, + 'r_nodes' => AdminAcl::READ_CONFIGURATION, + ]); + + $response = $this->getJson('/api/application/nodes/' . $node->id . '/configuration'); + + $response->assertOk(); + $response->assertJsonPath('uuid', $node->uuid); + $response->assertJsonPath('token_id', $node->daemon_token_id); + $this->assertSame(decrypt($node->daemon_token), $response->json('token')); + } + + public function testConfigurationKeyCannotGetOtherNodeConfiguration(): void + { + $node = Node::factory()->for(Location::factory())->create(); + $otherNode = Node::factory()->for(Location::factory())->create(); + $this->createNewDefaultApiKey($this->getApiUser(), [ + 'node_id' => $node->id, + 'r_nodes' => AdminAcl::READ_CONFIGURATION, + ]); + + $this->assertAccessDeniedJson($this->getJson('/api/application/nodes/' . $otherNode->id . '/configuration')); + } + + public function testWriteKeyCanGetNodeConfiguration(): void + { + $this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => AdminAcl::WRITE]); + + $node = Node::factory()->for(Location::factory())->create(); + + $this->getJson('/api/application/nodes/' . $node->id . '/configuration')->assertOk(); + } + + public function testReadOnlyNodeKeyCannotGetNodeConfiguration(): void + { + $this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => AdminAcl::READ]); + + $node = Node::factory()->for(Location::factory())->create(); + + $this->assertAccessDeniedJson($this->getJson('/api/application/nodes/' . $node->id . '/configuration')); + } + + public function testConfigurationKeyCannotUpdateNode(): void + { + $node = Node::factory()->for(Location::factory())->create(); + $this->createNewDefaultApiKey($this->getApiUser(), [ + 'node_id' => $node->id, + 'r_nodes' => AdminAcl::READ_CONFIGURATION, + ]); + + $this->assertAccessDeniedJson($this->patchJson(route('api.application.nodes.update', ['node' => $node]))); + } +} diff --git a/tests/Integration/Http/Controllers/Admin/NodeAutoDeployControllerTest.php b/tests/Integration/Http/Controllers/Admin/NodeAutoDeployControllerTest.php new file mode 100644 index 000000000..fd6e926b5 --- /dev/null +++ b/tests/Integration/Http/Controllers/Admin/NodeAutoDeployControllerTest.php @@ -0,0 +1,31 @@ +for(Location::factory())->create(); + + $response = $this->actingAs(User::factory()->admin()->create()) + ->postJson(route('admin.nodes.view.configuration.token', ['node' => $node])); + + $response->assertOk(); + $response->assertJsonPath('node', $node->id); + + $key = ApiKey::query() + ->where('identifier', substr($response->json('token'), 0, ApiKey::IDENTIFIER_LENGTH)) + ->firstOrFail(); + + $this->assertSame($node->id, $key->node_id); + $this->assertSame(AdminAcl::READ_CONFIGURATION, $key->r_nodes); + } +}