From 6819ce70c63914e0f3f123b87d247fef883e20c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 26 Sep 2026 16:17:16 +0200 Subject: [PATCH] fix(ops): read an empty TRUST_PROXY_HOPS as unset The shipped compose file passes TRUST_PROXY_HOPS as an empty string when the operator never set it, and the parser rejected the empty string as an invalid value, throwing wherever the client address was read. Empty now means the default of one trusted hop. --- src/lib/__tests__/get-client-ip.test.ts | 11 +++++++++++ src/lib/api-response.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib/__tests__/get-client-ip.test.ts b/src/lib/__tests__/get-client-ip.test.ts index 2f94ab68c..9e5a92d0e 100644 --- a/src/lib/__tests__/get-client-ip.test.ts +++ b/src/lib/__tests__/get-client-ip.test.ts @@ -61,6 +61,17 @@ describe("getClientIp trusted-proxy semantics (V3 audit)", () => { ).toThrow(/TRUST_PROXY_HOPS/); }); + it.each(["", " "])( + "treats an empty TRUST_PROXY_HOPS (%j) as unset — the default compose file passes one", + (value) => { + process.env.TRUST_PROXY_HOPS = value; + const ip = getClientIp( + makeRequest({ "x-forwarded-for": "9.9.9.9, 1.2.3.4, 5.6.7.8" }), + ); + expect(ip).toBe("5.6.7.8"); + }, + ); + it("with TRUST_PROXY_HOPS=0 ignores XFF entirely", () => { process.env.TRUST_PROXY_HOPS = "0"; const ip = getClientIp( diff --git a/src/lib/api-response.ts b/src/lib/api-response.ts index 46dea79db..860685e02 100644 --- a/src/lib/api-response.ts +++ b/src/lib/api-response.ts @@ -400,8 +400,12 @@ function readCfConnectingIp(request: Request): string | null { } function parseTrustProxyHops(raw: string | undefined): number { - if (raw === undefined) return 1; - const trimmed = raw.trim(); + // Empty is unset. The shipped docker-compose.yml passes + // `TRUST_PROXY_HOPS: "${TRUST_PROXY_HOPS:-}"`, which is an empty string for + // every operator who never set it; reading that as invalid threw on every + // request that asked for the client IP. + const trimmed = raw?.trim() ?? ""; + if (trimmed === "") return 1; if (!/^\d+$/.test(trimmed)) { // Reject explicitly-invalid values so an operator typo doesn't silently // switch a real-proxy deployment to "no XFF trust" mode (review