Files
PaperClipAI/server/src/__tests__
DottaandClaude Opus 5.5 fa8bc78d2a fix(posthog-proxy): forward the visitor IP behind a private proxy hop (#22052)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Cloud sends browser analytics to PostHog through a
same-origin proxy at `/_ph`
> - On Cloud, the server runs behind Railway's private proxy hop and
`TRUST_PROXY` is not set
> - So the proxy forwarded the hop address (100.64.x.x) to PostHog
instead of the visitor's IP
> - PostHog could not resolve a location, and every visitor shared one
per-client in-flight bucket
> - This pull request makes the proxy use the first `X-Forwarded-For`
entry when the socket peer is private
> - The benefit is correct visitor location in PostHog web analytics and
per-visitor limits that work on Cloud

## Linked Issues or Issue Description

**What happened?**
On Paperclip Cloud staging, every browser event that reaches PostHog
through the `/_ph` proxy has `$ip` set to a Railway private address
(100.64.0.5 to 100.64.0.21). PostHog shows no country or city. All
visitors also share a few per-client in-flight buckets.

**Expected behavior**
PostHog receives the visitor's own IP, so web analytics shows visitor
location. Each visitor gets its own in-flight bucket.

**Steps to reproduce**
1. Turn on browser analytics on a Cloud stack
(`PAPERCLIP_POSTHOG_BROWSER_ENABLED=1`) without `TRUST_PROXY`.
2. Load the board and navigate a few pages.
3. In PostHog, query `properties.$ip` for those `$pageview` events. The
value is a 100.64.x.x address and there is no geo data.

**Deployment mode**
Paperclip Cloud (authenticated, hosted on Railway).

## What Changed

- Add `posthogVisitorIp()` in `server/src/routes/posthog-proxy.ts`.
- Use it for the forwarded `X-Forwarded-For` header and for the
per-visitor in-flight limit.
- Add tests for the helper and for the forwarded header.

## Verification

- `npx vitest run src/__tests__/cloud-posthog-browser.test.ts` (server):
51 pass.
- Red check: with the old proxy code, the new tests fail (11 failures).
- Reproduced on paperclip.staging.paperclip.app: PostHog `$ip` values
were 100.64.0.5 to 100.64.0.21, with no location.

## Risks

- A client can set its own `X-Forwarded-For`. This only changes its own
analytics location and its per-visitor bucket. The global in-flight
limit still applies.
- The proxy is mounted only on Cloud with browser analytics on, so
self-hosted installs are not affected.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use.

## 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-17 14:18:45 -06:00
..
…

Server Tests

Server tests that need a real PostgreSQL process must use ./helpers/embedded-postgres.ts instead of constructing embedded-postgres directly.

The shared helper creates a throwaway data directory and a reserved-safe loopback port for each test database. This protects the live Paperclip control-plane Postgres from server vitest runs; see PAP-2033 for the incident that introduced this guard.