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
+17
-3
@@ -7,6 +7,7 @@ import { toMermaid, toDOT, graphHtml, type GraphView } from '../graph-viz';
|
||||
import { runPipeline, runSteps, PipelineStep } from '../pipeline';
|
||||
import { createHttpServer, parseQuery, RouteDef, WsRouteDef, RequestLimits } from '../http';
|
||||
import type { HttpOptions } from '../http';
|
||||
import { trustedProxyClient } from '../http/peer';
|
||||
import { buildFetch } from '../http/web';
|
||||
import {
|
||||
boundInbound,
|
||||
@@ -331,7 +332,10 @@ export function createApp(opts: {
|
||||
devRoutes,
|
||||
() => remoteRoutes,
|
||||
);
|
||||
const upgradeFn = buildAppUpgrade(routePlans, pipelineDeps, streams, bootApp, () => meshControl, opts.limits);
|
||||
const upgradeFn = buildAppUpgrade(routePlans, pipelineDeps, streams, bootApp, () => meshControl, {
|
||||
limits: opts.limits,
|
||||
trustProxy: opts.trustProxy,
|
||||
});
|
||||
|
||||
const listen = async (port: number): Promise<http.Server> => {
|
||||
const deps = pipelineDeps;
|
||||
@@ -1395,7 +1399,7 @@ function buildAppUpgrade(
|
||||
streams: Set<() => void>,
|
||||
ensureBooted: () => Promise<void>,
|
||||
getMeshControl: () => MeshControl | undefined,
|
||||
limits?: RequestLimits,
|
||||
{ limits, trustProxy }: { limits?: RequestLimits; trustProxy?: boolean },
|
||||
): (request: WsRequest, socket: WsSocket) => Promise<void> {
|
||||
let wsRoutes: WsRouteDef[] | undefined;
|
||||
|
||||
@@ -1444,10 +1448,20 @@ function buildAppUpgrade(
|
||||
return;
|
||||
}
|
||||
|
||||
await runWsConnection(ready, request, route, deps.bus, streams);
|
||||
await runWsConnection(ready, withProxyClient(request, trustProxy), route, deps.bus, streams);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* `request` with `ip` taken from the proxy header its adapter recorded, when `trustProxy` allows it
|
||||
* (the edge adapter's `cf-connecting-ip`, CARD-23); otherwise `request` unchanged. The same check
|
||||
* `app.fetch` runs, so an edge WebSocket upgrade and an edge HTTP request resolve `ctx.ip` alike.
|
||||
*/
|
||||
function withProxyClient(request: WsRequest, trustProxy: boolean | undefined): WsRequest {
|
||||
const ip = trustedProxyClient(request, trustProxy);
|
||||
return ip ? { ...request, ip } : request;
|
||||
}
|
||||
|
||||
/** The dev-only `/__graph__` route: serves the dependency graph as Mermaid text or an HTML viewer. */
|
||||
function devGraphRoute(graph: () => GraphView): RouteDef {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user