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(activity): guard new URL() and JSON.parse in live events feed (#61958)

posthog[bot]c42808b8d8bd
diff --git a/frontend/src/scenes/activity/live/liveEventsLogic.test.ts b/frontend/src/scenes/activity/live/liveEventsLogic.test.ts
new file mode 100644
index 000000000000..8885c5206ce1
--- /dev/null
+++ b/frontend/src/scenes/activity/live/liveEventsLogic.test.ts
@@ -0,0 +1,54 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { initKeaTests } from '~/test/init'
+import { LiveEvent } from '~/types'
+
+import { liveEventsLogic } from './liveEventsLogic'
+
+function makeLiveEvent(currentUrl?: string): LiveEvent {
+ return {
+ uuid: 'abc',
+ event: '$pageview',
+ properties: currentUrl !== undefined ? { $current_url: currentUrl } : {},
+ timestamp: '2026-01-01T00:00:00Z',
+ team_id: 1,
+ distinct_id: 'user-1',
+ created_at: '2026-01-01T00:00:00Z',
+ }
+}
+
+describe('liveEventsLogic', () => {
+ let logic: ReturnType<typeof liveEventsLogic.build>
+
+ beforeEach(() => {
+ initKeaTests()
+ logic = liveEventsLogic()
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic?.unmount()
+ })
+
+ describe('addEvents host extraction', () => {
+ it('records the host for a valid $current_url', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.addEvents([makeLiveEvent('https://app.posthog.com/insights')])
+ }).toMatchValues({
+ eventHosts: ['https://app.posthog.com'],
+ })
+ })
+
+ it.each([
+ ['malformed URL', 'not a url'],
+ ['empty host', 'https://'],
+ ['missing $current_url', undefined],
+ ])('does not throw and records no host for %s', async (_label, currentUrl) => {
+ await expectLogic(logic, () => {
+ logic.actions.addEvents([makeLiveEvent(currentUrl)])
+ }).toMatchValues({
+ eventHosts: [],
+ })
+ })
+ })
+})
diff --git a/frontend/src/scenes/activity/live/liveEventsLogic.tsx b/frontend/src/scenes/activity/live/liveEventsLogic.tsx
index ba56ef0bc985..0589d6d97ec5 100644
--- a/frontend/src/scenes/activity/live/liveEventsLogic.tsx
+++ b/frontend/src/scenes/activity/live/liveEventsLogic.tsx
@@ -148,7 +148,13 @@ export const liveEventsLogic = kea<liveEventsLogicType>([
signal: cache.eventSourceController.signal,
onMessage: (event) => {
lemonToast.dismiss(ERROR_TOAST_ID)
- const eventData = JSON.parse(event.data)
+ let eventData: LiveEvent
+ try {
+ eventData = JSON.parse(event.data)
+ } catch {
+ // Drop malformed stream payloads rather than throwing inside the listener
+ return
+ }
cache.batch.push(eventData)
if (cache.batch.length >= 10 || performance.now() - (values.lastBatchTimestamp || 0) > 300) {
actions.addEvents(cache.batch)
@@ -181,9 +187,13 @@ export const liveEventsLogic = kea<liveEventsLogicType>([
const event = events[0]
const eventUrl = event.properties?.$current_url
if (eventUrl) {
- const eventHost = new URL(eventUrl).host
- const eventProtocol = new URL(eventUrl).protocol
- actions.addEventHost(`${eventProtocol}//${eventHost}`)
+ // Live events can carry a malformed `$current_url`; skip host extraction rather than throw.
+ try {
+ const parsedUrl = new URL(eventUrl)
+ actions.addEventHost(`${parsedUrl.protocol}//${parsedUrl.host}`)
+ } catch {
+ // Ignore unparseable URLs
+ }
}
// The team's `ingested_event` flag is flipped server-side when the first event is
// processed, but only reaches the frontend on the next ~30s team refresh. Refresh