From b85872c0695a3b832ebdac34d0e17ec61378120e Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Mon, 25 Nov 2024 23:30:28 +0000 Subject: [PATCH] ah, much nicer --- src/__tests__/extensions/replay/config.test.ts | 17 +++++++++++++++-- src/extensions/replay/config.ts | 18 +++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/__tests__/extensions/replay/config.test.ts b/src/__tests__/extensions/replay/config.test.ts index 46b052522..6ef2d93f9 100644 --- a/src/__tests__/extensions/replay/config.test.ts +++ b/src/__tests__/extensions/replay/config.test.ts @@ -75,24 +75,28 @@ describe('config', () => { { name: 'https://app.posthog.com/api/feature_flag/', }, + undefined, ], [ { name: 'https://app.posthog.com/s/?ip=1&ver=123', }, undefined, + undefined, ], [ { name: 'https://app.posthog.com/e/?ip=1&ver=123', }, undefined, + undefined, ], [ { name: 'https://app.posthog.com/i/v0/e/?ip=1&ver=123', }, undefined, + undefined, ], [ { @@ -100,9 +104,18 @@ describe('config', () => { name: 'https://app.posthog.com/i/v0/s/?ip=1&ver=123', }, undefined, + undefined, ], - ])('ignores ingestion paths', (capturedRequest, expected) => { - const networkOptions = buildNetworkRequestOptions(defaultConfig(), {}) + [ + { + // even an imaginary future world of rust session replay capture + name: 'https://app.posthog.com/ingest/s/?ip=1&ver=123', + }, + undefined, + '/ingest', + ], + ])('ignores ingestion paths', (capturedRequest, expected, apiHost?: string) => { + const networkOptions = buildNetworkRequestOptions({ ...defaultConfig(), api_host: apiHost }, {}) 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 0fccaedd1..7f021ffd1 100644 --- a/src/extensions/replay/config.ts +++ b/src/extensions/replay/config.ts @@ -96,17 +96,13 @@ 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 && - // matches our `/s/` path but also paths within a reverse proxy like `/ingest/s/` - POSTHOG_PATHS_TO_IGNORE.some((path) => url.pathname.indexOf(path) >= 0) && - // since we're matching `/blah/` loosely, also check a couple of the query params we add - url.search.indexOf('ver=') >= 0 && - url.search.indexOf('ip=') >= 0 - ) { + const pathname = url?.pathname.replace(apiHostConfig, '') + if (url && pathname && POSTHOG_PATHS_TO_IGNORE.some((path) => pathname.indexOf(path) === 0)) { return undefined } return data @@ -219,7 +215,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)