Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cdp): disable geoip on internal events without a client ip (#115354)

Meikel Ratz9a2e52343a5d
diff --git a/nodejs/src/common/services/internal-capture.test.ts b/nodejs/src/common/services/internal-capture.test.ts
index 0b854ed1bc68..15f83ef77ba9 100644
--- a/nodejs/src/common/services/internal-capture.test.ts
+++ b/nodejs/src/common/services/internal-capture.test.ts
@@ -35,7 +35,7 @@ describe('InternalCaptureService', () => {
[
"http://localhost:8010/capture",
{
- "body": "{"api_key":"token","timestamp":"2025-01-01T00:00:00.000Z","distinct_id":"distinct-id","sent_at":"2025-01-01T00:00:00.000Z","event":"event-name","properties":{"capture_internal":true}}",
+ "body": "{"api_key":"token","timestamp":"2025-01-01T00:00:00.000Z","distinct_id":"distinct-id","sent_at":"2025-01-01T00:00:00.000Z","event":"event-name","properties":{"$geoip_disable":true,"capture_internal":true}}",
"headers": {
"Content-Type": "application/json",
},
@@ -47,6 +47,17 @@ describe('InternalCaptureService', () => {
)
})
+ it.each([
+ ['a client ip', { $ip: '203.0.113.7' }, { $ip: '203.0.113.7' }],
+ ['a caller opt-in', { $geoip_disable: false }, { $geoip_disable: false }],
+ ])('leaves geoip to the event when it has %s', async (_name, properties, expected) => {
+ await service.capture({ team_token: 'token', event: 'event-name', distinct_id: 'distinct-id', properties })
+ expect(parseJSON(mockInternalFetch.mock.calls[0][1].body).properties).toEqual({
+ ...expected,
+ capture_internal: true,
+ })
+ })
+
it('should allow some overrides', async () => {
await service.capture({
team_token: 'token',
@@ -64,6 +75,7 @@ describe('InternalCaptureService', () => {
"distinct_id": "distinct-id",
"event": "event-name",
"properties": {
+ "$geoip_disable": true,
"capture_internal": true,
"foo": "bar",
},
diff --git a/nodejs/src/common/services/internal-capture.ts b/nodejs/src/common/services/internal-capture.ts
index e46d9ffb3bd7..9cff18a12d1b 100644
--- a/nodejs/src/common/services/internal-capture.ts
+++ b/nodejs/src/common/services/internal-capture.ts
@@ -28,11 +28,17 @@ type CapturePayloadFormat = {
properties: Record<string, any>
}
+/** An internal event carries the server's IP, not the user's. GeoIP would turn it into a wrong or
+ * placeholder location and write that over the person's real location. */
+export function disableGeoipWithoutClientIp(properties: Record<string, any>): Record<string, any> {
+ return properties.$ip ? properties : { $geoip_disable: true, ...properties }
+}
+
export class InternalCaptureService {
constructor(private config: Pick<CommonConfig, 'CAPTURE_INTERNAL_URL'>) {}
private prepareEvent(event: InternalCaptureEvent): CapturePayloadFormat {
- const properties = { ...(event.properties ?? {}), capture_internal: true }
+ const properties = { ...disableGeoipWithoutClientIp(event.properties ?? {}), capture_internal: true }
const now = DateTime.utc().toISO()
return {
api_key: event.team_token,
diff --git a/nodejs/src/messaging/push-subscriptions/push-capture.test.ts b/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
index f4c8200d2d2c..7ccac173cd31 100644
--- a/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
@@ -67,7 +67,7 @@ describe('PushCaptureService', () => {
{
event: '$set',
distinct_id: 'user-1',
- properties: { $set: { $device_push_subscription_app: 'cipher' } },
+ properties: { $set: { $device_push_subscription_app: 'cipher' }, $geoip_disable: true },
options: { process_person_profile: true },
},
],
diff --git a/nodejs/src/messaging/push-subscriptions/push-capture.ts b/nodejs/src/messaging/push-subscriptions/push-capture.ts
index 17812b2a28ba..2cfe75052b77 100644
--- a/nodejs/src/messaging/push-subscriptions/push-capture.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-capture.ts
@@ -1,6 +1,7 @@
import { randomUUID } from 'crypto'
import { Counter } from 'prom-client'
+import { disableGeoipWithoutClientIp } from '~/common/services/internal-capture'
import { parseJSON } from '~/common/utils/json-parse'
import { logger } from '~/common/utils/logger'
import { FetchResponse, internalFetch } from '~/common/utils/request'
@@ -68,7 +69,7 @@ export class PushCaptureService {
uuid: randomUUID(),
distinct_id: event.distinctId,
timestamp: now,
- properties,
+ properties: disableGeoipWithoutClientIp(properties),
}
if (Object.keys(options).length > 0) {
entry.options = options