From ffe62dd73996f53636fc17862af877b35c2f7180 Mon Sep 17 00:00:00 2001 From: harsh-sikarwar Date: Sat, 8 Aug 2026 18:11:32 +0530 Subject: [PATCH] fix: restrict getMFAChallenge to custom challenges The endpoint checked challenge ownership but not challenge type, so a users.read API key that learned any pending challengeId (e.g. email or phone) could read that challenge's code -- the same field native factors use to store the OTP already delivered to the user. This endpoint exists solely to hand the custom factor's secret to the developer's own delivery mechanism, so it must refuse anything else. Adds a regression test creating a totp challenge and asserting the endpoint rejects it. Found by Greptile review. --- .../Users/Http/Users/MFA/Challenges/Get.php | 7 +++++- tests/e2e/Services/Users/UsersBase.php | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php index f11db67cf1..97ca994dac 100644 --- a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php +++ b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Users\Http\Users\MFA\Challenges; +use Appwrite\Auth\MFA\Type; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -64,7 +65,11 @@ class Get extends Action $challenge = $dbForProject->getDocument('challenges', $challengeId); - if ($challenge->isEmpty() || $challenge->getAttribute('userId') !== $userId) { + if ( + $challenge->isEmpty() + || $challenge->getAttribute('userId') !== $userId + || $challenge->getAttribute('type') !== Type::CUSTOM + ) { throw new Exception(Exception::USER_INVALID_TOKEN); } diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index 0745e85b2a..1be5c70edb 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -731,6 +731,30 @@ trait UsersBase ], $this->getHeaders())); $this->assertEquals(404, $response['headers']['status-code']); + + /** + * Test for FAILURE: a non-custom challenge (e.g. totp) must not be readable, + * even with valid ownership and a valid API key. Native factors deliver their + * own secret out of band (email/SMS); this endpoint only exists to hand the + * 'custom' factor's secret to the developer's own delivery mechanism. + */ + $totpChallenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenge', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-session' => $sessionSecret, + ], [ + 'factor' => 'totp' + ]); + + $this->assertEquals(201, $totpChallenge['headers']['status-code']); + $totpChallengeId = $totpChallenge['body']['$id']; + + $response = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $totpChallengeId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders())); + + $this->assertEquals(401, $response['headers']['status-code']); }