Skip to content

Commit 7268778

Browse files
committed
protect session id and mint before first window
1 parent 6dbc74f commit 7268778

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

apps/code/src/main/services/posthog-analytics.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,13 @@ describe("posthog-analytics", () => {
9797
}),
9898
);
9999
});
100+
101+
it("stamps the main-owned session id and ignores a caller override", () => {
102+
captureException(new Error("boom"), { $session_id: "spoofed" });
103+
104+
const props = mockCaptureException.mock.calls.at(-1)?.[2];
105+
expect(props.$session_id).toMatch(
106+
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
107+
);
108+
});
100109
});

apps/code/src/main/services/posthog-analytics.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export function initializePostHog() {
2323
enableExceptionAutocapture: true,
2424
});
2525

26+
// Mint the main-owned session id now, before the first window, so crash
27+
// handlers can stamp $session_id even when the renderer crashes during
28+
// startup (before it fetches the id to bootstrap posthog-js).
29+
getOrCreateSessionId();
30+
2631
return posthogClient;
2732
}
2833

@@ -121,8 +126,10 @@ export function captureException(
121126
const distinctId = currentUserId || "anonymous-app-event";
122127
posthogClient.captureException(error, distinctId, {
123128
team: "posthog-code",
124-
...(sessionId ? { $session_id: sessionId } : {}),
125129
...additionalProperties,
130+
// System-owned fields last so callers can't overwrite them: main owns the
131+
// session id used for crash->replay linking.
132+
...(sessionId ? { $session_id: sessionId } : {}),
126133
app_version: getAppVersion(),
127134
});
128135
}

0 commit comments

Comments
 (0)