Skip to content

fix(account): rate limit token session attempts per account - #14880

Merged
HarshMN2345 merged 2 commits into
mainfrom
fix-otp-abuse-per-account
Oct 23, 2026
Merged

HarshMN2345 merged 2 commits into
mainfrom
fix-otp-abuse-per-account

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

Token session routes (POST /v1/account/sessions/token, PUT /v1/account/sessions/magic-url, PUT /v1/account/sessions/phone) now also count attempts per account per route, alongside the existing IP + user key, using the array abuse-key form the abuse hook already supports.

Details are tracked internally.

Tested: php -l on the changed file.

@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Multi-key abuse-key duplicates X-RateLimit-* headers (account.php:1409) — The new two-key abuse-key label makes the rate-limit hook run its reporting closure once per key, and Response::addHeader appends rather than replaces (app/controllers/shared/api.php:630-632; packages/http/src/Response.php:463).

The change extends the abuse/rate-limit protection on three account token-verification routes by pairing a url:{url},userId:{param-userId} key with the existing IP-based key, so limits track the targeted account rather than only the caller's address.

Files Change
app/controllers/api/account.php Adds an array-valued abuse-key label (IP key plus url:{url},userId:{param-userId} key) to the token verification endpoints.

One low-severity issue was found: with multiple keys the rate-limit reporting closure appends a second set of X-RateLimit-* header values when the second key reports a smaller remaining, since Response::addHeader appends rather than replaces; the fix is to use setHeader in the reporting closure.

Reviewed commit: 3c2a64f

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

An earlier minor finding about missing cross-IP E2E coverage remains open.

The token, magic-URL, and phone session routes now apply an additional per-account abuse limit alongside the existing IP-and-user limit. The account-scoped key includes the project, route, and submitted user ID so accounts with matching IDs in different projects are isolated.

Latest changes: The latest commits add the project ID to the account-scoped abuse key on all three token-session routes.

Verdict New comments Fixed Still open
✅ Approved 0 1 1
Fix with agent prompt
### Issue 1
app/controllers/api/account.php:1409
**Test the per-account limit across IPs**

The existing abuse test covers email sign-in from one IP, so it does not verify that these new keys enforce a shared per-account cap across different IPs. An E2E case exercising the token-session route from multiple source IPs would cover the behavior this change adds.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 1
File Change
app/controllers/api/account.php Adds project-scoped per-account abuse keys to token, magic-URL, and phone session routes.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 1
  • Scope the account bucket by project · app/controllers/api/account.php:1409

Reviewed the commits since 3c2a64f · 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 A · See the inline comments. Summary

Comment thread app/controllers/api/account.php Resolved
Comment thread app/controllers/api/account.php Outdated
))
->label('abuse-limit', 10)
->label('abuse-key', 'ip:{ip},userId:{param-userId}')
->label('abuse-key', ['ip:{ip},userId:{param-userId}', 'url:{url},userId:{param-userId}'])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Test the per-account limit across IPs

The existing abuse test covers email sign-in from one IP, so it does not verify that these new keys enforce a shared per-account cap across different IPs. An E2E case exercising the token-session route from multiple source IPs would cover the behavior this change adds.

Prompt To Fix With AI
This is a comment left during a code review.
Path: app/controllers/api/account.php
Line: 1409

Comment:
**Test the per-account limit across IPs**

The existing abuse test covers email sign-in from one IP, so it does not verify that these new keys enforce a shared per-account cap across different IPs. An E2E case exercising the token-session route from multiple source IPs would cover the behavior this change adds.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@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

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-otp-abuse-per-account (after).

Metric Before After Change
🚀 Requests/sec 186.58 198.36 🟢 +6.3%
⏱️ Latency P50 91.31 ms 88.32 ms ⚪ -3.3%
⏱️ Latency P95 214.1 ms 202.86 ms 🟢 -5.3%
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 88.32 202.86 12,540 198.36 -11.24
Account 161.86 289.23 660 11.08 -52.88
TablesDB 86.17 158.86 6,820 110.13 -16.03
Storage 80.74 188.04 3,300 55.04 -9.45
Functions 124.84 231.62 1,760 30.13 -7.8

Top API waits (after)

API request Max wait (ms)
account.name.update 458.35
storage.buckets.create 442.7
account.prefs.update 391.84
functions.create 383.46
functions.variables.update 356.08

@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 A · Looks good to merge. Summary

@HarshMN2345
HarshMN2345 merged commit d71104d into main Oct 23, 2026
31 checks passed
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…ount

fix(account): rate limit token session attempts per account

(cherry picked from commit d71104d)
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.

1 participant