Files
PaperClipAI/server/src/__tests__/cloud-posthog-browser.test.ts
T
DottaandClaude Opus 5.5 3c460f20cc feat(analytics): ask EU, UK and Swiss visitors before Cloud browser analytics (#22186)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Cloud can send browser product analytics and session
replays to PostHog. Self-hosted installs never do this.
> - Today, browser analytics starts for every Cloud visitor after the
page settles.
> - Visitors in the EU, the EEA, the UK and Switzerland must agree
before analytics cookies and session recordings start.
> - This pull request adds a consent step for these visitors and a
switch in the user's profile settings.
> - The benefit is that Cloud analytics follows the owner decision: ask
in Europe, keep the current behavior elsewhere, and let every user
change the choice.

## Linked Issues or Issue Description

**Subsystem affected**
Paperclip Cloud browser product analytics (`ui/src/lib/analytics`,
`ProductAnalyticsGate`, the session route, and Profile settings).

**Problem or motivation**
Browser analytics and session replay start for every Cloud visitor
without a choice. EU, EEA, UK and Swiss visitors must consent first. The
privacy policy draft also says that users can change the choice in
settings. No such setting exists.

**Proposed solution**
Ask visitors in the EU, the EEA, the UK and Switzerland with a small
bottom banner (Accept or Decline). Do not load posthog-js before they
accept. Treat Do Not Track and Global Privacy Control as a decline for
these visitors. Keep the choice in `localStorage`. Add a "Product
analytics" switch to Profile settings for every user when browser
analytics is on.

**Alternatives considered**
A server-side country from a trusted edge header. Paperclip Cloud has no
such header today: Cloudflare records are DNS-only, so no `CF-IPCountry`
arrives, and the Railway edge adds no country header. This PR supports
an optional header (`PAPERCLIP_POSTHOG_COUNTRY_HEADER`) for a future
edge, and uses the browser time zone until then.

## What Changed

- `packages/shared`: `browserAnalytics` has a new optional
`consentRequired: boolean | null`.
- `packages/shared/src/analytics-consent.ts` (new): the one region
policy. `countryRequiresAnalyticsConsent` (EU and EEA with outermost
regions and Svalbard, GB with the Crown Dependencies and Gibraltar, CH)
and `timeZoneRequiresAnalyticsConsent` (`Europe/*`, EU, EEA and UK zones
outside Europe, and unknown zones such as `UTC` or `Etc/*`). The server
and the UI both use it.
- `server`: `resolveBrowserAnalyticsConfig` sets `consentRequired` from
the header that `PAPERCLIP_POSTHOG_COUNTRY_HEADER` names. No header or
an unknown code (`XX`) gives `null`.
- `ui/src/lib/analytics/consent.ts` (new): decides `granted`, `denied`
or `ask`. Order: for a visitor who must consent, Do Not Track or Global
Privacy Control gives `denied` (also over a stored Accept); then the
choice made on this page; then the stored choice; then the region
(server value, else browser time zone). The choice is in `localStorage`
key `paperclip.analyticsConsent`. All storage access is in `try/catch`.
A cross-tab `storage` event updates the page.
- `ProductAnalyticsGate`: one effect calls `syncProductAnalytics(config,
consent)`. Shows `AnalyticsConsentBanner` when consent is `ask`. Accept
loads posthog-js at once, without a reload.
- `product-analytics.ts`: new `syncProductAnalytics`. It starts, opts
in, opts out (`opt_out_capturing()` and `stopSessionRecording()`) or
cancels a pending load. Any consent other than `granted` stops
analytics. If consent is withdrawn while the script downloads, `init()`
does not run until a later opt-in.
- `AnalyticsConsentBanner` (new): a labelled bottom region with plain
copy, a privacy policy link, and Decline and Accept buttons (shared
`Button`). The copy says that recordings hide what you type and your
messages, documents and connected accounts, but may show page names like
task titles. It does not block the page. On mobile it sits above the
bottom navigation.
- `ProductAnalyticsSetting` (new) in Profile settings: a "Product
analytics" switch (shared `ToggleSwitch`). It shows only when browser
analytics is on for the instance. When GPC or DNT blocks analytics, it
is off and disabled and says why.
- `doc/cloud-browser-analytics.md`: new Consent section (signal,
behavior table, storage key, how to reset) and the new variable.
- `tests/runner-e2e/source-sdk-public-inputs.json`: refreshed for the
changed shared validator.

## Verification

- Red/green: the 7 new `ProductAnalyticsGate` consent tests fail against
the master gate (7 failed, 3 passed). The 5 review-fix tests (failed
write with working read, cross-tab change, decline during script
download, granted -> ask from another tab, granted -> ask from a session
refresh) fail on the first commit (5 failed, 16 passed). All pass now.
- `cd ui && npx vitest run src/lib/analytics
src/components/ProductAnalyticsGate.test.tsx
src/components/AnalyticsConsentBanner.test.tsx
src/components/ProductAnalyticsSetting.test.tsx
src/pages/ProfileSettings.test.tsx`: 267 tests pass.
- `cd server && npx vitest run
src/__tests__/cloud-posthog-browser.test.ts
src/__tests__/auth-routes.test.ts
src/__tests__/auth-session-route.test.ts`: 87 tests pass.
`packages/shared` `analytics-consent.test.ts` passes. `tsc --noEmit` for
`ui`, `server` and `packages/shared` shows no new errors.
- `pnpm verify` exits 2 (a doc is a global CI input). Required CI gives
the broad coverage.
- Real check in headless Chromium. I ran a local Cloud-managed instance
(external local PostgreSQL, `PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN`,
`PAPERCLIP_POSTHOG_BROWSER_ENABLED=1`). `PAPERCLIP_POSTHOG_HOST` pointed
at a local capture server, not PostHog. The capture server served a
small stand-in for `array.js` that sets a `ph_*` cookie and
`localStorage` key and posts `/e/` events. I set the visitor region with
the browser time zone:

| Visitor | Result |
| --- | --- |
| `Europe/Berlin`, no choice, 18 s wait | Banner shows. 0 `/_ph`
requests. No `ph_*` cookie or storage key. |
| Then Accept (keyboard Enter) | Banner closes. `GET
/_ph/static/array.js` and `POST /_ph/e/` in 3 s. Key is `granted`. After
reload: no banner, events continue. |
| `Europe/London`, Decline, wait, reload, wait | No banner. 0 `/_ph`
requests. No `ph_*` cookie or storage key. Key is `denied`. |
| `Europe/Zurich` with Global Privacy Control | No banner. 0 `/_ph`
requests. No `ph_*` cookie or storage key. |
| `America/New_York` | No banner. posthog-js loads after the page
settles, as before. |
| Profile settings switch (US) | Shows on. Off: client is opted out at
once. On: opt-in, events continue. |

- I ran the browser check again after the review fixes, with the same
results.
- Screenshots (banner light, banner dark, mobile 375 px light and dark,
settings switch): see the PR comment.
- Cold load (local dev server, fresh browser context per run, median of
7, ms):

| Build | Time zone | FCP | DOMContentLoaded | load |
| --- | --- | --- | --- | --- |
| master | New York | 728 / 724 | 687 / 684 | 689 / 686 |
| this PR | New York | 748 / 724 | 708 / 684 | 710 / 686 |
| master | Berlin | 740 | 694 | 697 |
| this PR | Berlin | 756 / 724 | 716 / 684 | 718 / 686 |

The second round gives the same medians for master and this PR. The
first-round difference is noise. The consent code adds no request and no
load-time wait.

## Risks

- Region by time zone is approximate. It asks some visitors outside the
EU (for example `Europe/Istanbul`, or `UTC` browsers). This is the safe
direction.
- A forged country header (only when `PAPERCLIP_POSTHOG_COUNTRY_HEADER`
is set) changes only the default for that visitor's own browser.
- The choice is per browser. A user on a new browser sees the banner
again (in consent regions).
- Visitors in consent regions now send no browser analytics until they
accept. Expect fewer EU/UK sessions in PostHog.
- Self-hosted installs: no change. They get no `browserAnalytics`
config, so no banner, no switch and no load.

## Model Used

- Claude Opus 5.5 (claude-opus-5-5), Anthropic, used through Claude Code
with tool use (shell, file edits, headless browser).

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:52:20 -06:00

347 lines
17 KiB
TypeScript

import http from "node:http";
import type { AddressInfo } from "node:net";
import express from "express";
import request from "supertest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
posthogAssetsOrigin,
posthogUiHost,
resolveBrowserAnalyticsConfig,
} from "../services/cloud-posthog-browser.js";
import { resolveCloudPostHogConfig } from "../services/cloud-posthog-config.js";
import {
posthogProxyRoutes,
posthogUpstreamUrl,
posthogVisitorIp,
resolvePostHogProxyMount,
} from "../routes/posthog-proxy.js";
const CLOUD = { PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN: "tenant-token", PAPERCLIP_CLOUD_STACK_ID: "stack-1" };
const TOKEN = { PAPERCLIP_POSTHOG_PROJECT_TOKEN: "phc_test" };
const BROWSER = { PAPERCLIP_POSTHOG_BROWSER_ENABLED: "1" };
describe("browser analytics config exposure", () => {
const cases: Array<[string, Record<string, string>, boolean, boolean | null]> = [
["self-hosted, even with every PostHog switch set", { ...TOKEN, ...BROWSER, PAPERCLIP_CLOUD_STACK_ID: "stack-1" }, false, null],
["Cloud without a token", { ...CLOUD, ...BROWSER }, false, null],
["Cloud without a stack ID", { PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN: "t", ...TOKEN, ...BROWSER }, false, null],
["Cloud with a token but no browser switch", { ...CLOUD, ...TOKEN }, false, null],
["Cloud with the browser switch set to something other than 1", { ...CLOUD, ...TOKEN, PAPERCLIP_POSTHOG_BROWSER_ENABLED: "true" }, false, null],
["Cloud with browser analytics on", { ...CLOUD, ...TOKEN, ...BROWSER }, true, true],
["Cloud with browser analytics on and replay off", { ...CLOUD, ...TOKEN, ...BROWSER, PAPERCLIP_POSTHOG_REPLAY_ENABLED: "0" }, true, false],
];
it.each(cases)("%s", (_name, env, exposed, replayEnabled) => {
const config = resolveBrowserAnalyticsConfig(env);
if (!exposed) {
expect(config).toBeNull();
return;
}
expect(config).toEqual({
token: "phc_test",
apiHost: "/_ph",
uiHost: "https://us.posthog.com",
environment: "production",
stackId: "stack-1",
replayEnabled,
consentRequired: null,
});
});
it("decides consent from the configured edge country header, and leaves it to the browser otherwise", () => {
const on = { ...CLOUD, ...TOKEN, ...BROWSER };
const withHeader = { ...on, PAPERCLIP_POSTHOG_COUNTRY_HEADER: "CF-IPCountry" };
expect(resolveBrowserAnalyticsConfig(on, { "cf-ipcountry": "DE" })?.consentRequired).toBeNull();
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "DE" })?.consentRequired).toBe(true);
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "gb" })?.consentRequired).toBe(true);
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "CH" })?.consentRequired).toBe(true);
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "NO" })?.consentRequired).toBe(true);
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "US" })?.consentRequired).toBe(false);
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": "XX" })?.consentRequired).toBeNull();
expect(resolveBrowserAnalyticsConfig(withHeader, {})?.consentRequired).toBeNull();
expect(resolveBrowserAnalyticsConfig(withHeader, { "cf-ipcountry": ["FR", "US"] })?.consentRequired).toBe(true);
});
it("always sends an environment: the explicit variable, else staging or production from the host", () => {
const on = { ...CLOUD, ...TOKEN, ...BROWSER };
expect(resolveBrowserAnalyticsConfig({ ...on, PAPERCLIP_POSTHOG_ENVIRONMENT: "qa" })?.environment).toBe("qa");
expect(resolveBrowserAnalyticsConfig({ ...on, PAPERCLIP_PRIMARY_HOST: "acme.staging.paperclip.app" })?.environment).toBe("staging");
expect(resolveBrowserAnalyticsConfig({ ...on, PAPERCLIP_PRIMARY_HOST: "acme.paperclip.app" })?.environment).toBe("production");
});
it("never turns replay on without the browser switch", () => {
expect(resolveCloudPostHogConfig({ ...CLOUD, ...TOKEN, PAPERCLIP_POSTHOG_REPLAY_ENABLED: "1" })?.replayEnabled).toBe(false);
});
it("keeps browser analytics off for an invalid host", () => {
expect(resolveBrowserAnalyticsConfig({ ...CLOUD, ...TOKEN, ...BROWSER, PAPERCLIP_POSTHOG_HOST: "not a url" })).toBeNull();
});
it("maps PostHog Cloud hosts to their assets and app hosts", () => {
expect(posthogAssetsOrigin("https://us.i.posthog.com")).toBe("https://us-assets.i.posthog.com");
expect(posthogAssetsOrigin("https://eu.i.posthog.com")).toBe("https://eu-assets.i.posthog.com");
expect(posthogAssetsOrigin("http://127.0.0.1:9999")).toBe("http://127.0.0.1:9999");
expect(posthogUiHost("https://eu.i.posthog.com")).toBe("https://eu.posthog.com");
expect(posthogUiHost("http://127.0.0.1:9999")).toBeNull();
});
});
describe("PostHog proxy mount", () => {
it("is not mounted unless browser analytics is on", () => {
expect(resolvePostHogProxyMount({})).toBeNull();
expect(resolvePostHogProxyMount({ ...TOKEN, ...BROWSER, PAPERCLIP_CLOUD_STACK_ID: "s" })).toBeNull();
expect(resolvePostHogProxyMount({ ...CLOUD, ...TOKEN })).toBeNull();
expect(resolvePostHogProxyMount({ ...CLOUD, ...TOKEN, ...BROWSER })?.path).toBe("/_ph");
});
it("routes static assets to the assets host and events to the ingest host", () => {
const host = "https://us.i.posthog.com";
expect(posthogUpstreamUrl(host, "GET", "/static/recorder.js?v=1")?.href)
.toBe("https://us-assets.i.posthog.com/static/recorder.js?v=1");
expect(posthogUpstreamUrl(host, "GET", "/array/phc_x/config.js")?.origin).toBe("https://us-assets.i.posthog.com");
expect(posthogUpstreamUrl(host, "GET", "/array/phc_x/config")?.origin).toBe("https://us-assets.i.posthog.com");
expect(posthogUpstreamUrl(host, "GET", "/static/1.442.1/lazy-recorder.js")?.href)
.toBe("https://us-assets.i.posthog.com/static/1.442.1/lazy-recorder.js");
expect(posthogUpstreamUrl(host, "POST", "/e/?ip=0")?.href).toBe("https://us.i.posthog.com/e/?ip=0");
expect(posthogUpstreamUrl(host, "POST", "/i/v0/e/")?.origin).toBe("https://us.i.posthog.com");
expect(posthogUpstreamUrl(host, "POST", "/s/?compression=gzip-js")?.pathname).toBe("/s/");
expect(posthogUpstreamUrl(host, "POST", "/flags/?v=2")?.pathname).toBe("/flags/");
});
it.each([
["GET", "/e/"],
["PUT", "/e/"],
["DELETE", "/s/"],
["OPTIONS", "/flags/"],
["POST", "/static/recorder.js"],
["GET", "/api/projects/"],
["POST", "/api/surveys/"],
["GET", "/static/../api/x.js"],
["GET", "/static/recorder.css"],
["GET", "/static/v1/../x.js"],
["GET", "/static/a/b/c.js"],
["GET", "//evil.example/static/x.js"],
["POST", "//evil.example/e/"],
["GET", "/array/phc_x/../../api"],
["GET", "/"],
])("refuses %s %s", (method, path) => {
expect(posthogUpstreamUrl("https://us.i.posthog.com", method, path)).toBeNull();
});
});
describe("PostHog proxy forwarding", () => {
let upstream: http.Server;
let upstreamUrl: string;
let seen: Array<{ method?: string; url?: string; headers: http.IncomingHttpHeaders; body: string }>;
beforeEach(async () => {
seen = [];
upstream = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (chunk: Buffer) => chunks.push(chunk));
req.on("end", () => {
seen.push({ method: req.method, url: req.url, headers: req.headers, body: Buffer.concat(chunks).toString() });
res.writeHead(200, { "content-type": "application/json", "set-cookie": "upstream=1" });
res.end(JSON.stringify({ status: 1 }));
});
});
await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve));
upstreamUrl = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`;
});
afterEach(async () => {
await new Promise<void>((resolve) => upstream.close(() => resolve()));
});
function proxyApp() {
const mount = resolvePostHogProxyMount({ ...CLOUD, ...TOKEN, ...BROWSER, PAPERCLIP_POSTHOG_HOST: upstreamUrl });
expect(mount).not.toBeNull();
const app = express();
app.use(mount!.path, mount!.router);
// A body parser after the proxy, as in app.ts, must never see proxied bodies.
app.use(express.json());
return app;
}
it("streams the request body and strips Host, Cookie, Authorization and set-cookie", async () => {
const res = await request(proxyApp())
.post("/_ph/e/?compression=gzip-js")
.set("Host", "acme.paperclip.app")
.set("Content-Type", "application/json")
.set("Cookie", "paperclip.session=secret")
.set("Authorization", "Bearer secret")
.set("X-POSTHOG-SESSION-ID", "s1")
.send(JSON.stringify({ event: "$pageview" }));
expect(res.status).toBe(200);
expect(res.body).toEqual({ status: 1 });
expect(res.headers["set-cookie"]).toBeUndefined();
expect(seen).toHaveLength(1);
expect(seen[0]!.method).toBe("POST");
expect(seen[0]!.url).toBe("/e/?compression=gzip-js");
expect(seen[0]!.body).toBe(JSON.stringify({ event: "$pageview" }));
expect(seen[0]!.headers.host).toBe(new URL(upstreamUrl).host);
expect(seen[0]!.headers.cookie).toBeUndefined();
expect(seen[0]!.headers.authorization).toBeUndefined();
expect(seen[0]!.headers["x-posthog-session-id"]).toBeUndefined();
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/"],
["DELETE", "/_ph/e/"],
["GET", "/_ph/e/"],
["POST", "/_ph/static/recorder.js"],
])("answers 404 to %s %s without contacting PostHog", async (method, path) => {
const res = await request(proxyApp())[method.toLowerCase() as "get"](path);
expect(res.status).toBe(404);
expect(seen).toHaveLength(0);
});
it("refuses a declared body over the limit without contacting PostHog", async () => {
const app = express();
app.use("/_ph", posthogProxyRoutes(upstreamUrl, { maxBodyBytes: 10, maxInFlightPerClient: 16, maxInFlight: 256 }));
const res = await request(app).post("/_ph/e/").set("Content-Type", "text/plain").send("x".repeat(11));
expect(res.status).toBe(413);
expect(seen).toHaveLength(0);
});
it("cuts off a streamed body that grows past the limit", async () => {
const app = express();
app.use("/_ph", posthogProxyRoutes(upstreamUrl, { maxBodyBytes: 10, maxInFlightPerClient: 16, maxInFlight: 256 }));
const server = app.listen(0, "127.0.0.1");
await new Promise<void>((resolve) => server.once("listening", () => resolve()));
const port = (server.address() as AddressInfo).port;
try {
const status = await new Promise<number | string>((resolve) => {
const req = http.request({ host: "127.0.0.1", port, path: "/_ph/e/", method: "POST", headers: { "content-type": "text/plain", "transfer-encoding": "chunked" } }, (res) => {
res.resume();
resolve(res.statusCode ?? 0);
});
req.on("error", () => resolve("error"));
req.write("x".repeat(8));
req.write("x".repeat(8));
req.end();
});
expect([413, "error"]).toContain(status);
expect(seen.every((entry) => entry.body.length <= 10)).toBe(true);
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
it("limits requests in flight from one client", async () => {
let release: () => void = () => {};
const held = new Promise<void>((resolve) => { release = resolve; });
const slow = http.createServer(async (_req, res) => { await held; res.end("ok"); });
await new Promise<void>((resolve) => slow.listen(0, "127.0.0.1", resolve));
const app = express();
app.use("/_ph", posthogProxyRoutes(`http://127.0.0.1:${(slow.address() as AddressInfo).port}`, { maxBodyBytes: 1024, maxInFlightPerClient: 1, maxInFlight: 256 }));
const server = app.listen(0, "127.0.0.1");
await new Promise<void>((resolve) => server.once("listening", () => resolve()));
try {
const first = request(server).get("/_ph/static/array.js").then((res) => res.status);
await new Promise((resolve) => setTimeout(resolve, 50));
const second = await request(server).get("/_ph/static/array.js");
expect(second.status).toBe(429);
release();
expect(await first).toBe(200);
// The slot frees up once the first request finishes.
expect((await request(server).get("/_ph/static/array.js")).status).toBe(200);
} finally {
release();
await new Promise<void>((resolve) => server.close(() => resolve()));
await new Promise<void>((resolve) => slow.close(() => resolve()));
}
});
it("never forwards the inbound Host header", async () => {
await request(proxyApp()).get("/_ph/static/recorder.js").set("Host", "acme.paperclip.app");
expect(seen[0]!.headers.host).toBe(new URL(upstreamUrl).host);
});
it("answers 502 when PostHog cannot be reached", async () => {
const mount = resolvePostHogProxyMount({ ...CLOUD, ...TOKEN, ...BROWSER, PAPERCLIP_POSTHOG_HOST: "http://127.0.0.1:1" });
const app = express();
app.use(mount!.path, mount!.router);
const res = await request(app).get("/_ph/static/array.js");
expect(res.status).toBe(502);
});
it("survives PostHog closing the connection partway through a response", async () => {
const broken = http.createServer((_req, res) => {
res.writeHead(200, { "content-type": "application/javascript", "content-length": "100000" });
res.write("partial");
setTimeout(() => res.socket?.destroy(), 20);
});
await new Promise<void>((resolve) => broken.listen(0, "127.0.0.1", resolve));
const brokenUrl = `http://127.0.0.1:${(broken.address() as AddressInfo).port}`;
const errors: unknown[] = [];
const onUncaught = (err: unknown) => errors.push(err);
process.on("uncaughtException", onUncaught);
try {
const mount = resolvePostHogProxyMount({ ...CLOUD, ...TOKEN, ...BROWSER, PAPERCLIP_POSTHOG_HOST: brokenUrl });
const app = express();
app.use(mount!.path, mount!.router);
const server = app.listen(0, "127.0.0.1");
await new Promise<void>((resolve) => server.once("listening", () => resolve()));
const port = (server.address() as AddressInfo).port;
const outcome = await new Promise<string>((resolve) => {
http.get(`http://127.0.0.1:${port}/_ph/static/recorder.js`, (res) => {
res.on("data", () => {});
res.on("end", () => resolve("end"));
res.on("error", () => resolve("error"));
res.on("aborted", () => resolve("aborted"));
}).on("error", () => resolve("error"));
});
// The browser sees a cut-off response, not a complete one.
expect(outcome).not.toBe("end");
await new Promise((resolve) => setTimeout(resolve, 50));
// The proxy keeps serving after the broken response.
const health = await request(server).put("/_ph/e/");
expect(health.status).toBe(404);
await new Promise<void>((resolve) => server.close(() => resolve()));
} finally {
process.off("uncaughtException", onUncaught);
await new Promise<void>((resolve) => broken.close(() => resolve()));
}
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");
});
});