Skip to content

Commit

Permalink
fix: endless capturing /s/ (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Nov 26, 2024
1 parent 92bb3e6 commit d49244f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
36 changes: 30 additions & 6 deletions src/__tests__/extensions/replay/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
17 changes: 14 additions & 3 deletions src/extensions/replay/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d49244f

Please sign in to comment.