Skip to content

fix(account): require a recent MFA challenge to disable MFA - #15057

Merged
ArnabChatterjee20k merged 2 commits into
mainfrom
security/mfa-disable-reauth
Oct 29, 2026
Merged

ArnabChatterjee20k merged 2 commits into
mainfrom
security/mfa-disable-reauth

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What does this PR do?

PATCH /v1/account/mfa could turn MFA off with any fully-factored session, without a fresh challenge. Deleting an authenticator already needs a recent challenge (mfaProtected), so the disable toggle was the weaker path to the same outcome.

  • Turning MFA off while it is in force (getMinimumFactors() > 1) now needs an MFA challenge completed in the last MFA_RECENT_DURATION, and returns 401 user_challenge_required otherwise, the same as deleting an authenticator.
  • Turning MFA on is unchanged: a user without MFA cannot pass a challenge yet.
  • When MFA is flagged on but no second factor is verified, nothing is enforced and no challenge can be completed, so disabling stays possible.
  • The recency check moves to User::sessionChallengeRecent(), used by both the mfaProtected hook and this action, so there is one check instead of two copies. The hook behaves the same: the session resource is the entry from the user's sessions.

mfaProtected is a static route group, so it cannot apply to only one direction of the toggle. That is why the check runs in the action.

Test Plan

  • E2E AccountCustomClientTest::testUpdateMFADisableRequiresRecentChallenge: enabling works without a challenge; disabling without one returns 401 user_challenge_required and MFA stays on; disabling works after a recovery-code challenge. Fails on main (200 instead of 401) and passes with this change.
  • E2E AccountCustomClientTest::testUpdateMFADisableWithoutSecondFactor: MFA flagged on with no verified factor can still be turned off.
  • Unit UserTest::testSessionChallengeRecent: recent, stale, never-challenged and unknown sessions.
  • Existing Account / Passkeys / Users MFA e2e filters pass.

Related PRs and Issues

Tracking: appwrite-labs/cloud#7053

🤖 Generated with Claude Code

ArnabChatterjee20k and others added 2 commits October 28, 2026 17:03
Turning MFA off while it is in force now needs the same recent challenge
as deleting an authenticator. The recency check moves onto the User
document so the mfaProtected hook and the action share it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 2 nitpicks below.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Update docs and SDK spec for the new MFA-disable challenge (update-mfa.md) — Disabling MFA now returns 401 user_challenge_required when no recent challenge exists (src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php:82), but the SDK Method description still points to docs/references/account/update-mfa.md, which only says "Enable or disable MFA on an account." The SDK spec also lists only the 200 response (src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php:50).
  • 🟢 Key-auth MFA disable now fails with 401, undocumented (Update.php:82) — The new guard at src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php:82 requires $user->sessionChallengeRecent($session->getId()), but the endpoint advertises AuthType::ADMIN (line 49).

The change gates PATCH /v1/account/mfa with mfa=false behind a recently completed MFA challenge, closing a bypass where an attacker with a hijacked session could unenroll without a second-factor proof.

Files Change
src/Appwrite/Utopia/Database/Documents/User.php, tests/unit/Utopia/Database/Documents/UserTest.php Adds sessionChallengeRecent() comparing session mfaUpdatedAt against MFA_RECENT_DURATION, with unit tests for fresh/stale/missing-session cases.
app/controllers/shared/api/auth.php, src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php Wires session challenge resources into the shared auth hook and enforces the new 401 user_challenge_required guard on MFA disable.
tests/e2e/Services/Account/AccountCustomClientTest.php Adds e2e coverage for disabling MFA without a fresh challenge and after completing one.

All MFA-enforcement paths were traced: the guard is consistent with the existing mfaProtected sibling routes, and the main follow-ups are documentation-facing (SDK spec and docs do not yet mention the new 401, including for key-authenticated callers).

Reviewed commit: 7135f67

@ghost

ghost commented Oct 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The change preserves the existing protected-route check and adds the requested guarded MFA-disable behavior with relevant coverage; no actionable defects were found.

The MFA disable action now requires a recent challenge when MFA is enforced, while allowing disablement when no verified second factor exists. The shared user document method centralizes the recency check used by the mfaProtected hook and the toggle action, with E2E and unit coverage for the updated behavior.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
app/controllers/shared/api/auth.php Routes the mfaProtected hook's recency check through the shared user method.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php Requires a recent challenge before disabling enforced MFA.
src/Appwrite/Utopia/Database/Documents/User.php Adds a shared recent-session-challenge check.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers guarded disablement, successful challenge flow, and disabling without a verified second factor.
tests/unit/Utopia/Database/Documents/UserTest.php Covers recent, stale, missing, and unknown session challenge timestamps.

Reviewed 7135f67 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → security/mfa-disable-reauth (after).

Metric Before After Change
🚀 Requests/sec 179.41 171.72 ⚪ -4.3%
⏱️ Latency P50 94.7 ms 99.78 ms 🔴 +5.4%
⏱️ Latency P95 232.84 ms 234.42 ms ⚪ +0.7%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 99.78 234.42 10,944 171.72 +1.58
Account 184.45 341.11 576 9.55 +7.06
TablesDB 96.88 179.32 5,952 96.05 -9.01
Storage 91.85 221.46 2,880 48.02 +14.76
Functions 136.14 275.8 1,536 26.22 +9.29

Top API waits (after)

API request Max wait (ms)
account.name.update 443.96
storage.buckets.create 443.59
storage.files.preview 441.43
tokens.files.create 407.4
functions.delete 404.97

@ArnabChatterjee20k
ArnabChatterjee20k merged commit a6cd58e into main Oct 29, 2026
121 of 123 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants