diff --git a/app/config/errors.php b/app/config/errors.php index 3abceea877..8fe036f44e 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -1231,6 +1231,11 @@ return [ 'description' => 'Key with the same ID already exists. Try again with a different ID.', 'code' => 409, ], + Exception::KEY_CREATION_DENIED => [ + 'name' => Exception::KEY_CREATION_DENIED, + 'description' => 'An API key cannot be created from a request authorized with an API key. Authenticate with a session instead. To create a short-lived key from a server, use the ephemeral key endpoint.', + 'code' => 403, + ], Exception::DEV_KEY_GONE => [ 'name' => Exception::DEV_KEY_GONE, 'description' => 'Dev key creation is no longer available.', diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index e3866e470f..2477eb0379 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -338,6 +338,7 @@ class Exception extends \Exception /** Keys */ public const string KEY_NOT_FOUND = 'key_not_found'; public const string KEY_ALREADY_EXISTS = 'key_already_exists'; + public const string KEY_CREATION_DENIED = 'key_creation_denied'; /** Dev Keys */ public const string DEV_KEY_GONE = 'dev_key_gone'; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php index c59fb03279..20062b5aaf 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Project\Http\Project\Keys; +use Appwrite\Auth\Key; use Appwrite\Event\Event as QueueEvent; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -55,7 +56,7 @@ class Create extends Base You can also create an ephemeral API key if you need a short-lived key instead. EOT, - auth: [AuthType::ADMIN, AuthType::KEY], + auth: [AuthType::ADMIN], responses: [ new SDKResponse( code: Response::STATUS_CODE_CREATED, @@ -72,6 +73,7 @@ class Create extends Base ->inject('dbForPlatform') ->inject('project') ->inject('authorization') + ->inject('apiKey') ->callback($this->action(...)); } @@ -85,7 +87,13 @@ class Create extends Base Database $dbForPlatform, Document $project, Authorization $authorization, + ?Key $apiKey, ) { + // Dynamic supported for backwards compatibility + if ($apiKey !== null && \in_array($apiKey->getType(), [API_KEY_STANDARD, API_KEY_EPHEMERAL, 'dynamic'], true)) { + throw new Exception(Exception::KEY_CREATION_DENIED); + } + $keyId = ($keyId == 'unique()') ? ID::unique() : $keyId; $key = new Document([ diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 53fce889a6..cc47d4f97e 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -2575,8 +2575,11 @@ trait MigrationsBase 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], ]; - // Create API key on source project - $response = $this->client->call(Client::METHOD_POST, '/project/keys', $sourceHeaders, [ + // Create API key on source project (key creation is denied for key-authorized requests) + $response = $this->client->call(Client::METHOD_POST, '/project/keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ 'keyId' => ID::unique(), 'name' => 'Test API Key', 'scopes' => ['databases.read', 'databases.write'], diff --git a/tests/e2e/Services/Project/KeysBase.php b/tests/e2e/Services/Project/KeysBase.php index c8687d9964..c56a055b76 100644 --- a/tests/e2e/Services/Project/KeysBase.php +++ b/tests/e2e/Services/Project/KeysBase.php @@ -99,8 +99,11 @@ trait KeysBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-response-format' => '1.9.0', + // Key creation is denied for key-authorized requests, so always use a console session + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-mode' => 'admin', ]; - $headers = array_merge($headers, $this->getHeaders()); $key = $this->client->call(Client::METHOD_POST, '/project/keys', $headers, [ 'keyId' => ID::unique(), @@ -239,6 +242,40 @@ trait KeysBase $this->deleteKey($customId); } + public function testCreateKeyRequiresSession(): void + { + // A request authorized with a standard API key cannot create a key + $response = $this->client->call(Client::METHOD_POST, '/project/keys', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'keyId' => ID::unique(), + 'name' => 'Key Made By Key', + 'scopes' => ['users.read'], + ]); + + $this->assertSame(403, $response['headers']['status-code']); + $this->assertSame('key_creation_denied', $response['body']['type']); + + // A request authorized with an ephemeral key cannot create a key either + $ephemeral = $this->createEphemeralKey(['keys.read', 'keys.write'], 900); + $this->assertSame(201, $ephemeral['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_POST, '/project/keys', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $ephemeral['body']['secret'], + ], [ + 'keyId' => ID::unique(), + 'name' => 'Key Made By Ephemeral Key', + 'scopes' => ['users.read'], + ]); + + $this->assertSame(403, $response['headers']['status-code']); + $this->assertSame('key_creation_denied', $response['body']['type']); + } + // ========================================================================= // Create ephemeral key tests // ========================================================================= @@ -890,7 +927,12 @@ trait KeysBase ]; if ($authenticated) { - $headers = array_merge($headers, $this->getHeaders()); + // Key creation is denied for key-authorized requests, so always use a console session + $headers = array_merge($headers, [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-mode' => 'admin', + ]); } return $this->client->call(Client::METHOD_POST, '/project/keys', $headers, $params);