serveDeno/serveBun drop the client address: ctx.ip is always empty, so rate-limit shares one bucket for every caller #116

Closed
opened 2026-10-06 02:05:06 +00:00 by chrnx · 1 comment
Owner

Problem

serveDeno and serveBun know the client's socket address but never give it to HTTP requests. ctx.ip is therefore always '' on those runtimes unless trustProxy is on, and with trustProxy it can only come from x-forwarded-for.

  • src/deno.ts: the WebSocket branch passes info.remoteAddr.hostname to toWsRequest, but the HTTP branch calls app.fetch(request) and drops info.
  • src/bun.ts: the same pattern. server.requestIP(request) feeds the WebSocket path, and HTTP gets a bare app.fetch(request).
  • src/http/web.ts (~L246): the fetch path sets ip from x-forwarded-for when trustProxy is on, and to '' otherwise. The comment says "there's no socket peer address to fall back to on the fetch path". That is true for a bare app.fetch, but not when core's own adapter owns the server.

Node's listen() does not have this problem: deriveIp falls back to req.socket.remoteAddress.

Impact

Anything that keys on ctx.ip breaks silently on Deno and Bun. The worst case is @green-tea/rate-limit. Its default keyOf folds '' into one unknown bucket, so every client shares one counter per rule. A single caller who spends a sign-in limit (5 per 15 minutes) locks out every user. The fail-closed behavior turns a brute-force guard into a denial-of-service lever.

The plugin README mentions that ip is empty through app.fetch(), but it is just as empty through serveDeno/serveBun, which are the supported way to serve on those runtimes.

Repro (Deno, core 26.9.0-beta.2)

@Route('/') class C { @Get('/ip') ip(@needs('req') req: { ip: string }) { return { ip: req.ip }; } }
@Module({ mountpoint: '/', controllers: [C] }) class M {}
await serveDeno(createApp({ modules: [M] }), { port: 4000 });
// curl localhost:4000/ip  →  {"ip":""}

Found while integrating @green-tea/rate-limit on Deno in a downstream migration. The workaround there is to deploy behind a proxy with trustProxy: true.

Expected

On Deno and Bun, ctx.ip should be the socket peer address, with x-forwarded-for taking over only when trustProxy is on, the same as Node's deriveIp.

Suggested direction

Let the adapters pass the peer address into the fetch path. For example, an internal app.fetch(request, { remoteAddr }) (or a non-public symbol) that web.ts uses as the untrusted fallback in place of ''. Plain app.fetch(request) from a user-owned server keeps today's behavior.

Acceptance

  • serveDeno and serveBun produce a non-empty ctx.ip for a socket client, both with and without trustProxy.
  • With trustProxy, x-forwarded-for's first hop wins. Without it, a spoofed x-forwarded-for is ignored.
  • A test per adapter, plus a rate-limit regression: two clients get separate buckets on Deno.
