mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
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.
This commit is contained in:
1 parent
ba22932101
commit
d6d0f7e73f
8 files changed
+309
-20
No files matched your search
+62
-1
@@ -3,6 +3,11 @@ import { NextResponse } from "next/server";
|
||||
import type { ZodError, ZodIssue } from "zod/v4";
|
||||
|
||||
import { defaultErrorCodeForStatus } from "@/lib/api/default-error-codes";
|
||||
import {
|
||||
peerAddressStampInstalled,
|
||||
readPeerAddress,
|
||||
} from "@/lib/http/peer-address";
|
||||
import { isPublicIp } from "@/lib/validations/notifications";
|
||||
|
||||
export function apiSuccess<T>(data: T, status = 200) {
|
||||
return NextResponse.json({ data, error: null }, { status });
|
||||
@@ -485,13 +490,22 @@ export async function safeJson<T = unknown>(
|
||||
* Trust model (`TRUST_PROXY_HOPS` env):
|
||||
* - "0" → trust no forwarding header at all, XFF or x-real-ip
|
||||
* (use this if HealthLog is internet-facing without a
|
||||
* reverse proxy you control); the IP resolves to null
|
||||
* reverse proxy you control); the IP is the connection's
|
||||
* own peer address
|
||||
* - "1" (default)→ trust exactly one hop (typical Coolify / Caddy /
|
||||
* Cloudflare-Tunnel single-proxy deployment); read the
|
||||
* rightmost XFF entry which is the IP your proxy
|
||||
* observed when the request arrived.
|
||||
* - "N" (>1) → trust N hops; read the Nth-from-rightmost XFF entry.
|
||||
*
|
||||
* Forwarding headers are read only from a connection whose peer could be a
|
||||
* proxy: loopback, a private or unique-local network, CGNAT (Tailscale). A
|
||||
* connection from a public address reached the server directly, so whatever
|
||||
* forwarding headers it sends are its own, and its own address is the client
|
||||
* IP. The peer comes from `@/lib/http/peer-address`. Without that check, the
|
||||
* default of one trusted hop let any direct caller name a fresh address per
|
||||
* request and step around every per-IP limit.
|
||||
*
|
||||
* Cloudflare opt-in (`TRUST_CF_CONNECTING_IP=1`): when the env flag is
|
||||
* set, the helper prefers the `cf-connecting-ip` header before walking
|
||||
* the XFF chain. Cloudflare strips and re-sets this header on every
|
||||
@@ -610,6 +624,29 @@ function warnUnreadProxyHeadersOnce(hops: number, header: string): void {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Once per process: a connection from a public address carried forwarding
|
||||
* headers, which are ignored. Either HealthLog is reachable without its
|
||||
* proxy (the published port is open to the internet), or the proxy in front
|
||||
* connects from a public address and needs `TRUST_CF_CONNECTING_IP` or a
|
||||
* private network between it and HealthLog.
|
||||
*/
|
||||
let publicPeerForwardingWarned = false;
|
||||
|
||||
/** Test-only reset for {@link warnPublicPeerForwardingOnce}. */
|
||||
export function _resetPublicPeerForwardingWarningForTests(): void {
|
||||
publicPeerForwardingWarned = false;
|
||||
}
|
||||
|
||||
function warnPublicPeerForwardingOnce(): void {
|
||||
if (publicPeerForwardingWarned) return;
|
||||
publicPeerForwardingWarned = true;
|
||||
console.warn(
|
||||
"[getClientIp] a connection from a public address sent X-Forwarded-For or X-Real-IP; those headers are ignored and the connection's own address is used. " +
|
||||
"If HealthLog is meant to be reached only through a reverse proxy, the published port is open beyond it.",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tagged return shape so a caller can apply a tighter universal
|
||||
* rate-limit when the trust chain is misconfigured. F-6 (mobile security
|
||||
@@ -640,6 +677,30 @@ export function getClientIpOrTrustWarning(request: Request): {
|
||||
|
||||
const hops = parseTrustProxyHops(process.env.TRUST_PROXY_HOPS);
|
||||
|
||||
// The connection's own peer, stamped by the server for every request it
|
||||
// receives. `undefined` only for a request the server did not receive (a
|
||||
// suite's hand-built request) — or, once the stamp is installed, one that
|
||||
// arrived before it was, whose forwarding headers nothing vouches for.
|
||||
const peer = readPeerAddress(request);
|
||||
if (peer === null || (peer === undefined && peerAddressStampInstalled())) {
|
||||
return { ip: null, trustViolation: false };
|
||||
}
|
||||
if (peer !== undefined) {
|
||||
// No proxy declared: the peer is the client.
|
||||
if (hops === 0) return { ip: peer, trustViolation: false };
|
||||
// A peer on a public address reached the server directly; its
|
||||
// forwarding headers are its own choice, not a proxy's report.
|
||||
if (isPublicIp(peer)) {
|
||||
if (
|
||||
request.headers.has("x-real-ip") ||
|
||||
request.headers.get("x-forwarded-for")?.trim() !== peer
|
||||
) {
|
||||
warnPublicPeerForwardingOnce();
|
||||
}
|
||||
return { ip: peer, trustViolation: false };
|
||||
}
|
||||
}
|
||||
|
||||
if (hops > 0) {
|
||||
const forwarded = request.headers.get("x-forwarded-for");
|
||||
if (forwarded) {
|
||||
|
||||
Reference in new issue
Block a user