From 362d4680de9164aac8c4b4c3f6c5a0d6ffcaf8fc Mon Sep 17 00:00:00 2001 From: Ben White Date: Tue, 5 Dec 2023 09:06:53 +0100 Subject: [PATCH] fix: Sanitize given api_host urls to not have a trailing slash --- src/__tests__/posthog-core.js | 9 +++++++++ src/posthog-core.ts | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index f9262e0ab..7db3a4f6c 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -702,6 +702,15 @@ describe('posthog core', () => { expect(given.overrides._send_request.mock.calls.length).toBe(0) // No outgoing requests }) + it('sanitizes api_host urls', () => { + given('config', () => ({ + api_host: 'https://example.com/custom/', + })) + given.subject() + + expect(given.lib.config.api_host).toBe('https://example.com/custom') + }) + it('does not set __loaded_recorder_version flag if recording script has not been included', () => { given('overrides', () => ({ __loaded_recorder_version: undefined, diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 0de7d319a..4deecff6f 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -1712,6 +1712,8 @@ export class PostHog { this.config.disable_persistence = this.config.disable_cookie } + // We assume the api_host is without a trailing slash in most places throughout the codebase + this.config.api_host = this.config.api_host.replace(/\/$/, '') this.persistence?.update_config(this.config) this.sessionPersistence?.update_config(this.config)