## Problem `serveDeno` and `serveBun` know the client's socket address but never give it to HTTP requests. `ctx.ip` is therefore always `''` on those runtimes unless `trustProxy` is on, and with `trustProxy` it can only come from `x-forwarded-for`. - `src/deno.ts`: the WebSocket branch passes `info.remoteAddr.hostname` to `toWsRequest`, but the HTTP branch calls `app.fetch(request)` and drops `info`. - `src/bun.ts`: the same pattern. `server.requestIP(request)` feeds the WebSocket path, and HTTP gets a bare `app.fetch(request)`. - `src/http/web.ts` (~L246): the fetch path sets `ip` from `x-forwarded-for` when `trustProxy` is on, and to `''` otherwise. The comment says *"there's no socket peer address to fall back to on the fetch path"*. That is true for a bare `app.fetch`, but not when core's own adapter owns the server. Node's `listen()` does not have this problem: `deriveIp` falls back to `req.socket.remoteAddress`. ## Impact Anything that keys on `ctx.ip` breaks silently on Deno and Bun. The worst case is `@green-tea/rate-limit`. Its default `keyOf` folds `''` into one `unknown` bucket, so **every client shares one counter per rule**. A single caller who spends a sign-in limit (5 per 15 minutes) locks out every user. The fail-closed behavior turns a brute-force guard into a denial-of-service lever. The plugin README mentions that `ip` is empty through `app.fetch()`, but it is just as empty through `serveDeno`/`serveBun`, which are the supported way to serve on those runtimes. ## Repro (Deno, core 26.9.0-beta.2) ```ts @Route('/') class C { @Get('/ip') ip(@needs('req') req: { ip: string }) { return { ip: req.ip }; } } @Module({ mountpoint: '/', controllers: [C] }) class M {} await serveDeno(createApp({ modules: [M] }), { port: 4000 }); // curl localhost:4000/ip → {"ip":""} ``` Found while integrating `@green-tea/rate-limit` on Deno in a downstream migration. The workaround there is to deploy behind a proxy with `trustProxy: true`. ## Expected On Deno and Bun, `ctx.ip` should be the socket peer address, with `x-forwarded-for` taking over only when `trustProxy` is on, the same as Node's `deriveIp`. ## Suggested direction Let the adapters pass the peer address into the fetch path. For example, an internal `app.fetch(request, { remoteAddr })` (or a non-public symbol) that `web.ts` uses as the untrusted fallback in place of `''`. Plain `app.fetch(request)` from a user-owned server keeps today's behavior. ## Acceptance - `serveDeno` and `serveBun` produce a non-empty `ctx.ip` for a socket client, both with and without `trustProxy`. - With `trustProxy`, `x-forwarded-for`'s first hop wins. Without it, a spoofed `x-forwarded-for` is ignored. - A test per adapter, plus a rate-limit regression: two clients get separate buckets on Deno.
chrnx added the bug label 2026-10-06 02:05:21 +00:00
Author
Owner

Fixed in 26.11.0-rc.1 (b86cd60, f78c9dc). From the changelog:

  • serveDeno() and serveBun() populate ctx.ip with the client's address (#116). The fetch path had no way to learn the socket peer, so every HTTP request on Deno and Bun saw ctx.ip as '', and anything keyed on it put every client in one bucket — the rate-limit plugin, where one client could exhaust the limit for everyone, above all. Both adapters now hand the peer to app.fetch, and ctx.ip follows the same rule as Node's listen(): the first x-forwarded-for hop when trustProxy is on and the header is present, the socket peer otherwise. That includes trustProxy with no x-forwarded-for, which used to answer '' on the fetch path and now falls back to the peer, as Node always has. Addresses are passed through as each runtime reports them, so the IPv6 loopback is ::1 on all three but Node may report an IPv4 client as ::ffff:127.0.0.1. A bare app.fetch(request) from your own code has no socket to report and still answers '' (or the forwarded hop), and so does a Request copied from the one the adapter received. No signature changed. The edge adapter is unchanged: trusting cf-connecting-ip is only correct behind Cloudflare, and needs its own decision.
Fixed in **26.11.0-rc.1** (`b86cd60`, `f78c9dc`). From the changelog: - **`serveDeno()` and `serveBun()` populate `ctx.ip` with the client's address** (#116). The fetch path had no way to learn the socket peer, so every HTTP request on Deno and Bun saw `ctx.ip` as `''`, and anything keyed on it put every client in one bucket — the rate-limit plugin, where one client could exhaust the limit for everyone, above all. Both adapters now hand the peer to `app.fetch`, and `ctx.ip` follows the same rule as Node's `listen()`: the first `x-forwarded-for` hop when `trustProxy` is on and the header is present, the socket peer otherwise. That includes `trustProxy` with no `x-forwarded-for`, which used to answer `''` on the fetch path and now falls back to the peer, as Node always has. Addresses are passed through as each runtime reports them, so the IPv6 loopback is `::1` on all three but Node may report an IPv4 client as `::ffff:127.0.0.1`. A bare `app.fetch(request)` from your own code has no socket to report and still answers `''` (or the forwarded hop), and so does a `Request` copied from the one the adapter received. No signature changed. The edge adapter is unchanged: trusting `cf-connecting-ip` is only correct behind Cloudflare, and needs its own decision.
chrnx closed this issue 2026-11-18 01:04:08 +00:00
Sign in to join this conversation.