Skip to content

fix(abuse): key rate limits on route path, not request host - #14785

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/abuse-key-host
Oct 22, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/abuse-key-host

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Rate-limit keys now depend only on server-side request facts, not on the hostname the request names.

  • app/controllers/shared/api.php: the {url} abuse-key component is the matched route path instead of hostname + route path, in both the init check and the shutdown reset. Key shape is otherwise unchanged; labels still scope by project, IP, user, email, and so on.
  • app/realtime.php: the realtime connection limit keys on the fixed /v1/realtime path instead of the raw request URI.
  • packages/http: TrustedHeaders gains host and port lists, empty by default. The Swoole request reads x-forwarded-host / x-forwarded-port only when they are named there, the same way ip and proto already work. Before this change it always believed them.
  • Appwrite\Utopia\Request trusts x-forwarded-host / x-forwarded-port only when the connection comes from an address in _APP_TRUSTED_PROXIES, matching how getIP() treats X-Forwarded-For. Deployments behind a CDN or a proxy on a private network (Traefik, Fastly into the cluster) keep their current hostname resolution.

Existing buckets are keyed differently after deploy, so counters start fresh once.

Test plan

  • bin/monorepo check http, bin/monorepo test http (new SwooleRequestTest cases: forwarded host/port ignored by default, honoured when named, non-numeric port skipped)
  • tests/unit/Utopia/RequestTest.php: forwarded host/port ignored from an untrusted client, honoured from a trusted proxy
  • Pint, PHPStan on touched files, composer refactor:check
  • CI Abuse lane: AbuseTest::testAbuseLimitIgnoresHost exhausts the POST /account limit, then expects 429 with a different Host and with a different X-Forwarded-Host

Related: appwrite-labs/cloud#6674

@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

  • 🟠 Trusted host/port headers read the spoofable leftmost value — Request.php:169
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Dropping hostname collapses per-vhost abuse counters (api.php:613) — The abuse key no longer contains any hostname: '{url}' => $route->getPath() (app/controllers/shared/api.php:613, and again at line 993).

The change introduces a shared TrustedHeaders catalog and a trustedValue() helper in packages/http/src/Request.php, wires getHostname()/getPort() through trusted-proxy gating in both the Swoole adapter and Appwrite\Utopia\Request, and simplifies abuse-key construction in app/controllers/shared/api.php and app/realtime.php. Trust gating on remote_addr membership is a solid direction, and the IP path correctly adopts a rightmost-untrusted model. However, the host/port path selects the leftmost forwarded value, contradicting the PR's own IP trust model and leaving hostname spoofing open on appending proxies. The abuse key also lost its hostname component without a replacement tenant discriminator, collapsing per-project counters behind shared egress IPs.

Files Change
packages/http/src/Request.php, packages/http/src/TrustedHeaders.php New trusted-header catalog and trustedValue() helper gating host/port resolution behind trusted proxies
packages/http/src/Adapter/Swoole/Request.php, src/Appwrite/Utopia/Request.php getHostname()/getPort() now honor trusted headers; getIP() documents the rightmost-untrusted policy
app/controllers/shared/api.php, app/realtime.php Abuse keys rebuilt from route path + IP, hostname removed; realtime key switched to the literal /v1/realtime route
packages/http/tests/SwooleRequestTest.php, tests/unit/Utopia/RequestTest.php, tests/e2e/General/AbuseTest.php Tests covering trusted-host parsing and spoofed-header abuse behavior

Reviewed commit: c7f04ba

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

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Abuse-limit keys now use the matched route path, and realtime connection limits use the fixed realtime path. Forwarded host and port headers are configurable in the HTTP package and trusted by Appwrite only for requests from configured proxies. Trusted forwarded host and port values now use the nearest proxy’s comma-separated entry, with regression coverage for host selection.

