From 1df93903b6326befcb5ee19e69cf42008df25dd0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 14 Sep 2026 13:27:44 +0530 Subject: [PATCH 1/5] fix(functions): forward authenticated JWT to executions --- .../Functions/Http/Executions/Create.php | 9 +- .../Functions/FunctionsCustomClientTest.php | 94 +++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 6328792e5b..7f6723b02d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -99,6 +99,7 @@ class Create extends Base ->inject('dbForProject') ->inject('dbForPlatform') ->inject('user') + ->inject('impersonatorUser') ->inject('queueForEvents') ->inject('publisherForFunctions') ->inject('geo') @@ -127,6 +128,7 @@ class Create extends Base Database $dbForProject, Database $dbForPlatform, User $user, + Document $impersonatorUser, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Geo $geo, @@ -207,8 +209,11 @@ class Create extends Base throw new Exception(Exception::USER_UNAUTHORIZED, $authorization->getDescription()); } - $jwt = ''; // initialize - if (!$user->isEmpty()) { // If userId exists, generate a JWT for function + // Forward the authenticated user's token only when no other credential or impersonation applies. + $jwt = !$user->isEmpty() && $impersonatorUser->isEmpty() && $request->getHeaderLine('x-appwrite-key', '') === '' + ? ($request->getHeaderLine('x-appwrite-jwt', '') ?: '') + : ''; + if (!$user->isEmpty() && $jwt === '') { // Generate a JWT for session-authenticated users $sessions = $user->getAttribute('sessions', []); $current = new Document(); diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index a4b459bd9f..04b8b65226 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -279,6 +279,100 @@ final class FunctionsCustomClientTest extends Scope $this->cleanupFunction($functionId); } + public function testCreateExecutionWithJWT(): void + { + $functionId = $this->setupFunction([ + 'functionId' => ID::unique(), + 'name' => 'JWT execution', + 'execute' => [Role::user($this->getUser()['$id'])->toString()], + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'timeout' => 10, + ]); + + try { + $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('basic'), + 'activate' => true, + ]); + + $token = $this->client->call(Client::METHOD_POST, '/account/jwts', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals(201, $token['headers']['status-code']); + $jwt = $token['body']['jwt']; + + /** + * Test for SUCCESS + */ + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ], [ + 'headers' => ['x-appwrite-user-jwt' => 'OVERRIDDEN'], + ]); + + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals('completed', $execution['body']['status']); + $this->assertEquals(200, $execution['body']['responseStatusCode']); + $output = json_decode($execution['body']['responseBody'], true); + $this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']); + // Preserve the original token, including its expiry, when forwarding it. + $this->assertSame($jwt, $output['APPWRITE_FUNCTION_JWT']); + + $account = $this->client->call(Client::METHOD_GET, '/account', [ + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $output['APPWRITE_FUNCTION_JWT'], + ]); + $this->assertEquals(200, $account['headers']['status-code']); + $this->assertEquals($this->getUser()['$id'], $account['body']['$id']); + + // API-key executions must not forward an additional user credential. + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-jwt' => $jwt, + ]); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals(200, $execution['body']['responseStatusCode']); + $output = json_decode($execution['body']['responseBody'], true); + $this->assertSame('', $output['APPWRITE_FUNCTION_JWT']); + + /** + * Test for FAILURE + */ + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => 'invalid', + ]); + $this->assertEquals(401, $execution['headers']['status-code']); + $this->assertEquals('user_jwt_invalid', $execution['body']['type']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-jwt' => 'invalid', + ]); + $this->assertEquals(401, $execution['headers']['status-code']); + $this->assertEquals('user_jwt_invalid', $execution['body']['type']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ], $this->getHeaders())); + $this->assertEquals(401, $execution['headers']['status-code']); + $this->assertEquals('user_jwt_and_cookie_set', $execution['body']['type']); + } finally { + $this->cleanupFunction($functionId); + } + } + public function testNonOverrideOfHeaders() { $functionId = $this->setupFunction([ From 7948ff01226d060f5bda3ccfc678af1ca5b7fbf4 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 14 Sep 2026 13:40:21 +0530 Subject: [PATCH 2/5] test(functions): keep JWT execution payload headers valid --- tests/e2e/Services/Functions/FunctionsCustomClientTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 04b8b65226..1952e90639 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -310,8 +310,7 @@ final class FunctionsCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-jwt' => $jwt, - ], [ - 'headers' => ['x-appwrite-user-jwt' => 'OVERRIDDEN'], + 'x-appwrite-user-jwt' => 'OVERRIDDEN', ]); $this->assertEquals(201, $execution['headers']['status-code']); @@ -366,7 +365,7 @@ final class FunctionsCustomClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-jwt' => $jwt, ], $this->getHeaders())); - $this->assertEquals(401, $execution['headers']['status-code']); + $this->assertEquals(403, $execution['headers']['status-code']); $this->assertEquals('user_jwt_and_cookie_set', $execution['body']['type']); } finally { $this->cleanupFunction($functionId); From 9aeb053353df70c9029efbd4a54feb7d61642f69 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 14 Sep 2026 14:05:31 +0530 Subject: [PATCH 3/5] fix(functions): forward the caller's JWT like cookie sessions Forward x-appwrite-jwt from the existing session branch when no cookie session matches, instead of special-casing API keys and impersonation. JWT callers now get the same treatment as cookie callers. --- .../Modules/Functions/Http/Executions/Create.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 7f6723b02d..b0361eb495 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -99,7 +99,6 @@ class Create extends Base ->inject('dbForProject') ->inject('dbForPlatform') ->inject('user') - ->inject('impersonatorUser') ->inject('queueForEvents') ->inject('publisherForFunctions') ->inject('geo') @@ -128,7 +127,6 @@ class Create extends Base Database $dbForProject, Database $dbForPlatform, User $user, - Document $impersonatorUser, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Geo $geo, @@ -209,11 +207,8 @@ class Create extends Base throw new Exception(Exception::USER_UNAUTHORIZED, $authorization->getDescription()); } - // Forward the authenticated user's token only when no other credential or impersonation applies. - $jwt = !$user->isEmpty() && $impersonatorUser->isEmpty() && $request->getHeaderLine('x-appwrite-key', '') === '' - ? ($request->getHeaderLine('x-appwrite-jwt', '') ?: '') - : ''; - if (!$user->isEmpty() && $jwt === '') { // Generate a JWT for session-authenticated users + $jwt = ''; // initialize + if (!$user->isEmpty()) { // If userId exists, generate a JWT for function $sessions = $user->getAttribute('sessions', []); $current = new Document(); @@ -234,6 +229,9 @@ class Create extends Base 'userId' => $user->getId(), 'sessionId' => $current->getId(), ]); + } else { + // A JWT cannot be used to create another JWT, so forward the caller's token as-is + $jwt = $request->getHeaderLine('x-appwrite-jwt'); } } From 75b53e8613574a1dcb42d18781bd6fa2616a91a2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 14 Sep 2026 14:05:31 +0530 Subject: [PATCH 4/5] test(functions): assert only JWT forwarding in execution test --- .../Functions/FunctionsCustomClientTest.php | 103 ++++-------------- 1 file changed, 23 insertions(+), 80 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 1952e90639..2543470c82 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -279,97 +279,40 @@ final class FunctionsCustomClientTest extends Scope $this->cleanupFunction($functionId); } - public function testCreateExecutionWithJWT(): void + public function testCreateExecutionWithJWT() { + /** + * Test for SUCCESS + */ $functionId = $this->setupFunction([ 'functionId' => ID::unique(), - 'name' => 'JWT execution', + 'name' => 'Test JWT execution', 'execute' => [Role::user($this->getUser()['$id'])->toString()], 'runtime' => 'node-22', 'entrypoint' => 'index.js', 'timeout' => 10, ]); + $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('basic'), + 'activate' => true + ]); - try { - $this->setupDeployment($functionId, [ - 'code' => $this->packageFunction('basic'), - 'activate' => true, - ]); + $token = $this->client->call(Client::METHOD_POST, '/account/jwts', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $jwt = $token['body']['jwt']; - $token = $this->client->call(Client::METHOD_POST, '/account/jwts', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - $this->assertEquals(201, $token['headers']['status-code']); - $jwt = $token['body']['jwt']; + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ]); + $output = json_decode($execution['body']['responseBody'], true); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals($jwt, $output['APPWRITE_FUNCTION_JWT']); - /** - * Test for SUCCESS - */ - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-jwt' => $jwt, - 'x-appwrite-user-jwt' => 'OVERRIDDEN', - ]); - - $this->assertEquals(201, $execution['headers']['status-code']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - $output = json_decode($execution['body']['responseBody'], true); - $this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']); - // Preserve the original token, including its expiry, when forwarding it. - $this->assertSame($jwt, $output['APPWRITE_FUNCTION_JWT']); - - $account = $this->client->call(Client::METHOD_GET, '/account', [ - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-jwt' => $output['APPWRITE_FUNCTION_JWT'], - ]); - $this->assertEquals(200, $account['headers']['status-code']); - $this->assertEquals($this->getUser()['$id'], $account['body']['$id']); - - // API-key executions must not forward an additional user credential. - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - 'x-appwrite-jwt' => $jwt, - ]); - $this->assertEquals(201, $execution['headers']['status-code']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - $output = json_decode($execution['body']['responseBody'], true); - $this->assertSame('', $output['APPWRITE_FUNCTION_JWT']); - - /** - * Test for FAILURE - */ - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-jwt' => 'invalid', - ]); - $this->assertEquals(401, $execution['headers']['status-code']); - $this->assertEquals('user_jwt_invalid', $execution['body']['type']); - - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - 'x-appwrite-jwt' => 'invalid', - ]); - $this->assertEquals(401, $execution['headers']['status-code']); - $this->assertEquals('user_jwt_invalid', $execution['body']['type']); - - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-jwt' => $jwt, - ], $this->getHeaders())); - $this->assertEquals(403, $execution['headers']['status-code']); - $this->assertEquals('user_jwt_and_cookie_set', $execution['body']['type']); - } finally { - $this->cleanupFunction($functionId); - } + $this->cleanupFunction($functionId); } public function testNonOverrideOfHeaders() From a141e3d35414604f9301efe91dd5e062e6693d2d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 14 Sep 2026 14:43:33 +0530 Subject: [PATCH 5/5] test(functions): cover JWT forwarding for async executions Async executions do not store the response body, so the basic function logs the forwarded JWT and the test reads it from the execution logs. --- .../Functions/FunctionsCustomClientTest.php | 20 +++++++++++++++++++ tests/resources/functions/basic/index.js | 1 + 2 files changed, 21 insertions(+) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 2543470c82..be1e7f35c1 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -312,6 +312,26 @@ final class FunctionsCustomClientTest extends Scope $this->assertEquals(201, $execution['headers']['status-code']); $this->assertEquals($jwt, $output['APPWRITE_FUNCTION_JWT']); + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ], [ + 'async' => true, + ]); + $this->assertEquals(202, $execution['headers']['status-code']); + $executionId = $execution['body']['$id']; + + // Async executions do not store the response body, so read the forwarded JWT from the logs + $this->assertEventually(function () use ($functionId, $executionId, $jwt) { + $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $executionId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertStringContainsString('user-jwt-is-' . $jwt, (string) $execution['body']['logs']); + }, 60000, 500); + $this->cleanupFunction($functionId); } diff --git a/tests/resources/functions/basic/index.js b/tests/resources/functions/basic/index.js index 47f7100b5b..85f222430a 100644 --- a/tests/resources/functions/basic/index.js +++ b/tests/resources/functions/basic/index.js @@ -19,6 +19,7 @@ module.exports = async(context) => { context.log('method-is-' + (context.req.method ?? '').toLowerCase()); context.log('path-is-' + (context.req.path ?? '')); context.log('user-is-' + (context.req.headers['x-appwrite-user-id'] ?? '')); + context.log('user-jwt-is-' + (context.req.headers['x-appwrite-user-jwt'] ?? '')); const statusCode = context.req.query['code'] || '200';