Skip to content

fix(abuse): count code guesses for every caller on the recovery and session routes (appwrite:appwrite:alert#4729) - #15470

Merged
HarshMN2345 merged 3 commits into
mainfrom
fix/abuse-code-guesses-every-caller
Nov 11, 2026
Merged

HarshMN2345 merged 3 commits into
mainfrom
fix/abuse-code-guesses-every-caller

Conversation

@claudear

Copy link
Copy Markdown
Contributor

What does this PR do?

Code scanning alert #4729. The everyone flag that makes an abuse limit count for an API key or a privileged console caller landed only on the three verification confirmations. The sibling routes that verify the same class of short numeric secret were missed, and for each of them that limit was the only cap on guesses:

Route What a correct guess does
PUT /v1/account/recovery/otp sets a new password, marks the email verified and, for an unverified account, revokes the registrant's other credentials
POST /v1/account/sessions/token full session — the shared createSession step accepts a secret verified with the code proof
PUT /v1/account/sessions/phone full session, same path
PUT /v1/account/sessions/magic-url full session, same path
PUT /v1/account/mfa/challenges clears the MFA factor on the session

packages/auth/src/Proofs/Code.php defaults to six digits, so the space is 10^6. Nothing else bounds the attempts: app/controllers/shared/api/auth.php returns early for keys and privileged callers, and User::tokenVerify() records no failed attempt. A key holding sessions.write — the key an SSR backend relays logins and recoveries with, inherited by every unauthenticated end user of that app — could walk the whole code space for a chosen user and mint fresh codes meanwhile so many are valid at once. That is account takeover with no access to the victim's inbox or handset, and the same key is deliberately refused the recovery secret unless it also holds users.write.

Every limit kept per a part the caller names now counts for every caller on those routes, the long-token recovery confirmation (PUT /v1/account/recovery) included for consistency. The limits kept only per caller, per ip, stay exempt: one server relaying the logins of many users would otherwise lock all of them out at once.

The unit lock is derived instead of listing three paths. It walks the registered routes, picks the ones that verify a secret from the request against a proof or take a one-time password, and holds each to limits that count for everyone — so a route added later is covered the moment it is registered.

Test Plan

  • tests/unit/Abuse/RoutesTest.php — the derived lock. Fails on main (POST /v1/account/sessions/token verifies a secret and is limited per 'param.userId', but trusted callers are exempt from that limit), passes with the fix. Full unit suite: 2588 tests, no failures (19 errors are the usual environmental KeyTest / ListCacheTest ones).
  • tests/e2e/General/AbuseTest.php — two new abuseEnabled tests drive the real API with a project key: ten wrong six-digit guesses answer 401, the eleventh answers 429, on PUT /v1/account/recovery/otp and POST /v1/account/sessions/token. Both fail on main (401 is identical to 429) and pass with the fix.
  • tests/e2e/General/AbuseTest.php --group abuseEnabled: 40 tests, only the 6 known DocumentsDB/VectorsDB transaction cases fail locally (service disabled in the local stack, 403 before the abuse hook). tests/e2e/Services/Account --group abuseEnabled: 3 tests OK.
  • Pint and Rector clean on the touched files.

Related PRs

🤖 Generated with Claude Code

…ession routes

The everyone flag that makes an abuse limit count for an API key or a
privileged console caller was applied only to the three verification
confirmations. The sibling routes that verify the same class of short
numeric secret were missed:

- PUT /v1/account/recovery/otp verifies a six digit recovery code and a
  correct guess sets a new password, marks the email verified and, for an
  unverified account, revokes the registrant's other credentials. Its
  single per account limit was its only cap.
- POST /v1/account/sessions/token, PUT /v1/account/sessions/phone and
  PUT /v1/account/sessions/magic-url all redeem a token through the shared
  createSession step, which accepts a secret verified with the code proof,
  so the same six digit email or phone code yields a full session.
- PUT /v1/account/mfa/challenges verifies an MFA code, and its limit per
  challenge was the only cap on it.

Nothing else bounds the guesses: the auth method hook returns early for
keys and privileged callers, and tokenVerify records no failed attempt. A
key holding sessions.write, the key an SSR backend relays logins and
recoveries with, could walk the 10^6 code space for a chosen user and mint
fresh codes meanwhile so many are valid at once -- account takeover with no
access to the victim's inbox or handset. That same key is deliberately
refused the recovery secret unless it also holds users.write.

Every limit kept per a part the caller names now counts for every caller on
those routes, the long token recovery confirmation included. The limits kept
only per caller, per ip, stay exempt: one server relaying the logins of many
users would otherwise lock all of them out at once.

The unit lock is now derived rather than a list of three paths. It walks the
registered routes, picks the ones that verify a secret from the request
against a proof or take a one time password, and holds each of them to a
limit that counts for everyone, so a route added later is covered the moment
it is registered.

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

luxor-code-review Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high · 🟡 2 medium

💬 Inline comments (2)

  • 🟠 Per-user recovery limits enable attacker-driven lockout — Update.php:73
  • 🟡 Align the phone route's abuse limits with magic-url and token — Update.php:72
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Authenticator verification route has no abuse limit · src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php

PUT /v1/account/mfa/authenticators/:type takes a six-digit otp and verifies it via Challenge\TOTP::step, but its constructor declares no abuse label at all (src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php:83), unlike the MFA challenge route which caps guesses at 10 per challenge. Challenge\TOTP::step performs no attempt counting of its own, so nothing in the route's configuration throttles guesses at the TOTP code for the current step window. The new route detector in tests/unit/Abuse/RoutesTest.php:57-68 collects otp-param routes but only requires a limit when a proof injection is present, so this otp-only route is not locked by the test either.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Canary list misses proofForToken routes and a vacuous-detector guard (RoutesTest.php:83) — The canary assertions that guard the derived detector against going blind name only four routes (tests/unit/Abuse/RoutesTest.php:83-86), while the routes verifying secrets through the proofForToken injection — PUT /v1/account/verifications/email, PUT /v1/account/verifications/phone, and PUT /v1/account/recovery/code — are unnamed.

The change flips everyone: true on the per-account limits of the remaining secret-verifying Account routes and adds tests/unit/Abuse/RoutesTest.php, which derives the set of routes taking a secret/otp param plus a proof injection and asserts each declares an abuse label, backed by tests/e2e/General/AbuseTest.php checking a real 429 after the tenth attempt.

Files Change
src/Appwrite/Abuse/Limit.php Adds the everyone flag so attempts/failures limits also key on a caller-independent namespace
src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/*.php, Sessions/Phone/Update.php Adds everyone: true limits keyed on param.userId via Limit::attempts, without a per-IP companion limit
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/MagicURL/Update.php, Sessions/Token/Create.php Adds everyone: true per-user limits via Limit::failures plus the existing per-IP backstop
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php Adds everyone: true to the existing MFA challenge limit
tests/unit/Abuse/RoutesTest.php New contract test scanning route definitions for secret-verifying routes and asserting non-empty abuse limits
tests/e2e/General/AbuseTest.php New e2e coverage asserting 429 throttling under abuseEnabled with a single-window counting helper

The intent is sound — closes distributed brute-force paths left open when limits were IP-keyed only — but the rollout is inconsistent: recovery and phone still use Limit::attempts (no refund on success) keyed solely on caller-supplied userId with no per-IP backstop, unlike the token and magic-url siblings that use Limit::failures plus an IP backstop, and one OTP-verifying route (MFA/Authenticators) is missed by both the code and the new detector.

Reviewed commit: 587d16f

@appwrite

appwrite Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The change marks secret-verification abuse limits to count for API keys and privileged callers, and extends the route-level unit test to inspect registered verification routes. It adds end-to-end checks for recovery OTP and token-session guesses.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 10
File Change
src/Appwrite/Abuse/Limit.php Documents the requirement for verification routes to count account-specific limits for every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php Adds an abuse limit for MFA authenticator OTP verification.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php Makes the challenge-specific MFA abuse limit apply to every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/OTP/Update.php Makes recovery OTP attempts count for every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/Update.php Makes recovery-token confirmation attempts count for every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/MagicURL/Update.php Makes per-user Magic URL session failure limits apply to every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/Phone/Update.php Makes per-user phone-session attempts count for every caller.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/Token/Create.php Makes per-user token-session failure limits apply to every caller.
tests/e2e/General/AbuseTest.php Adds API-key tests for throttling recovery OTP and token-session guesses.
tests/unit/Abuse/RoutesTest.php Derives verification routes from registered routes and asserts their account-specific abuse limits count for every caller.
🔇 Filtered out · 1

Findings Hansi considered but did not post.

Finding Why
Keep trusted callers exempt for the high-entropy recovery token Verifier: This is a deliberate design choice, and the unit test tests/unit/Abuse/RoutesTest.php enforces it for every route that verifies a secret. The limit is keyed on projectId, url and param.userId, with no IP. Any anonymous caller could already lock out a user's recovery with ten wrong attempts, so letting a project key do the same adds no new exposure. The fix just applies the limit to trusted callers too.

Reviewed 1a2b9d8 · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

@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.

@appwrite appwrite Bot 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

@luxor-code-review luxor-code-review Bot 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.

This PR adds brute-force abuse limits (everyone: true) to six Account authentication endpoints — MFA challenge, recovery (token and OTP), magic-url, phone, and token session — and introduces a unit contract test plus e2e coverage locking those limits into every route that verifies a secret.

Key findings

  • 🟠 Per-user recovery limits enable attacker-driven lockout — Update.php:73
  • 🟡 Align the phone route's abuse limits with magic-url and token — Update.php:72
  • 🟡 Authenticator verification route has no abuse limit — Update.php

Comment on lines 72 to 75
->label('abuse', [
Limit::attempts(['url', 'ip', 'param.userId'], 10),
Limit::attempts(['projectId', 'url', 'param.userId'], 10),
Limit::attempts(['url', 'ip', 'param.userId'], 10, everyone: true),
Limit::attempts(['projectId', 'url', 'param.userId'], 10, everyone: true),
])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 bug · medium

Align the phone route's abuse limits with magic-url and token

The phone session verification route is the only sibling still using Limit::attempts, which counts every request and never releases a hit (src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/Phone/Update.php:72-75); magic-url and token use Limit::failures, which refunds a successful request's reserved hit. Under attempts, a user who mistypes a code twice and then succeeds has permanently spent 3 of the 10 per-account attempts, and repeated successful phone logins exhaust the budget until the window resets. Both declared limits are also keyed jointly on param.userId, so rotating userId values evades them entirely, and the route lacks the Limit::failures(['projectId', 'url', 'ip'], 100) per-caller backstop that both sibling routes keep.

📋 Prompt for AI Agents

In src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/Phone/Update.php lines 72-75, change the abuse label to mirror Sessions/MagicURL/Update.php lines 73-77: Limit::failures(['url', 'ip', 'param.userId'], 10, everyone: true), Limit::failures(['projectId', 'url', 'param.userId'], 10, everyone: true), and Limit::failures(['projectId', 'url', 'ip'], 100). The current label uses Limit::attempts (successful verifications permanently consume the per-account budget, unlike the failure-refund semantics of the sibling routes) and lacks any ip-only limit, so a caller rotating userId values faces no aggregate cap.

contentType: ContentType::JSON,
))
->label('abuse', [Limit::attempts(['projectId', 'url', 'param.userId'], 10)])
->label('abuse', [Limit::attempts(['projectId', 'url', 'param.userId'], 10, everyone: true)])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 security · high

Per-user recovery limits enable attacker-driven lockout

Both recovery confirmation routes declare a single Limit::attempts(['projectId', 'url', 'param.userId'], 10, everyone: true) keyed only on the caller-supplied userId, with no per-ip companion limit (src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/OTP/Update.php:73). The abuse hook evaluates limits before the action runs, and attempts counts every request without ever releasing a hit, so once 10 requests land against a userId the victim's next attempt is refused with 429 before tokenVerify() is reached. An attacker who knows a victim's userId (Appwrite user IDs are caller-visible) can deny that user password recovery for the full one-hour window, indefinitely by repeating. The sibling token and magic-url routes avoid this by using Limit::failures plus a per-ip backstop.

📋 Prompt for AI Agents

In src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/OTP/Update.php line 73 and src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/Update.php line 73, replace the single Limit::attempts(['projectId', 'url', 'param.userId'], 10, everyone: true) with a limit list mirroring Sessions/Token/Create.php lines 67-71: Limit::failures(['url', 'ip', 'param.userId'], 10, everyone: true), Limit::failures(['projectId', 'url', 'param.userId'], 10, everyone: true), and a per-attacker backstop Limit::failures(['projectId', 'url', 'ip'], 100). As written, an attacker who knows a victim's userId can burn the victim's 10-request attempts budget with junk requests and 429-lock them out of password recovery for the hour, because the abuse hook runs before the action and attempts never releases counts.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/abuse-code-guesses-every-caller (after).

Metric Before After Change
🚀 Requests/sec 208.79 213.72 ⚪ +2.4%
⏱️ Latency P50 82.57 ms 80.27 ms ⚪ -2.8%
⏱️ Latency P95 192.34 ms 193.06 ms ⚪ +0.4%
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 80.27 193.06 13,680 213.72 +0.72
Account 151.46 302.8 720 11.94 +24.76
TablesDB 76.36 145.54 7,440 119.42 -12.05
Storage 75.27 181.62 3,600 59.45 +4.86
Functions 117.62 226.76 1,920 32.29 +14.18

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 493.49
functions.variables.update 439.49
account.name.update 438.66
tablesdb.rows.update 438.16
account.prefs.update 435.92

@HarshMN2345
HarshMN2345 force-pushed the fix/abuse-code-guesses-every-caller branch from 769b562 to 1a2b9d8 Compare November 11, 2026 07:12
@HarshMN2345
HarshMN2345 merged commit a8ca30d into main Nov 11, 2026
68 checks passed
ChiragAgg5k added a commit that referenced this pull request Nov 12, 2026
…and MFA routes

Backports #15470 to the label-based abuse hook on 2.7.x, with its e2e tests.
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