Skip to content

fix(security): rate-limit guest transaction writes and compare MFA codes in constant time - #14762

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/semgrep-mfa-transactions
Oct 22, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/semgrep-mfa-transactions

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What

Resolves the open Semgrep code-scanning alerts for guest-write-without-abuse-limit (Databases, DocumentsDB and VectorsDB transactions) and secret-compare-timing (MFA challenges).

Why

Guests hold documents.write, rows.write, documentsdb.documents.write and vectorsdb.documents.write (app/config/roles.php), so POST/PATCH/DELETE /v1/{databases,tablesdb,documentsdb,vectorsdb}/transactions[/:transactionId] were callable by unauthenticated clients with no abuse limit. The shared api hook treats a missing abuse-limit as unlimited. Their sibling transactions/:transactionId/operations route and the document/row write routes are already limited.

The transaction routes now carry the same labels as those siblings: key ip:{ip},method:{method},url:{url},userId:{userId}, APP_LIMIT_WRITE_RATE_DEFAULT * 2 for create/update and APP_LIMIT_WRITE_RATE_DEFAULT for delete, per APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT. TablesDB is included even though Semgrep did not flag it (its array scope label escapes the rule's pattern), since it is the same route behind the newer API name.

MFA email/phone/custom codes and TOTP values were compared with ===, whose running time depends on how long the matching prefix is. They now use hash_equals; a missing or non-string stored code still fails verification.

Baseline

.semgrep/baseline.json is regenerated with node .semgrep/baseline.js update from a Semgrep 1.179.0 scan. It drops the 13 entries this resolves. The OAuth2 callback entry in app/controllers/api/account.php stays, with its line number updated.

Tests

tests/e2e/General/AbuseTest.php gains create/update/delete transaction cases for all four APIs in the abuseEnabled group, using the existing assertGuestLimited helper: the request after the limit must return 429. The MFA change keeps verification behavior identical, which the existing Account MFA e2e tests cover; timing cannot be asserted in a test.

Overlaps with #14320, which makes the same hash_equals change but rewrites the whole baseline file and has no rate-limit fix.

Related: appwrite-labs/cloud#6666

…des in constant time

Guests hold documents.write, rows.write and the DocumentsDB/VectorsDB write
scopes, so the transaction create/update/delete routes on every database API
were reachable without an abuse limit while their operations sibling and the
document/row routes were limited. They now use the same key and limits as
those siblings.

MFA challenge codes and TOTP values were compared with ===, whose running
time depends on the matching prefix; hash_equals removes that signal.

The Semgrep baseline drops the findings these resolve, plus a stale account
entry that no longer matched.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This change wires abuse-key, abuse-limit, and abuse-time labels onto the MFA challenge endpoints (Custom, Email, Phone, TOTP) and the transaction create/update/delete endpoints across the four database APIs, so they are protected by the shared abuse-mechanism rate limiting. The {ip}, {method}, {url}, {userId} placeholders resolve against the existing enforcement in app/controllers/shared/api.php, and the limits reuse the documented _APP_LIMIT_WRITE_RATE_* constants. The e2e AbuseTest is extended to exercise the newly limited routes, and the semgrep baseline is refreshed accordingly.

Reviewed commit: d119963

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Transaction create, update, and delete routes across Databases, TablesDB, DocumentsDB, and VectorsDB now apply guest abuse limits, and MFA challenge/TOTP code checks use constant-time comparisons. E2E abuse coverage and the Semgrep baseline were updated alongside those changes; the latest commit restores the OAuth2 callback finding to the baseline.

Latest changes: The newest commit adds the OAuth2 POST callback finding back to the Semgrep baseline so the known finding remains matched.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
.semgrep/baseline.json Updates the Semgrep baseline for the transaction and MFA fixes and retains the OAuth2 callback finding.
src/Appwrite/Auth/MFA/Challenge/{Custom,Email,Phone,TOTP}.php Compares MFA challenge and TOTP codes with hash_equals while preserving failure for missing or non-string stored codes.
src/Appwrite/Platform/Modules/Databases/Http/{Databases,DocumentsDB,TablesDB,VectorsDB}/Transactions/{Create,Update,Delete}.php Adds abuse-limit labels to transaction lifecycle routes.
tests/e2e/General/AbuseTest.php Adds guest rate-limit cases for transaction creation, update, and deletion across the four database APIs.

Reviewed the commits since d119963 · 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 S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 22, 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.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/semgrep-mfa-transactions (after).

Metric Before After Change
🚀 Requests/sec 178.11 191.11 🟢 +7.3%
⏱️ Latency P50 95.98 ms 90.56 ms 🟢 -5.6%
⏱️ Latency P95 232.81 ms 214.44 ms 🟢 -7.9%
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 90.56 214.44 12,027 191.11 -18.37
Account 174.59 309.85 633 10.51 -34.68
TablesDB 87.76 161.85 6,541 106.45 -27.04
Storage 82.68 191.7 3,165 53.43 -14.03
Functions 126.69 246.84 1,688 29.24 -9.88

Top API waits (after)

API request Max wait (ms)
functions.variables.update 467.66
account.prefs.update 429.7
account.name.update 406.22
functions.create 393.32
tablesdb.rows.update 381.18

A local scan missed it, so the regenerated baseline dropped an entry that CI
still reports. Only its line number moved.
@ChiragAgg5k
ChiragAgg5k merged commit 4e5a556 into main Oct 22, 2026
65 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/semgrep-mfa-transactions branch October 22, 2026 10:51
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Only the MFA challenge comparison part of #14762 is ported here.

(cherry picked from commit 4e5a556)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Transaction create, update and delete had no abuse labels, so guests could open and churn transactions without a limit. Only the transaction rate-limit part of #14762 is backported.

(cherry picked from commit 4e5a556)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…actions

fix(security): rate-limit guest transaction writes and compare MFA codes in constant time

(cherry picked from commit 4e5a556)
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