mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(http): trust X-Real-IP only behind a declared proxy
The client-IP resolver read X-Real-IP on every path: with TRUST_PROXY_HOPS=0, which means no proxy in front and no forwarding header to believe, and on the path where the X-Forwarded-For chain was shorter than the declared hops, which means the proxies were bypassed. A caller sending a new X-Real-IP with each request got a new per-IP rate-limit bucket each time. X-Real-IP now gets the trust X-Forwarded-For gets: it is read only when at least one proxy hop is declared and the request carries no X-Forwarded-For. A short chain resolves to no address. The reverse-proxy guide no longer suggests TRUST_PROXY_HOPS=0 for a proxy that sets X-Real-IP; such a proxy takes 1.
This commit is contained in:
1 parent
92c5b0a60d
commit
bc69dec8e6
4 files changed
+50
-26
No files matched your search
@@ -72,7 +72,7 @@ describe("getClientIp trusted-proxy semantics (V3 audit)", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("with TRUST_PROXY_HOPS=0 ignores XFF entirely", () => {
|
||||
it("with TRUST_PROXY_HOPS=0 trusts no forwarding header, x-real-ip included", () => {
|
||||
process.env.TRUST_PROXY_HOPS = "0";
|
||||
const ip = getClientIp(
|
||||
makeRequest({
|
||||
@@ -80,7 +80,17 @@ describe("getClientIp trusted-proxy semantics (V3 audit)", () => {
|
||||
"x-real-ip": "8.8.8.8",
|
||||
}),
|
||||
);
|
||||
expect(ip).toBe("8.8.8.8");
|
||||
expect(ip).toBeNull();
|
||||
});
|
||||
|
||||
it("x-real-ip rotation — with no declared proxy a caller cannot mint a fresh bucket per request", () => {
|
||||
process.env.TRUST_PROXY_HOPS = "0";
|
||||
const seen = new Set(
|
||||
["1.1.1.1", "2.2.2.2", "3.3.3.3"].map((ip) =>
|
||||
getClientIp(makeRequest({ "x-real-ip": ip })),
|
||||
),
|
||||
);
|
||||
expect(seen).toEqual(new Set([null]));
|
||||
});
|
||||
|
||||
it("with TRUST_PROXY_HOPS=0 and no x-real-ip returns null", () => {
|
||||
@@ -277,7 +287,8 @@ describe("getClientIpOrTrustWarning shape (F-6, 2026-05-16)", () => {
|
||||
expect(result.ip).toBeNull();
|
||||
});
|
||||
|
||||
it("returns trustViolation=true and x-real-ip when chain is short but x-real-ip is set", () => {
|
||||
it("returns trustViolation=true and ignores x-real-ip when the chain is short", () => {
|
||||
// The declared proxies were bypassed, so x-real-ip came from the caller.
|
||||
process.env.TRUST_PROXY_HOPS = "2";
|
||||
const result = getClientIpOrTrustWarning(
|
||||
makeRequest({
|
||||
@@ -285,7 +296,7 @@ describe("getClientIpOrTrustWarning shape (F-6, 2026-05-16)", () => {
|
||||
"x-real-ip": "9.9.9.9",
|
||||
}),
|
||||
);
|
||||
expect(result).toEqual({ ip: "9.9.9.9", trustViolation: true });
|
||||
expect(result).toEqual({ ip: null, trustViolation: true });
|
||||
});
|
||||
|
||||
it("returns trustViolation=false when XFF is absent (no chain to validate)", () => {
|
||||
|
||||
@@ -176,8 +176,9 @@ describe("checkAuthSurfaceRateLimit — trust-violation tightening", () => {
|
||||
5,
|
||||
15 * 60 * 1000,
|
||||
);
|
||||
// Falls through to x-real-ip when the chain is too short.
|
||||
expect(result.ip).toBe("9.9.9.9");
|
||||
// A short chain means the declared proxies were bypassed, so the
|
||||
// caller-set x-real-ip is not believed either.
|
||||
expect(result.ip).toBeNull();
|
||||
expect(lastKey()).toBe("auth:anon:trust-violation");
|
||||
} finally {
|
||||
warn.mockRestore();
|
||||
|
||||
Reference in new issue
Block a user