mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
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.
This commit is contained in:
1 parent
8da7583ec6
commit
6819ce70c6
2 files changed
+17
-2
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user