Repository navigation
fix(abuse): count code guesses for every caller on the recovery and session routes (appwrite:appwrite:alert#4729) - #15470
Conversation
…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]>
|
Review complete. 🟠 1 high · 🟡 2 medium 💬 Inline comments (2)
📍 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
🧹 Nitpicks (1) — 🟢 1 low
The change flips
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 Reviewed commit: 587d16f |
🟢 Tier S · Ready to mergeThe 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.
📂 Walkthrough · 10
🔇 Filtered out · 1Findings Hansi considered but did not post.
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
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
| ->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), | ||
| ]) |
There was a problem hiding this comment.
🟡 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)]) |
There was a problem hiding this comment.
🟠 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.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
769b562 to
1a2b9d8
Compare
…and MFA routes Backports #15470 to the label-based abuse hook on 2.7.x, with its e2e tests.
What does this PR do?
Code scanning alert #4729. The
everyoneflag 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:PUT /v1/account/recovery/otpPOST /v1/account/sessions/tokencreateSessionstep accepts a secret verified with the code proofPUT /v1/account/sessions/phonePUT /v1/account/sessions/magic-urlPUT /v1/account/mfa/challengespackages/auth/src/Proofs/Code.phpdefaults to six digits, so the space is 10^6. Nothing else bounds the attempts:app/controllers/shared/api/auth.phpreturns early for keys and privileged callers, andUser::tokenVerify()records no failed attempt. A key holdingsessions.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 holdsusers.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 onmain(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 environmentalKeyTest/ListCacheTestones).tests/e2e/General/AbuseTest.php— two newabuseEnabledtests drive the real API with a project key: ten wrong six-digit guesses answer 401, the eleventh answers 429, onPUT /v1/account/recovery/otpandPOST /v1/account/sessions/token. Both fail onmain(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.Related PRs
🤖 Generated with Claude Code