Files
HealthLog/src/lib/http/__tests__/peer-address.test.ts
T
Marc-André Bombeck d6d0f7e73f fix(http): read forwarding headers only from a connection a proxy could make
The client-IP resolver trusted one forwarding hop by default and read the
rightmost X-Forwarded-For entry. Next fills that header with the socket's
address only when the request carries none, so a caller reaching the
published port directly could send its own and choose a fresh address per
request: every per-IP limit (sign-in, second factor, passkey, share unlock,
webhooks) became per-request, and sessions, the audit log and sign-in
notices recorded the chosen address.

The server now writes each connection's own peer address into an internal
header from Node's http.server.request.start diagnostics channel, installed
at boot and overwriting anything a caller sent under that name. The resolver
reads forwarding headers only when that peer is loopback, private,
unique-local or CGNAT, where a reverse proxy sits; a public peer is its own
client address, and a log line says once that its forwarding headers were
ignored. With TRUST_PROXY_HOPS=0 the peer is used instead of no address, so
a directly exposed host no longer shares one bucket across all anonymous
callers. Proxies on the same host, Docker network or tailnet need no change.
2026-10-25 13:52:04 +01:00

69 lines
2.1 KiB
TypeScript

import { request as httpRequest, createServer } from "node:http";
import type { AddressInfo } from "node:net";
import { describe, expect, it, onTestFinished } from "vitest";
import {
__uninstallPeerAddressStampForTests,
installPeerAddressStamp,
PEER_ADDRESS_HEADER,
readPeerAddress,
} from "../peer-address";
/**
* The stamp is what the client-IP resolver trusts, so a caller must not be
* able to set it: the server writes the socket's address over whatever the
* request carried under that name.
*/
describe("installPeerAddressStamp", () => {
it("writes the socket's address over a header the caller sent", async () => {
installPeerAddressStamp();
installPeerAddressStamp(); // once per process, however often it is called
onTestFinished(__uninstallPeerAddressStampForTests);
const seen: Array<string | string[] | undefined> = [];
const server = createServer((req, res) => {
seen.push(req.headers[PEER_ADDRESS_HEADER]);
res.end("ok");
});
await new Promise<void>((resolve) =>
server.listen(0, "127.0.0.1", resolve),
);
const { port } = server.address() as AddressInfo;
try {
await new Promise<void>((resolve, reject) => {
const req = httpRequest(
{
host: "127.0.0.1",
port,
path: "/",
headers: { [PEER_ADDRESS_HEADER]: "203.0.113.9" },
},
(res) => {
res.resume();
res.on("end", resolve);
},
);
req.on("error", reject);
req.end();
});
} finally {
await new Promise((resolve) => server.close(resolve));
}
expect(seen).toEqual(["127.0.0.1"]);
});
it("reads the stamp back, normalised", () => {
const read = (value?: string) =>
readPeerAddress(
new Request("http://x/", {
headers: value === undefined ? {} : { [PEER_ADDRESS_HEADER]: value },
}),
);
expect(read()).toBeUndefined();
expect(read("::ffff:10.0.0.1")).toBe("10.0.0.1");
expect(read("2001:db8::1")).toBe("2001:db8::1");
expect(read("not-an-ip")).toBeNull();
});
});