feat(edge)!: trust cf-connecting-ip only under trustProxy, for HTTP and WebSocket (CARD-23, #119)
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>
This commit is contained in:
8 files changed
+244
-10
No files matched your search
@@ -25,3 +25,28 @@ export function setPeer(request: Request, ip: string): void {
|
||||
export function peerOf(request: Request): string {
|
||||
return peers.get(request) ?? '';
|
||||
}
|
||||
|
||||
// The client address a runtime adapter read from a header its platform's proxy sets — today only the
|
||||
// edge adapter, from `cf-connecting-ip`. Unlike the socket peer above, a header is client-supplied
|
||||
// unless a proxy in front overwrites it, so it only counts under `trustProxy`; the adapter cannot
|
||||
// decide that, because the option lives on the app, so it records the value and the app decides.
|
||||
// Keyed by the `Request` (HTTP) or the `WsRequest` (WebSocket upgrade) the adapter hands the app, for
|
||||
// the same reasons as the peer: no handler-visible header, no extra public parameter. Same
|
||||
// `globalThis` registration, because `@green-tea/core/edge` is a separate bundle too.
|
||||
const PROXY_KEY = Symbol.for('@green-tea/core:proxy-client');
|
||||
const proxyRegistry = globalThis as { [PROXY_KEY]?: WeakMap<object, string> };
|
||||
const proxyClients = (proxyRegistry[PROXY_KEY] ??= new WeakMap<object, string>());
|
||||
|
||||
/** Records `ip` as the client address a proxy header named for `source`. An empty value is not recorded. */
|
||||
export function setProxyClient(source: object, ip: string): void {
|
||||
if (ip) proxyClients.set(source, ip);
|
||||
}
|
||||
|
||||
/**
|
||||
* The client address an adapter recorded for `source` from a proxy header, when `trustProxy` is on;
|
||||
* `''` otherwise. The one check both `app.fetch` and `app.upgrade` run, so HTTP and WebSocket cannot
|
||||
* disagree about whether the header counts.
|
||||
*/
|
||||
export function trustedProxyClient(source: object, trustProxy: boolean | undefined): string {
|
||||
return trustProxy ? (proxyClients.get(source) ?? '') : '';
|
||||
}
|
||||
+5
-2
@@ -11,7 +11,7 @@ import {
|
||||
type Preflight,
|
||||
} from './core';
|
||||
import { mergeInjectedHeaders } from './headers';
|
||||
import { peerOf } from './peer';
|
||||
import { peerOf, trustedProxyClient } from './peer';
|
||||
import { declaresOverLimit, type BodyFailure, type BodyReader, type MaybePromise } from './body';
|
||||
import { HttpError } from '../signals';
|
||||
import { renderError, type ErrorRequest } from '../transformers';
|
||||
@@ -299,7 +299,10 @@ export function buildFetch(routes: RouteDef[], opts: HttpOptions | undefined) {
|
||||
// proxy is trusted, and take its first hop — an untrusted XFF is client-spoofable.
|
||||
// Otherwise the socket peer, which only the Deno and Bun adapters can supply; a bare
|
||||
// `app.fetch` has none, so there the answer is still ''.
|
||||
ip: forwardedFor(headers, opts) || peerOf(request),
|
||||
// Ahead of both, under `trustProxy` only: the address the edge adapter read from
|
||||
// `cf-connecting-ip`. Cloudflare sets that header itself, while XFF can carry hops the
|
||||
// client supplied, so on the edge it wins (CARD-23).
|
||||
ip: trustedProxyClient(request, opts?.trustProxy) || forwardedFor(headers, opts) || peerOf(request),
|
||||
},
|
||||
// Already computed above for the shed-503 path; handing it down keeps `cors.origins` to
|
||||
// one call per request.
|
||||
|
||||
Reference in new issue
Block a user