mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13203 from appwrite/fix/deny-key-authorized-key-creation
fix(project): deny key creation for key-authorized requests
This commit is contained in:
5 files changed
+64
-5
No files matched your search
@@ -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.',
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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([
|
||||
|
||||
@@ -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'],
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user