From 69c01a4b9d00835d8dad0dd3582cc2f07f6ef975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 8 Aug 2026 20:32:11 +0200 Subject: [PATCH] fix(mfa): expose challenge code directly in response model instead of aliasing to secret Co-Authored-By: Claude Fable 5 --- .../Users/Http/Users/MFA/Challenges/Get.php | 4 +--- .../Response/Model/MFAChallengeSecret.php | 4 ++-- .../Account/AccountCustomClientTest.php | 24 ++++++++++--------- tests/e2e/Services/Users/UsersBase.php | 9 +++---- 4 files changed, 21 insertions(+), 20 deletions(-) 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 a6b16fa59d..e1d40b548e 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 @@ -37,7 +37,7 @@ class Get extends Action namespace: 'users', group: 'mfa', name: 'getMFAChallenge', - description: 'Get a custom MFA challenge for a user, including the secret to be delivered through your own channel.', + description: 'Get a custom MFA challenge for a user, including the code to be delivered through your own channel.', auth: [AuthType::ADMIN, AuthType::KEY], responses: [ new SDKResponse( @@ -72,8 +72,6 @@ class Get extends Action throw new Exception(Exception::USER_INVALID_TOKEN); } - $challenge->setAttribute('secret', $challenge->getAttribute('code')); - $response->dynamic($challenge, Response::MODEL_MFA_CHALLENGE_SECRET); } } diff --git a/src/Appwrite/Utopia/Response/Model/MFAChallengeSecret.php b/src/Appwrite/Utopia/Response/Model/MFAChallengeSecret.php index 952c2ff9f3..1f56027e19 100644 --- a/src/Appwrite/Utopia/Response/Model/MFAChallengeSecret.php +++ b/src/Appwrite/Utopia/Response/Model/MFAChallengeSecret.php @@ -11,9 +11,9 @@ class MFAChallengeSecret extends MFAChallenge parent::__construct(); $this - ->addRule('secret', [ + ->addRule('code', [ 'type' => self::TYPE_STRING, - 'description' => 'Challenge secret to be delivered to the end user through a custom channel.', + 'description' => 'Challenge code to be delivered to the end user through a custom channel.', 'default' => '', 'example' => '446372', ]); diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index df2f7c3871..1dfcd1f965 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -4286,27 +4286,29 @@ final class AccountCustomClientTest extends Scope $this->assertEquals(201, $challenge['headers']['status-code']); $this->assertNotEmpty($challenge['body']['$id']); + // The challenge code must never be exposed on the client-facing create response + $this->assertArrayNotHasKey('code', $challenge['body']); $challengeId = $challenge['body']['$id']; $userId = $challenge['body']['userId']; - // Test FAILURE: a client session (no API key) must not be able to read the secret - $clientSecretResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([ + // Test FAILURE: a client session (no API key) must not be able to read the code + $clientCodeResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, ], $this->getHeaders())); - $this->assertEquals(401, $clientSecretResponse['headers']['status-code']); + $this->assertEquals(401, $clientCodeResponse['headers']['status-code']); - // Server SDK reads the raw secret via the server-only endpoint - $secretResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, [ + // Server SDK reads the raw code via the server-only endpoint + $codeResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $apiKey, ]); - $this->assertEquals(200, $secretResponse['headers']['status-code']); - $this->assertNotEmpty($secretResponse['body']['secret']); - $secret = $secretResponse['body']['secret']; + $this->assertEquals(200, $codeResponse['headers']['status-code']); + $this->assertNotEmpty($codeResponse['body']['code']); + $code = $codeResponse['body']['code']; // Test FAILURE: verifying with a random/incorrect otp before the real one is used $wrongFirst = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenge', array_merge([ @@ -4319,13 +4321,13 @@ final class AccountCustomClientTest extends Scope $this->assertEquals(401, $wrongFirst['headers']['status-code']); - // Test SUCCESS: Verify with the correct secret + // Test SUCCESS: Verify with the correct code $verification = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenge', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, ], $this->getHeaders()), [ 'challengeId' => $challengeId, - 'otp' => $secret + 'otp' => $code ]); $this->assertEquals(200, $verification['headers']['status-code']); @@ -4341,7 +4343,7 @@ final class AccountCustomClientTest extends Scope 'x-appwrite-project' => $projectId, ], $this->getHeaders()), [ 'challengeId' => $challengeId, - 'otp' => $secret + 'otp' => $code ]); $this->assertEquals(401, $reuse['headers']['status-code']); diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index 1be5c70edb..d63d4fda9d 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -663,10 +663,11 @@ trait UsersBase ]); $this->assertEquals(201, $challenge['headers']['status-code']); + $this->assertArrayNotHasKey('code', $challenge['body']); $challengeId = $challenge['body']['$id']; /** - * Test for SUCCESS: server (API key) can read the secret + * Test for SUCCESS: server (API key) can read the code */ $response = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([ 'content-type' => 'application/json', @@ -676,7 +677,7 @@ trait UsersBase $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals($challengeId, $response['body']['$id']); $this->assertEquals($userId, $response['body']['userId']); - $this->assertNotEmpty($response['body']['secret']); + $this->assertNotEmpty($response['body']['code']); /** * Test for FAILURE: a client session (not a server key) must be rejected @@ -735,8 +736,8 @@ trait UsersBase /** * 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. + * own code out of band (email/SMS); this endpoint only exists to hand the + * 'custom' factor's code to the developer's own delivery mechanism. */ $totpChallenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenge', [ 'content-type' => 'application/json',