diff --git a/src/__tests__/extensions/replay/config.test.ts b/src/__tests__/extensions/replay/config.test.ts index 9f89b5840..20b2faec3 100644 --- a/src/__tests__/extensions/replay/config.test.ts +++ b/src/__tests__/extensions/replay/config.test.ts @@ -75,34 +75,58 @@ describe('config', () => { { name: 'https://app.posthog.com/api/feature_flag/', }, + undefined, ], [ { - name: 'https://app.posthog.com/s/', + name: 'https://app.posthog.com/s/?ip=1&ver=123', }, undefined, + undefined, ], [ { - name: 'https://app.posthog.com/e/', + name: 'https://app.posthog.com/e/?ip=1&ver=123', }, undefined, + undefined, ], [ { - name: 'https://app.posthog.com/i/v0/e/', + name: 'https://app.posthog.com/i/v0/e/?ip=1&ver=123', }, undefined, + undefined, ], [ { // even an imaginary future world of rust session replay capture - name: 'https://app.posthog.com/i/v0/s/', + name: 'https://app.posthog.com/i/v0/s/?ip=1&ver=123', }, undefined, + undefined, ], - ])('ignores ingestion paths', (capturedRequest, expected) => { - const networkOptions = buildNetworkRequestOptions(defaultConfig(), {}) + [ + { + // using a relative path as a reverse proxy api host + name: 'https://app.posthog.com/ingest/s/?ip=1&ver=123', + }, + undefined, + '/ingest', + ], + [ + { + // using a reverse proxy with a path + name: 'https://app.posthog.com/ingest/s/?ip=1&ver=123', + }, + undefined, + 'https://app.posthog.com/ingest', + ], + ])('ignores ingestion paths', (capturedRequest, expected, apiHost?: string) => { + const networkOptions = buildNetworkRequestOptions( + { ...defaultConfig(), api_host: apiHost || 'https://us.posthog.com' }, + {} + ) const x = networkOptions.maskRequestFn!(capturedRequest as CapturedNetworkRequest) expect(x).toEqual(expected) }) diff --git a/src/extensions/replay/config.ts b/src/extensions/replay/config.ts index 466a2d7b9..d4bc96c4f 100644 --- a/src/extensions/replay/config.ts +++ b/src/extensions/replay/config.ts @@ -96,9 +96,20 @@ const removeAuthorizationHeader = (data: CapturedNetworkRequest): CapturedNetwor const POSTHOG_PATHS_TO_IGNORE = ['/s/', '/e/', '/i/'] // want to ignore posthog paths when capturing requests, or we can get trapped in a loop // because calls to PostHog would be reported using a call to PostHog which would be reported.... -const ignorePostHogPaths = (data: CapturedNetworkRequest): CapturedNetworkRequest | undefined => { +const ignorePostHogPaths = ( + data: CapturedNetworkRequest, + apiHostConfig: PostHogConfig['api_host'] +): CapturedNetworkRequest | undefined => { const url = convertToURL(data.name) - if (url && url.pathname && POSTHOG_PATHS_TO_IGNORE.some((path) => url.pathname.indexOf(path) === 0)) { + + // we need to account for api host config as e.g. pathname could be /ingest/s/ and we want to ignore that + let replaceValue = apiHostConfig.indexOf('http') === 0 ? convertToURL(apiHostConfig)?.pathname : apiHostConfig + if (replaceValue === '/') { + replaceValue = '' + } + const pathname = url?.pathname.replace(replaceValue || '', '') + + if (url && pathname && POSTHOG_PATHS_TO_IGNORE.some((path) => pathname.indexOf(path) === 0)) { return undefined } return data @@ -211,7 +222,7 @@ export const buildNetworkRequestOptions = ( const payloadLimiter = limitPayloadSize(config) const enforcedCleaningFn: NetworkRecordOptions['maskRequestFn'] = (d: CapturedNetworkRequest) => - payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d))) + payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d), instanceConfig.api_host)) const hasDeprecatedMaskFunction = isFunction(instanceConfig.session_recording.maskNetworkRequestFn)