diff --git a/doc/cloud-browser-analytics.md b/doc/cloud-browser-analytics.md index 27bb253688e..b52a6dc4805 100644 --- a/doc/cloud-browser-analytics.md +++ b/doc/cloud-browser-analytics.md @@ -64,6 +64,19 @@ browser analytics is on. It does not require a session: posthog-js sends some events while a page unloads, and the proxy can only reach the two PostHog hosts with the public project token, like PostHog's own ingest endpoint. +### Visitor address behind the platform proxy + +The per-client limit uses `req.ip`, which comes from the server's trusted proxy +settings (`TRUST_PROXY`). It never comes from a header the client controls. If +`TRUST_PROXY` is unset behind a platform proxy, `req.ip` is the proxy's private +hop, so every visitor behind that hop shares one 16-request limit. Set +`TRUST_PROXY` to the platform's proxy range so that `req.ip` is the visitor. + +The `X-Forwarded-For` value that the proxy sends to PostHog carries the +visitor's location. When the socket peer is private, the proxy uses the +rightmost public `X-Forwarded-For` entry, which the platform edge appended. +This value is used only for location, never for limits. + ## Identity - After sign-in: `identify(session.userId, { email, name })`. `session.userId` diff --git a/server/src/__tests__/cloud-posthog-browser.test.ts b/server/src/__tests__/cloud-posthog-browser.test.ts index 8cf1863a8ce..4a913a0b7c9 100644 --- a/server/src/__tests__/cloud-posthog-browser.test.ts +++ b/server/src/__tests__/cloud-posthog-browser.test.ts @@ -12,6 +12,7 @@ import { resolveCloudPostHogConfig } from "../services/cloud-posthog-config.js"; import { posthogProxyRoutes, posthogUpstreamUrl, + posthogVisitorIp, resolvePostHogProxyMount, } from "../routes/posthog-proxy.js"; @@ -171,6 +172,17 @@ describe("PostHog proxy forwarding", () => { expect(seen[0]!.headers["x-forwarded-for"]).toBeTruthy(); }); + it("forwards the visitor's IP from X-Forwarded-For when the peer is a private proxy hop", async () => { + const res = await request(proxyApp()) + .post("/_ph/e/") + .set("Content-Type", "application/json") + .set("X-Forwarded-For", "8.8.8.8, 100.64.0.11") + .send(JSON.stringify({ event: "$pageview" })); + + expect(res.status).toBe(200); + expect(seen[0]!.headers["x-forwarded-for"]).toBe("8.8.8.8"); + }); + it.each([ ["GET", "/_ph/api/projects/@current"], ["PUT", "/_ph/e/"], @@ -293,3 +305,27 @@ describe("PostHog proxy forwarding", () => { expect(errors).toEqual([]); }); }); + +describe("posthogVisitorIp", () => { + it("keeps a public peer address and ignores X-Forwarded-For", () => { + expect(posthogVisitorIp("9.9.9.9", { "x-forwarded-for": "8.8.8.8" })).toBe("9.9.9.9"); + }); + + it.each(["100.64.0.11", "10.1.2.3", "172.20.0.1", "192.168.1.5", "127.0.0.1", "::1", "::ffff:100.64.0.5", "::ffff:7f00:1", "fd00::1", "febf::1"])( + "uses the edge-appended X-Forwarded-For entry behind private hop %s", + (peer) => { + expect(posthogVisitorIp(peer, { "x-forwarded-for": "8.8.8.8, 100.64.0.2" })).toBe("8.8.8.8"); + }, + ); + + it("ignores a client-supplied leftmost entry and uses the address the edge appended", () => { + expect(posthogVisitorIp("100.64.0.11", { "x-forwarded-for": "1.2.3.4, 1.1.1.1" })).toBe("1.1.1.1"); + expect(posthogVisitorIp("100.64.0.11", { "x-forwarded-for": "1.2.3.4, 1.1.1.1, 100.64.0.3" })).toBe("1.1.1.1"); + }); + + it("falls back to the peer when X-Forwarded-For is missing or not an IP", () => { + expect(posthogVisitorIp("100.64.0.11", {})).toBe("100.64.0.11"); + expect(posthogVisitorIp("100.64.0.11", { "x-forwarded-for": "not-an-ip" })).toBe("100.64.0.11"); + expect(posthogVisitorIp("100.64.0.11", { "x-forwarded-for": "10.0.0.1, 100.64.0.3" })).toBe("100.64.0.11"); + }); +}); diff --git a/server/src/lib/ip-address.ts b/server/src/lib/ip-address.ts new file mode 100644 index 00000000000..e1328ed0d61 --- /dev/null +++ b/server/src/lib/ip-address.ts @@ -0,0 +1,72 @@ +import { isIP } from "node:net"; + +export function parseIpv4Address(address: string) { + const parts = address.split("."); + if (parts.length !== 4) return null; + const parsed = parts.map((part) => { + if (!/^\d+$/.test(part)) return NaN; + return Number(part); + }); + if (parsed.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) { + return null; + } + return parsed as [number, number, number, number]; +} + +export function isPrivateOrReservedIpv4(address: string) { + const octets = parseIpv4Address(address); + if (!octets) return true; + const [a, b, c] = octets; + if (a === 0) return true; + if (a === 10) return true; + if (a === 100 && b >= 64 && b <= 127) return true; + if (a === 127) return true; + if (a === 169 && b === 254) return true; + if (a === 172 && b >= 16 && b <= 31) return true; + if (a === 192 && b === 0 && c === 0) return true; + if (a === 192 && b === 168) return true; + if (a === 192 && b === 0 && c === 2) return true; + if (a === 192 && b === 88 && c === 99) return true; + if (a === 198 && (b === 18 || b === 19)) return true; + if (a === 198 && b === 51 && c === 100) return true; + if (a === 203 && b === 0 && c === 113) return true; + if (a >= 224) return true; + return false; +} + +function parseMappedIpv4Hex(address: string) { + const match = address.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); + if (!match) return null; + const hi = Number.parseInt(match[1]!, 16); + const lo = Number.parseInt(match[2]!, 16); + if (!Number.isInteger(hi) || !Number.isInteger(lo)) return null; + return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`; +} + +export function isPrivateOrReservedIpv6(address: string) { + const lower = address.toLowerCase(); + if (lower.startsWith("::ffff:")) { + const mappedIpv4 = lower.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/); + if (mappedIpv4?.[1]) return isPrivateOrReservedIpv4(mappedIpv4[1]); + const mappedIpv4Hex = parseMappedIpv4Hex(lower); + if (mappedIpv4Hex) return isPrivateOrReservedIpv4(mappedIpv4Hex); + return true; + } + if (lower === "::" || lower === "::1") return true; + if (lower.startsWith("fc") || lower.startsWith("fd")) return true; + if (/^fe[89ab]/.test(lower)) return true; + if (lower.startsWith("ff")) return true; + if (lower === "100::" || lower.startsWith("100:")) return true; + if (lower.startsWith("2001:db8:") || lower === "2001:db8::") return true; + if (lower.startsWith("2001:2:") || lower === "2001:2::") return true; + if (lower.startsWith("2002:")) return true; + if (lower.startsWith("64:ff9b:")) return true; + return false; +} + +export function isPublicIpAddress(address: string) { + const ipVersion = isIP(address); + if (ipVersion === 4) return !isPrivateOrReservedIpv4(address); + if (ipVersion === 6) return !isPrivateOrReservedIpv6(address); + return false; +} diff --git a/server/src/routes/access.ts b/server/src/routes/access.ts index 67071145726..cdba0bb983d 100644 --- a/server/src/routes/access.ts +++ b/server/src/routes/access.ts @@ -1,4 +1,5 @@ import { notifyRetentionWork, writeJoinRequestChange } from "../services/retention-work-notifications.js"; +import { isPublicIpAddress, parseIpv4Address } from "../lib/ip-address.js"; import { createAgentLifecycle } from "../services/agent-lifecycle.js"; import { createHash, @@ -2345,77 +2346,6 @@ type InviteResolutionNetwork = { ): Promise; }; -function parseIpv4Address(address: string) { - const parts = address.split("."); - if (parts.length !== 4) return null; - const parsed = parts.map((part) => { - if (!/^\d+$/.test(part)) return NaN; - return Number(part); - }); - if (parsed.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) { - return null; - } - return parsed as [number, number, number, number]; -} - -function isPrivateOrReservedIpv4(address: string) { - const octets = parseIpv4Address(address); - if (!octets) return true; - const [a, b, c] = octets; - if (a === 0) return true; - if (a === 10) return true; - if (a === 100 && b >= 64 && b <= 127) return true; - if (a === 127) return true; - if (a === 169 && b === 254) return true; - if (a === 172 && b >= 16 && b <= 31) return true; - if (a === 192 && b === 0 && c === 0) return true; - if (a === 192 && b === 168) return true; - if (a === 192 && b === 0 && c === 2) return true; - if (a === 192 && b === 88 && c === 99) return true; - if (a === 198 && (b === 18 || b === 19)) return true; - if (a === 198 && b === 51 && c === 100) return true; - if (a === 203 && b === 0 && c === 113) return true; - if (a >= 224) return true; - return false; -} - -function parseMappedIpv4Hex(address: string) { - const match = address.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); - if (!match) return null; - const hi = Number.parseInt(match[1]!, 16); - const lo = Number.parseInt(match[2]!, 16); - if (!Number.isInteger(hi) || !Number.isInteger(lo)) return null; - return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`; -} - -function isPrivateOrReservedIpv6(address: string) { - const lower = address.toLowerCase(); - if (lower.startsWith("::ffff:")) { - const mappedIpv4 = lower.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/); - if (mappedIpv4?.[1]) return isPrivateOrReservedIpv4(mappedIpv4[1]); - const mappedIpv4Hex = parseMappedIpv4Hex(lower); - if (mappedIpv4Hex) return isPrivateOrReservedIpv4(mappedIpv4Hex); - return true; - } - if (lower === "::" || lower === "::1") return true; - if (lower.startsWith("fc") || lower.startsWith("fd")) return true; - if (/^fe[89ab]/.test(lower)) return true; - if (lower.startsWith("ff")) return true; - if (lower === "100::" || lower.startsWith("100:")) return true; - if (lower.startsWith("2001:db8:") || lower === "2001:db8::") return true; - if (lower.startsWith("2001:2:") || lower === "2001:2::") return true; - if (lower.startsWith("2002:")) return true; - if (lower.startsWith("64:ff9b:")) return true; - return false; -} - -function isPublicIpAddress(address: string) { - const ipVersion = isIP(address); - if (ipVersion === 4) return !isPrivateOrReservedIpv4(address); - if (ipVersion === 6) return !isPrivateOrReservedIpv6(address); - return false; -} - function hostnameForResolution(url: URL) { return url.hostname.replace(/^\[|\]$/g, ""); } diff --git a/server/src/routes/posthog-proxy.ts b/server/src/routes/posthog-proxy.ts index f1964ffbf91..b89c7006f77 100644 --- a/server/src/routes/posthog-proxy.ts +++ b/server/src/routes/posthog-proxy.ts @@ -19,6 +19,7 @@ import https from "node:https"; import { pipeline, Transform } from "node:stream"; import type { IncomingHttpHeaders, OutgoingHttpHeaders } from "node:http"; import { Router, type Request, type Response } from "express"; +import { isPublicIpAddress } from "../lib/ip-address.js"; import { logger } from "../middleware/logger.js"; import type { CloudInstanceEnv } from "../services/cloud-instance.js"; import { @@ -104,6 +105,17 @@ export function posthogUpstreamUrl(ingestHost: string, method: string, requestUr return new URL(`${origin}${path}${query}`); } +export function posthogVisitorIp(peerIp: string | undefined, headers: IncomingHttpHeaders): string | undefined { + if (peerIp && isPublicIpAddress(peerIp)) return peerIp; + const raw = headers["x-forwarded-for"]; + const entries = (Array.isArray(raw) ? raw.join(",") : raw ?? "").split(",").map((entry) => entry.trim()); + for (let index = entries.length - 1; index >= 0; index -= 1) { + const entry = entries[index]!; + if (isPublicIpAddress(entry)) return entry; + } + return peerIp; +} + export function posthogForwardRequestHeaders( headers: IncomingHttpHeaders, clientIp: string | undefined, @@ -165,7 +177,7 @@ export function posthogProxyRoutes(ingestHost: string, limits: PostHogProxyLimit const transport = target.protocol === "https:" ? https : http; const upstreamReq = transport.request(target, { method: req.method, - headers: posthogForwardRequestHeaders(req.headers, req.ip, target.host), + headers: posthogForwardRequestHeaders(req.headers, posthogVisitorIp(req.ip, req.headers), target.host), timeout: UPSTREAM_TIMEOUT_MS, });