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.
This commit is contained in:
harsh-sikarwar committed 2026-08-08 18:11:32 +05:30
1 parent 477653c848
commit ffe62dd739
2 files changed
+30 -1

No files matched your search

@@ -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);
}
+24
View File
@@ -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']);
}