Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(auth): read the client IP past every trusted proxy (TRUSTED_PROXIES)
Sidwebworks6503113fca4a
diff --git a/packages/env/src/schema.ts b/packages/env/src/schema.tsindex a78f009aab..6994a33d96 100644--- a/packages/env/src/schema.ts+++ b/packages/env/src/schema.ts@@ -253,6 +253,12 @@ export const schema = define({description: "Sign-in attempts allowed per window (5)",type: number},+ TRUSTED_PROXIES: {+ group: "auth",+ description:+ "Proxies in front of the app that append to X-Forwarded-For (1); 2 behind an ALB and Traefik",+ type: number+ },BOT_PROTECTION: {group: "auth",description: "Login bot check: botid or turnstile",diff --git a/packages/utils/src/headers.test.ts b/packages/utils/src/headers.test.tsindex cec93317b9..734435dc0a 100644--- a/packages/utils/src/headers.test.ts+++ b/packages/utils/src/headers.test.ts@@ -30,6 +30,31 @@ describe("getClientIp", () => {expect(getClientIp(req({}))).toBeNull();expect(getClientIp(req({ "x-forwarded-for": " , " }))).toBeNull();});++ it("skips our own proxies when there are several (TRUSTED_PROXIES)", () => {+ const before = process.env.TRUSTED_PROXIES;+ try {+ process.env.TRUSTED_PROXIES = "2";+ // Behind an ALB and then Traefik: a forged entry, the client the ALB+ // saw, then the ALB itself.+ expect(+ getClientIp(+ req({ "x-forwarded-for": "6.6.6.6, 203.0.113.9, 10.0.50.136" })+ )+ ).toBe("203.0.113.9");+ // Fewer entries than proxies: the first is the client.+ expect(getClientIp(req({ "x-forwarded-for": "203.0.113.9" }))).toBe(+ "203.0.113.9"+ );+ process.env.TRUSTED_PROXIES = "nonsense";+ expect(+ getClientIp(req({ "x-forwarded-for": "203.0.113.9, 10.0.50.136" }))+ ).toBe("10.0.50.136");+ } finally {+ if (before === undefined) delete process.env.TRUSTED_PROXIES;+ else process.env.TRUSTED_PROXIES = before;+ }+ });});describe("getRequestProtocol", () => {diff --git a/packages/utils/src/headers.ts b/packages/utils/src/headers.tsindex d232e04746..b3ad049fb0 100644--- a/packages/utils/src/headers.ts+++ b/packages/utils/src/headers.ts@@ -29,14 +29,30 @@ const listOf = (value: string | null) =>/*** The client's IP address, or null when no proxy reported one.*- * `X-Forwarded-For` is a chain each proxy APPENDS to, so only its LAST entry —- * the address the proxy in front of the app saw — cannot be forged: the ALB- * appends to whatever the caller sent, and a first-entry read there hands the- * caller any rate-limit bucket or audit-log address it likes. Vercel and Caddy- * write a single entry, which is the same address.+ * `X-Forwarded-For` is a chain each proxy APPENDS to, so only the entries our+ * own proxies wrote — the last ones — cannot be forged: the ALB appends to+ * whatever the caller sent, and a first-entry read there hands the caller any+ * rate-limit bucket or audit-log address it likes. With one proxy in front of+ * the app (Vercel, the ALB, Caddy) the client is the last entry.+ *+ * `TRUSTED_PROXIES` counts the proxies when there are more. Behind an ALB and+ * then Traefik the chain arrives as `client, alb`, and the last entry is the+ * ALB's own address — every user one rate-limit bucket. With 2 the client is+ * the second from the end. A chain shorter than that came from fewer proxies+ * than promised, and its first entry is the client.*/export function getClientIp(input: RequestLike): string | null {- return listOf(headersOf(input).get("x-forwarded-for")).at(-1) ?? null;+ const chain = listOf(headersOf(input).get("x-forwarded-for"));+ return chain.at(-trustedProxies()) ?? chain[0] ?? null;+}++// Read here rather than through @carbon/env, which depends on this package.+// Declared in packages/env/src/schema.ts like every other variable.+function trustedProxies(): number {+ const raw =+ typeof process === "undefined" ? undefined : process.env.TRUSTED_PROXIES;+ const n = Number.parseInt(raw ?? "", 10);+ return Number.isInteger(n) && n >= 1 ? n : 1;}/** `https` or `http` as the client used it: the proxy's, else `request.url`'s. */