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(toolbar): stop hedgehog config save from logging users out (#65929)

Fernando Gomeseff2fe8af541
diff --git a/frontend/src/lib/components/HedgehogMode/hedgehogModeLogic.ts b/frontend/src/lib/components/HedgehogMode/hedgehogModeLogic.ts
index edecb88f33f8..67ca489f3e62 100644
--- a/frontend/src/lib/components/HedgehogMode/hedgehogModeLogic.ts
+++ b/frontend/src/lib/components/HedgehogMode/hedgehogModeLogic.ts
@@ -65,26 +65,23 @@ export const hedgehogModeLogic = kea<hedgehogModeLogicType>([
},
updateRemoteConfig: async ({ config }) => {
- try {
- const endpoint = '/api/users/@me/hedgehog_config'
- let newConfig: Partial<HedgehogConfig>
-
- const payload = {
- ...values.hedgehogConfig,
- ...config,
- }
+ const endpoint = '/api/users/@me/hedgehog_config'
+ const payload = {
+ ...values.hedgehogConfig,
+ ...config,
+ }
- const mountedToolbarConfigLogic = toolbarConfigLogic.findMounted()
- if (mountedToolbarConfigLogic) {
- // If toolbarConfigLogic is mounted, we're inside the Toolbar
- if (!mountedToolbarConfigLogic.values.isAuthenticated) {
- return null
- }
- newConfig = await (await toolbarFetch(endpoint, 'PATCH', payload)).json()
- } else {
- newConfig = await api.update(endpoint, payload)
- }
+ const mountedToolbarConfigLogic = toolbarConfigLogic.findMounted()
+ if (mountedToolbarConfigLogic) {
+ // Inside the Toolbar the OAuth token is scoped to `user:read` only
+ // (TOOLBAR_OAUTH_SCOPES grants no `user:write`), so persisting the hedgehog
+ // config would 403 — and toolbarFetch turns any 403 into a session reset,
+ // kicking the user out of the Toolbar. Keep the config local to this session.
+ return mountedToolbarConfigLogic.values.isAuthenticated ? payload : null
+ }
+ try {
+ const newConfig: Partial<HedgehogConfig> = await api.update(endpoint, payload)
return newConfig ?? null
} catch (e) {
console.error('Failed to update hedgehog remote config', e)
diff --git a/frontend/src/toolbar/shims/shimConsumerIntegration.test.ts b/frontend/src/toolbar/shims/shimConsumerIntegration.test.ts
index 3e5c9aedc670..aa0877b62a66 100644
--- a/frontend/src/toolbar/shims/shimConsumerIntegration.test.ts
+++ b/frontend/src/toolbar/shims/shimConsumerIntegration.test.ts
@@ -36,6 +36,14 @@ describe('shim consumer integration', () => {
})
describe('hedgehogModeLogic with shims', () => {
+ // MSW installs a never-resolving global.fetch in a beforeAll and never restores it per test,
+ // so a test that swaps global.fetch must put it back or the swap leaks into later tests.
+ let restoreFetch: (() => void) | undefined
+ afterEach(() => {
+ restoreFetch?.()
+ restoreFetch = undefined
+ })
+
it('mounts without error and has shimmed defaults', async () => {
const { hedgehogModeLogic } = await import('~/lib/components/HedgehogMode/hedgehogModeLogic')
const logic = hedgehogModeLogic.build()
@@ -53,6 +61,49 @@ describe('shim consumer integration', () => {
logic.mount()
}).toDispatchActions(['loadRemoteConfig'])
})
+
+ it('does not PATCH hedgehog_config from the Toolbar, keeping the session alive', async () => {
+ // The Toolbar OAuth token is scoped to `user:read` but not `user:write`, so the real
+ // backend 403s the PATCH while the GET still succeeds. toolbarFetch turns any 403 into a
+ // session reset, which used to log the user out of the Toolbar whenever hedgehog mode
+ // moved the hedgehog around. Swap in our own mock and let afterEach restore the original.
+ const originalFetch = global.fetch
+ restoreFetch = () => {
+ global.fetch = originalFetch
+ }
+ const fetchMock = jest.fn((_url: string, options?: RequestInit) =>
+ Promise.resolve({
+ ok: options?.method !== 'PATCH',
+ status: options?.method === 'PATCH' ? 403 : 200,
+ json: () => Promise.resolve({}),
+ } as Response)
+ )
+ global.fetch = fetchMock as unknown as typeof fetch
+
+ // Authenticate the Toolbar session — this is the path that used to break.
+ toolbarConfigLogic.actions.setOAuthTokens('access-token', 'refresh-token', 'client-id')
+ expect(toolbarConfigLogic.values.isAuthenticated).toBe(true)
+
+ const { hedgehogModeLogic } = await import('~/lib/components/HedgehogMode/hedgehogModeLogic')
+ const logic = hedgehogModeLogic.build()
+ logic.mount()
+
+ await expectLogic(logic, () => {
+ logic.actions.updateRemoteConfig({ enabled: true })
+ }).toDispatchActions(['updateRemoteConfigSuccess'])
+
+ // The write must never hit the network — a 403 there resets the Toolbar session.
+ const patchCalls = fetchMock.mock.calls.filter(([, options]) => options?.method === 'PATCH')
+ expect(patchCalls).toHaveLength(0)
+
+ // Session survives: the user is not kicked out of the Toolbar.
+ expect(toolbarConfigLogic.values.isAuthenticated).toBe(true)
+
+ // The change is still applied locally so the hedgehog reflects it this session.
+ expect(logic.values.remoteConfig).toMatchObject({ enabled: true })
+
+ logic.unmount()
+ })
})
describe('themeLogic with shims', () => {