Repository navigation
fix(abuse): count verification code guesses for every caller (appwrite:appwrite:alert#4645) - #15277
Conversation
Every API key also holds the 'public' scope, so any key — including a narrowly scoped one and the ephemeral keys handed to function runtimes — reaches the verification confirmation routes. The abuse hook exempts keys and privileged console callers from counting: it peeks at the limiter instead of hitting it and never refuses the request. On PUT /v1/account/verifications/email/otp and PUT /v1/account/verifications/phone that limit is the only cap on guesses at a six digit code, the user and the secret come from the request, and a wrong guess costs the caller nothing — so a key could walk the 10^6 code space and verify an address or phone number it has no access to, an outcome otherwise gated behind users.write. A limit can now declare that it counts for everyone, and the abuse hook decides per limit instead of per caller. The exemption still covers the limits that only pace legitimate server side work; the three verification confirmations opt out of it. The release hook mirrors the same rule, so a failure limit a trusted caller was counted for still gives its hit back on success. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Review complete. 🟠 1 high · 🟡 1 medium 💬 Inline comments (1)
📍 Findings outside the diff (1) — 🟠 1 high — defects on lines GitHub can't attach comments to🟠 High — Sibling secret-confirmation routes stay key-exempt · src/Appwrite/Platform/Modules/Account/Http/Account/Recovery/Update.php The PR's own rule — a limit that is the only cap on guesses at a secret the request carries must count every caller — is applied only to the three This PR introduces an
Reviewed commit: 943f0cb |
🟢 Tier S · Ready to mergeThe abuse-limit model now marks limits that apply to trusted callers, and the API hooks use that per-limit policy when counting requests and releasing failure reservations. The email, email OTP, and phone verification confirmation routes opt into limits for every caller, with tests covering the limit policy and route declarations.
📂 Walkthrough · 7
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
Adds an everyone exemption flag to the abuse rate-limit system so trusted callers skip most limits, with opt-in counting on the three email/phone verification confirmation routes.
Key findings
- 🟠 Sibling secret-confirmation routes stay key-exempt — Update.php
- 🟡 Default
everyone: falseexempts trusted callers from all existing limits — api.php:624
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
What
Code scanning alert #4645: any API key can reach the account verification confirmation routes with abuse limits effectively disabled, leaving unlimited guesses at the six digit email and phone verification codes.
Two reasonable decisions compose into a hole:
app/controllers/shared/api.phpappendspublicto every key's scopes, so any project key, organization key or ephemeral runtime key passes the any-of scope gate on thePUT /v1/account/verifications/*routes.peek()ed the limiter instead ofcheck()ing it and returned$shouldCheckAbuse && $result->limited, so the counter never advanced and the request was never refused — only rate limit headers came back.On
PUT /v1/account/verifications/email/otpandPUT /v1/account/verifications/phonethat limit is the only defense.userIdandsecretcome from the request, the target user is loaded with authorization skipped,User::tokenVerify()keeps no failed attempt state, and the code is six digits (new Code(), default length 6) living 15 minutes (email OTP) or an hour (phone). So a key — including one scoped to something unrelated — could walk the code space and mark an arbitrary user's email or phone verified, which grants theusers/verifiedpermission dimension and defeats account-claim protections. The same outcome is otherwise gated behindusers.write.How
A
Limitnow carries whether it counts for everyone, and the abuse hook decides per limit instead of per caller:Limit::counts(bool $trusted)holds the rule: a trusted caller (key or privileged console caller) is exempt from limits that only pace legitimate work, never from a limit that is the only cap on guesses at a secret the request carries.email,email/otp,phone) opt out of the exemption. Each is keyed per user and per hour, so a key doing the one legitimate confirmation per user is unaffected; 10 guesses per hour against 10^6 codes is not a brute force.failureslimit a trusted caller was counted for still gives its reserved hit back on success. No current limit is bothfailuresandeveryone, but the init/release pair must not drift.Everything else keeps today's behavior: the exemption still applies to every other limit, and
_APP_OPTIONS_ABUSE=disabledstill disables counting entirely.Tests
TDD — both tests fail on
main(Error: Unknown named parameter $everyone, and the route assertions fail) and pass with the fix:tests/unit/Abuse/LimitTest.php— a throughput limit does not count a trusted caller; a limit declared for everyone does.tests/unit/Abuse/RoutesTest.php— contract lock over the registered routes: every limit on the three verification confirmation paths counts for every caller, so the exemption cannot creep back in on a new limit.tests/unit/Abuse(15 tests) green, Pint and PHPStan clean on the changed files, Rector dry-run clean. There is no e2e coverage because the e2e environment ships_APP_OPTIONS_ABUSE=disabled, so no limit is enforced there for any caller.Follow-ups, not in this PR
sessions.write, which keys can legitimately hold:POST /v1/account/sessions/token,PUT /v1/account/sessions/phone,PUT /v1/account/recovery/otp,PUT /v1/account/sessions/magic-url. They were not newly opened by thepublicappend, and flipping them changes rate limiting for busy server side auth flows (notably theprojectId,url,ipfailure limits), so that is a maintainer call rather than part of a security patch. The mechanism added here is the one-argument fix if you want it.tokensdocument, so a wrong guess costs the attacker something regardless of who the caller is or whether the middleware ran. Needs a collection attribute plus a migration.publicto key scopes is right at all:publicappears in no file underapp/config/scopes, so it acts as an unlisted catch-all. An explicit per-route opt-in for the endpoints keys genuinely should reach would be tighter.🤖 Generated with Claude Code