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