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
+16
-1
@@ -1,5 +1,6 @@
|
||||
import type { App } from './app/types';
|
||||
import { eventSocket, toWsRequest, upgradeSafely, type EventfulSocket } from './http/ws-adapter';
|
||||
import { setProxyClient } from './http/peer';
|
||||
|
||||
// Minimal ambient for the Cloudflare Workers globals we use — avoids @cloudflare/workers-types.
|
||||
// Declares only what we call; mirrors the real workerd signatures.
|
||||
@@ -30,7 +31,9 @@ export function edgeHandler(
|
||||
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
||||
const [client, server] = Object.values(new WebSocketPair()) as [EdgeWebSocket, EdgeWebSocket];
|
||||
server.accept();
|
||||
const wsReq = toWsRequest(request, request.headers.get('cf-connecting-ip') ?? '');
|
||||
// No peer address on the edge, so `ip` starts empty; the header only fills it under `trustProxy`.
|
||||
const wsReq = toWsRequest(request, '');
|
||||
recordClient(wsReq, request);
|
||||
const lifecycle = upgradeSafely(app, wsReq, eventSocket(server));
|
||||
if (ctx?.waitUntil) ctx.waitUntil(lifecycle);
|
||||
else void lifecycle;
|
||||
@@ -38,6 +41,18 @@ export function edgeHandler(
|
||||
return new Response(null, { status: 101, webSocket: client } as ResponseInit & { webSocket: EdgeWebSocket });
|
||||
}
|
||||
|
||||
recordClient(request, request);
|
||||
return app.fetch(request);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hands the app the `cf-connecting-ip` header for `source`, the object the app will be given for this
|
||||
* request. The app — not this adapter — decides whether it counts, since `trustProxy` is an app
|
||||
* option; HTTP and WebSocket both go through here and through the same check on the app side.
|
||||
* Without `trustProxy` the header is ignored and `ctx.ip` stays `''`: workerd exposes no peer address,
|
||||
* and an untrusted header is whatever the client chose to send.
|
||||
*/
|
||||
function recordClient(source: object, request: Request): void {
|
||||
setProxyClient(source, request.headers.get('cf-connecting-ip') ?? '');
|
||||
}
|
||||
Reference in new issue
Block a user