Skip to content

fix(abuse): count verification code guesses for every caller (appwrite:appwrite:alert#4645) - #15277

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/verification-code-abuse-limit-for-keys
Nov 10, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/verification-code-abuse-limit-for-keys

Conversation

@claudear

@claudear claudear commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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.php appends public to every key's scopes, so any project key, organization key or ephemeral runtime key passes the any-of scope gate on the PUT /v1/account/verifications/* routes.
  • The abuse hook exempted keys and privileged console callers wholesale: it peek()ed the limiter instead of check()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/otp and PUT /v1/account/verifications/phone that limit is the only defense. userId and secret come 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 the users/verified permission dimension and defeats account-claim protections. The same outcome is otherwise gated behind users.write.

How

A Limit now carries whether it counts for everyone, and the abuse hook decides per limit instead of per caller:

->label('abuse', [Limit::attempts(['url', 'param.userId'], 10, everyone: true)])
  • 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.
  • The three verification confirmations (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.
  • The release shutdown hook mirrors the same per-limit rule, so a failures limit a trusted caller was counted for still gives its reserved hit back on success. No current limit is both failures and everyone, 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=disabled still 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

  • Other routes verify a short code behind 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 the public append, and flipping them changes rate limiting for busy server side auth flows (notably the projectId,url,ip failure 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.
  • Per-token failed attempt counter on the tokens document, 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.
  • Whether appending public to key scopes is right at all: public appears in no file under app/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

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

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

Copy link
Copy Markdown

Review complete. 🟠 1 high · 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Default everyone: false exempts trusted callers from all existing limits — api.php:624
📍 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 /v1/account/verifications/* confirm routes, but the new counts() gate at app/controllers/shared/api.php:624 exempts trusted callers everywhere else. The sibling confirmation routes still declare limits without everyone: true: Recovery/Update.php, Recovery/OTP/Update.php (recovery token/OTP brute-force), Sessions/Phone/Update.php, Sessions/MagicURL/Update.php, Sessions/Token/Create.php (magic-URL/phone/token secret guessing), Sessions/Email/Create.php (password brute-force via failure limits), MFA/Challenges/Update.php (OTP guessing), Passkeys/Verification/Update.php, and Teams/Memberships/Status/Update.php (membership secret). Each carries a secret in the request and is reachable with an API key or privileged console session, so a compromised or over-scoped key can now walk the whole code/token space on these routes.


This PR introduces an everyone parameter on Limit::attempts()/Limit::failures() plus a counts() gate, wired through app/controllers/shared/api.php so API-key and console callers no longer count against route limits by default; the three /v1/account/verifications/* confirmation routes opt in with everyone: true to keep brute-force protection. Unit tests for the limit and routes behavior are added.

Files Change
src/Appwrite/Abuse/Limit.php Adds everyone param to attempts()/failures() and a counts(bool $trusted) gate exempting trusted callers unless opted in
app/controllers/shared/api.php Applies the trusted-caller exemption in the abuse init/shutdown hooks
src/Appwrite/Platform/Modules/Account/Http/Account/Verifications/{Email,Email/OTP,Phone}/Update.php Set everyone: true on the token/OTP confirmation limits
tests/unit/Abuse/{LimitTest,RoutesTest}.php New unit tests covering limit accounting and route opt-in invariants

Reviewed commit: 943f0cb

@appwrite

appwrite Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

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

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 7
File Change
app/controllers/shared/api.php Uses each limit's caller-counting policy in the abuse init and shutdown hooks.
src/Appwrite/Abuse/Limit.php Adds the everyone flag and caller-specific limit counting behavior.
src/Appwrite/Platform/Modules/Account/Http/Account/Verifications/Email/OTP/Update.php Counts email OTP confirmation attempts from all callers.
src/Appwrite/Platform/Modules/Account/Http/Account/Verifications/Email/Update.php Counts email verification confirmation attempts from all callers.
src/Appwrite/Platform/Modules/Account/Http/Account/Verifications/Phone/Update.php Counts phone verification confirmation attempts from all callers.
tests/unit/Abuse/LimitTest.php Covers trusted-caller exemptions and limits that count for everyone.
tests/unit/Abuse/RoutesTest.php Locks the abuse-limit policy for the verification confirmation routes.

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

@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

@github-actions

github-actions Bot commented Nov 4, 2026

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.

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

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: false exempts trusted callers from all existing limits — api.php:624

Comment thread app/controllers/shared/api.php Resolved
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/verification-code-abuse-limit-for-keys (after).

Metric Before After Change
🚀 Requests/sec 211.89 218.16 ⚪ +3%
⏱️ Latency P50 79.2 ms 79.64 ms ⚪ +0.6%
⏱️ Latency P95 198.21 ms 183.51 ms 🟢 -7.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 79.64 183.51 13,680 218.16 -14.69
Account 153.37 306.15 720 12.03 +33.31
TablesDB 78.59 147.34 7,440 120.58 -11.04
Storage 70.48 163.04 3,600 60.36 -15.97
Functions 109.91 210.25 1,920 32.83 -15.51

Top API waits (after)

API request Max wait (ms)
account.name.update 477.02
account.prefs.update 393.62
functions.create 387.49
tablesdb.rows.create 352.03
functions.variables.update 344.33

@ChiragAgg5k
ChiragAgg5k merged commit 6a67424 into main Nov 10, 2026
76 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/verification-code-abuse-limit-for-keys branch November 10, 2026 20:59
ChiragAgg5k added a commit that referenced this pull request Nov 12, 2026
Backports #15277 and #15280 to the label-based abuse hook on 2.7.x: account- and address-keyed limits are kept per project, and the verification confirmation limits count every caller. Ports the two cross-project e2e tests and adds one for the verification limit.
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.

3 participants