Merge pull request #13190 from appwrite/fix/deny-jwt-creation-with-jwt-auth

fix(account): deny JWT creation for JWT-authorized requests
This commit is contained in:
Matej Bačo authored and GitHub committed 2026-08-13 10:37:54 +02:00
commit 7cb6f311ac
4 files changed
+24 -2

No files matched your search

+5
View File
@@ -397,6 +397,11 @@ return [
'description' => 'JWT and cookie used in the same request. Use either `setJWT` or `setCookie`. Learn about which authentication method to use in the SSR docs: https://appwrite.io/docs/products/auth/server-side-rendering',
'code' => 403,
],
Exception::USER_JWT_CREATION_DENIED => [
'name' => Exception::USER_JWT_CREATION_DENIED,
'description' => 'A JWT cannot be created from a request authorized with a JWT. Authenticate with a session cookie or session header instead.',
'code' => 403,
],
Exception::API_KEY_EXPIRED => [
'name' => Exception::API_KEY_EXPIRED,
'description' => 'The ephemeral API key has expired. Please don\'t use ephemeral API keys for more than duration of the execution.',
+7 -2
View File
@@ -3219,7 +3219,7 @@ Http::post('/v1/account/jwts')
group: 'tokens',
name: 'createJWT',
description: '/docs/references/account/create-jwt.md',
auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::JWT],
auth: [AuthType::ADMIN, AuthType::SESSION],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_CREATED,
@@ -3232,11 +3232,16 @@ Http::post('/v1/account/jwts')
->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2)
->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT)
->label('abuse-key', 'url:{url},userId:{userId}')
->inject('request')
->inject('response')
->inject('user')
->inject('store')
->inject('proofForToken')
->action(function (int $duration, Response $response, User $user, Store $store, ProofsToken $proofForToken) {
->action(function (int $duration, Request $request, Response $response, User $user, Store $store, ProofsToken $proofForToken) {
if (!empty($request->getHeaderLine('x-appwrite-jwt', ''))) {
throw new Exception(Exception::USER_JWT_CREATION_DENIED);
}
$sessionId = $user->sessionVerify($store->getProperty('secret', ''), $proofForToken);
if (!$sessionId) {
+1
View File
@@ -116,6 +116,7 @@ class Exception extends \Exception
public const string USER_TARGET_ALREADY_EXISTS = 'user_target_already_exists';
public const string USER_API_KEY_AND_SESSION_SET = 'user_api_key_and_session_set';
public const string USER_JWT_AND_COOKIE_SET = 'user_jwt_and_cookie_set';
public const string USER_JWT_CREATION_DENIED = 'user_jwt_creation_denied';
public const string USER_ID_MISSING = 'user_id_missing';
public const string API_KEY_EXPIRED = 'api_key_expired';
@@ -2762,6 +2762,17 @@ final class AccountCustomClientTest extends Scope
$this->assertEquals(200, $response['headers']['status-code']);
// Ensure a JWT cannot be created from a request authorized with a JWT
$response = $this->client->call(Client::METHOD_POST, '/account/jwt', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-jwt' => $jwt,
]));
$this->assertEquals(403, $response['headers']['status-code']);
$this->assertEquals('user_jwt_creation_denied', $response['body']['type']);
$response = $this->client->call(Client::METHOD_DELETE, '/account/sessions/' . $sessionId, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',