Latest changes: The latest commit changes trusted forwarded host and port parsing to select the rightmost value and updates the host test to verify that a client-supplied leftmost value is ignored.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 9
File Change
app/controllers/shared/api.php Uses the matched route path in abuse-limit key construction and reset.
app/realtime.php Keys realtime connection limits on the fixed route path.
packages/http/src/Adapter/Swoole/Request.php Uses configured trusted forwarded host and port headers.
packages/http/src/Request.php Adds shared trusted-header parsing, selecting the nearest proxy’s value.
packages/http/src/TrustedHeaders.php Adds normalized host and port trusted-header lists.
packages/http/tests/SwooleRequestTest.php Tests forwarded host and port trust behavior, including nearest-value host selection.
src/Appwrite/Utopia/Request.php Trusts forwarded host and port only when the connection comes from a configured trusted proxy.
tests/e2e/General/AbuseTest.php Verifies account abuse limits remain shared across differing host headers.
tests/unit/Utopia/RequestTest.php Tests forwarded host and port handling for trusted and untrusted proxy sources.

Merged main (5d68088); no changes to this PR's code since e6cedd8 · 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

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

This PR adds trusted-proxy handling for host/port headers and removes the client-controllable hostname from abuse-limit keys, but the new header-trust logic reads the spoofable leftmost X-Forwarded-Host value.

Key findings

  • 🟠 Trusted host/port headers read the spoofable leftmost value — Request.php:169

Comment thread packages/http/src/Request.php Resolved
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/abuse-key-host (after).

Metric Before After Change
🚀 Requests/sec 186.17 188.64 ⚪ +1.3%
⏱️ Latency P50 90.5 ms 91.34 ms ⚪ +0.9%
⏱️ Latency P95 219.72 ms 217.13 ms ⚪ -1.2%
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 91.34 217.13 11,685 188.64 -2.58
Account 170.54 323.82 615 10.2 -64.68
TablesDB 88.98 168.29 6,355 104.45 -7.54
Storage 83.22 202.87 3,075 52.64 +4.93
Functions 126.57 242.97 1,640 28.95 -50.36

Top API waits (after)

API request Max wait (ms)
account.prefs.update 504.05
account.name.update 472.03
functions.create 423.96
storage.buckets.create 392.79
tablesdb.rows.create 354.14

@ChiragAgg5k

Copy link
Copy Markdown
Member Author

On the tenki nitpick about dropping the hostname from {url}: this is intended. The hostname comes from the request, so it never worked as a tenant boundary, and projects on the shared regional API endpoint already shared these buckets. Labels that need per-project scoping can name {projectId} explicitly, as some already do.

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

# Conflicts:
#	tests/e2e/General/AbuseTest.php
@ChiragAgg5k
ChiragAgg5k merged commit 5cb4be8 into main Oct 22, 2026
68 checks passed
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…14785)

The {url} abuse-key component included the request hostname, so a client could reset any limit by changing the Host or X-Forwarded-Host header. It is now the matched route path, and the realtime connection limit keys on /v1/realtime instead of the raw URI. The forwarded host and port trust changes live in packages/http, which 1.9 does not have, and are not ported.

(cherry picked from commit 5cb4be8)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…14785)

The {url} abuse-key component included the request hostname, so a client could reset any limit by changing the Host or X-Forwarded-Host header. It is now the matched route path, and the realtime connection limit keys on /v1/realtime instead of the raw URI. The forwarded host and port trust changes live in packages/http, which 1.9 does not have, and are not ported.

(cherry picked from commit 5cb4be8)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…14785)

The {url} abuse-key component included the request hostname, so a client could reset any limit by changing the Host or X-Forwarded-Host header. It is now the matched route path, and the realtime connection limit keys on /v1/realtime instead of the raw URI. The forwarded host and port trust changes live in packages/http, which 1.9 does not have, and are not ported.

(cherry picked from commit 5cb4be8)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…14785)

The {url} abuse-key component included the request hostname, so a client could reset any limit by changing the Host or X-Forwarded-Host header. It is now the matched route path, and the realtime connection limit keys on /v1/realtime instead of the raw URI. The forwarded host and port trust changes live in packages/http, which 1.9 does not have, and are not ported.

(cherry picked from commit 5cb4be8)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…14785)

The {url} abuse-key component included the request hostname, so a client could reset any limit by changing the Host or X-Forwarded-Host header. It is now the matched route path, and the realtime connection limit keys on /v1/realtime instead of the raw URI. The forwarded host and port trust changes live in packages/http, which 1.9 does not have, and are not ported.

(cherry picked from commit 5cb4be8)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
fix(abuse): key rate limits on route path, not request host

(cherry picked from commit 5cb4be8)
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