feat(edge)!: trust cf-connecting-ip only under trustProxy, for HTTP and WebSocket (CARD-23, #119) #139

Merged
chrnx merged 1 commits from feature/fprd-001c-edge-cf-connecting-ip into develop 2026-10-23 05:49:47 +00:00
Owner

What and why

CARD-23, decision D21, core issue #119. Before this change, edgeHandler copied cf-connecting-ip into ctx.ip on every WebSocket upgrade, so any client chose its own address, and it never read the header for HTTP, where IP-keyed plugins saw ''. Now the header counts only under trustProxy, on both paths, and there is no new option.

How. The adapter can't decide on its own, because trustProxy is an app option. So:

  • src/edge.ts: edgeHandler records the header against the object it hands the app: the Request for HTTP, the WsRequest for an upgrade, whose ip now starts as ''. One helper, recordClient, serves both paths.
  • src/http/peer.ts: a second internal registry, next to the #116 peer one, with the same globalThis symbol registration because @green-tea/core/edge is its own bundle. trustedProxyClient(source, trustProxy) returns '' unless trustProxy is on. This is the single check both paths use.
  • src/http/web.ts (app.fetch): trustedProxyClient(...) || x-forwarded-for (trusted) || peer, so the header beats XFF under trustProxy (rule 3).
  • src/app/index.ts (app.upgrade): the same check overrides the WsRequest.ip when present. buildAppUpgrade (internal) now takes { limits, trustProxy }.
  • Nothing else writes to the registry, so Node, Deno, Bun, and a bare app.fetch/app.upgrade ignore the header as before (T6).
  • CHANGELOG.md: ### Breaking (edge WS) and ### Fixed (edge HTTP).

No public API changed: peer.ts isn't exported, and App/types.ts are unchanged.

Tests

  • test/edge-client-ip.test.ts (Node/Vitest): T1–T3 through edgeHandler itself; T4/T5 through app.upgrade with the value recorded the way edgeHandler records it; T6 through the Node listener (cf-connecting-ip ignored, peer IP kept). Also covers: a bare app.fetch/app.upgrade ignoring the header, XFF still applying without the header, and an adapter-supplied WS ip passing through. Against the old src, T1, T2, T4 and T5 fail.
  • test/edge/run.ts + worker.ts (real workerd via Miniflare): T1–T5 end to end, including the real WebSocketPair upgrade, with a trusted host for a trustProxy: true app. Against the old src this fails at T1.
Check Result
npm run lint 0 errors (50 existing no-explicit-any warnings, unchanged)
npm run format:check pass
npm run typecheck pass
npm test 925 passed
npm run build pass
npm run complexity:check every function ≤ 25
npm run test:deno 36 passed, 0 failed
npm run test:bun 26 pass, 0 fail
npm run test:edge PASS (incl. CARD-23 cases, router parity 1158 + 55)

No frozen router-parity capture changed.

Rebased on origin/develop after #134 and #135 merged. npm audit --audit-level=moderate passes locally on this branch now, and the Bun close-code tests are fixed on develop. Local checks were re-run after the rebase with the same results.

Unsure about

  • WS + x-forwarded-for: under trustProxy without cf-connecting-ip, an edge WS upgrade still gets ''. WS never derived XFF (a comment in buildWsRoutes marks that out of scope), and the card doesn't ask for it, so I didn't widen the scope.
  • Cloudflare without edgeHandler: an app deployed as { fetch: app.fetch } directly doesn't get the header, because only edgeHandler records it. I treated this as the conservative reading of "edge-only".
  • The registry is keyed by object identity. A Request copied before it reaches app.fetch loses the value, the same property the #116 peer registry has (and tests).

Docs follow-up (CARD-12)

  • Edge/deploy guide: ctx.ip on Cloudflare comes from cf-connecting-ip only with createApp({ trustProxy: true }), for HTTP and WebSocket. It takes precedence over XFF. Without trustProxy it is ''.
  • Rate-limit plugin edge docs: mention trustProxy (otherwise all clients share one bucket).
  • Migration note: edge WebSocket apps that relied on the header now need trustProxy: true.
## What and why CARD-23, decision D21, core issue #119. Before this change, `edgeHandler` copied `cf-connecting-ip` into `ctx.ip` on **every** WebSocket upgrade, so any client chose its own address, and it never read the header for HTTP, where IP-keyed plugins saw `''`. Now the header counts **only under `trustProxy`**, on both paths, and there is no new option. **How.** The adapter can't decide on its own, because `trustProxy` is an app option. So: - `src/edge.ts`: `edgeHandler` records the header against the object it hands the app: the `Request` for HTTP, the `WsRequest` for an upgrade, whose `ip` now starts as `''`. One helper, `recordClient`, serves both paths. - `src/http/peer.ts`: a second internal registry, next to the #116 peer one, with the same `globalThis` symbol registration because `@green-tea/core/edge` is its own bundle. `trustedProxyClient(source, trustProxy)` returns `''` unless `trustProxy` is on. This is the single check both paths use. - `src/http/web.ts` (`app.fetch`): `trustedProxyClient(...) || x-forwarded-for (trusted) || peer`, so the header beats XFF under `trustProxy` (rule 3). - `src/app/index.ts` (`app.upgrade`): the same check overrides the `WsRequest.ip` when present. `buildAppUpgrade` (internal) now takes `{ limits, trustProxy }`. - Nothing else writes to the registry, so Node, Deno, Bun, and a bare `app.fetch`/`app.upgrade` ignore the header as before (T6). - `CHANGELOG.md`: `### Breaking` (edge WS) and `### Fixed` (edge HTTP). No public API changed: `peer.ts` isn't exported, and `App`/`types.ts` are unchanged. ## Tests - `test/edge-client-ip.test.ts` (Node/Vitest): T1–T3 through `edgeHandler` itself; T4/T5 through `app.upgrade` with the value recorded the way `edgeHandler` records it; T6 through the Node listener (`cf-connecting-ip` ignored, peer IP kept). Also covers: a bare `app.fetch`/`app.upgrade` ignoring the header, XFF still applying without the header, and an adapter-supplied WS `ip` passing through. Against the old `src`, T1, T2, T4 and T5 fail. - `test/edge/run.ts` + `worker.ts` (real workerd via Miniflare): T1–T5 end to end, including the real `WebSocketPair` upgrade, with a `trusted` host for a `trustProxy: true` app. Against the old `src` this fails at T1. | Check | Result | |---|---| | `npm run lint` | 0 errors (50 existing `no-explicit-any` warnings, unchanged) | | `npm run format:check` | pass | | `npm run typecheck` | pass | | `npm test` | 925 passed | | `npm run build` | pass | | `npm run complexity:check` | every function ≤ 25 | | `npm run test:deno` | 36 passed, 0 failed | | `npm run test:bun` | 26 pass, 0 fail | | `npm run test:edge` | PASS (incl. CARD-23 cases, router parity 1158 + 55) | No frozen router-parity capture changed. **Rebased on `origin/develop` after #134 and #135 merged.** `npm audit --audit-level=moderate` passes locally on this branch now, and the Bun close-code tests are fixed on develop. Local checks were re-run after the rebase with the same results. ## Unsure about - **WS + `x-forwarded-for`:** under `trustProxy` without `cf-connecting-ip`, an edge WS upgrade still gets `''`. WS never derived XFF (a comment in `buildWsRoutes` marks that out of scope), and the card doesn't ask for it, so I didn't widen the scope. - **Cloudflare without `edgeHandler`:** an app deployed as `{ fetch: app.fetch }` directly doesn't get the header, because only `edgeHandler` records it. I treated this as the conservative reading of "edge-only". - The registry is keyed by object identity. A `Request` copied before it reaches `app.fetch` loses the value, the same property the #116 peer registry has (and tests). ## Docs follow-up (CARD-12) - Edge/deploy guide: `ctx.ip` on Cloudflare comes from `cf-connecting-ip` only with `createApp({ trustProxy: true })`, for HTTP and WebSocket. It takes precedence over XFF. Without `trustProxy` it is `''`. - Rate-limit plugin edge docs: mention `trustProxy` (otherwise all clients share one bucket). - Migration note: edge WebSocket apps that relied on the header now need `trustProxy: true`.
chrnx force-pushed feature/fprd-001c-edge-cf-connecting-ip from d0869788a5 to 7fd5d7492e 2026-10-23 04:55:37 +00:00 Compare
chrnx force-pushed feature/fprd-001c-edge-cf-connecting-ip from 7fd5d7492e to a4245458c3 2026-10-23 05:02:47 +00:00 Compare
Author
Owner

Owner decisions (2026-10-23): both edge cases accepted as implemented: WS with no cf-connecting-ip keeps ip === '', and a bare app.fetch ignores the header (documented). Not merging yet: implementation review first.

Owner decisions (2026-10-23): both edge cases accepted as implemented: WS with no `cf-connecting-ip` keeps `ip === ''`, and a bare `app.fetch` ignores the header (documented). Not merging yet: implementation review first.
chrnx added 1 commit 2026-10-23 05:48:03 +00:00
feat(edge)!: trust cf-connecting-ip only under trustProxy, for HTTP and WebSocket (CARD-23, #119)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 25s
CI / runtimes (deno) (pull_request) Successful in 19s
CI / runtimes (edge) (pull_request) Successful in 15s
88cbe41f72
edgeHandler copied cf-connecting-ip into ctx.ip on every WebSocket
upgrade, so any client that sent the header chose its own address, and
it never read the header for HTTP, so IP-keyed plugins saw '' there.
D21 settles both: the header counts only under trustProxy, on both
paths, with no new option.

The adapter cannot make that call, because trustProxy is an app option.
So edgeHandler records the header against the object it hands the app
(the Request for HTTP, the WsRequest for an upgrade) in an internal
registry next to the Deno/Bun peer one, and app.fetch and app.upgrade
both consult it through one helper, trustedProxyClient(), which returns
'' unless trustProxy is on. Under trustProxy it takes precedence over
x-forwarded-for, since Cloudflare sets it itself and XFF can carry
client-supplied hops.

Nothing else records into the registry, so the Node, Deno and Bun
adapters, and a bare app.fetch or app.upgrade, ignore the header as
before.

BREAKING CHANGE: an edge WebSocket upgrade no longer takes ctx.ip from
  cf-connecting-ip unless the app sets trustProxy: true; without it,
  ctx.ip is ''.
Refs: #119

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed feature/fprd-001c-edge-cf-connecting-ip from a4245458c3 to 88cbe41f72 2026-10-23 05:48:03 +00:00 Compare
chrnx merged commit a9e0555516 into develop 2026-10-23 05:49:47 +00:00
chrnx deleted branch feature/fprd-001c-edge-cf-connecting-ip 2026-10-23 05:49:48 +00:00
Sign in to join this conversation.