mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix(account): deny JWT creation for JWT-authorized requests
A request authorized with a JWT could attempt to mint a new JWT and received a misleading user_session_not_found error. Reject it explicitly with a dedicated user_jwt_creation_denied (403) error and drop JWT from the endpoint's SDK auth types. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cd368e707d
commit
5a1264e91f
4 files changed
+24
-2
No files matched your search
@@ -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.',
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in new issue
Block a user