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']); }