: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(integrations): show a recoverable message when an OAuth callback state expires (#76173)
Tom Owersa3291f1a2dee
diff --git a/frontend/src/lib/integrations/integrationsLogic.test.ts b/frontend/src/lib/integrations/integrationsLogic.test.tsindex c01657322318..7baf7c314ef3 100644--- a/frontend/src/lib/integrations/integrationsLogic.test.ts+++ b/frontend/src/lib/integrations/integrationsLogic.test.ts@@ -68,6 +68,23 @@ describe('integrationsLogic — handleOauthCallback', () => {})})+ it('does not create the integration when the OAuth state token no longer matches the cookie', async () => {+ // A stale/expired flow: the cookie minted at authorize time is gone or changed, so the token+ // carried in the state can't match. The callback must recover by redirecting back rather than+ // POST a create, so an expired (or forged) state can never link an integration.+ document.cookie = 'ph_oauth_state=a-different-token'+ const state = 'next=%2Fproject%2F228502%2Fsettings%2Fproject-integrations&token=csrf-tok'++ await expectLogic(logic, () => {+ logic.actions.handleOauthCallback('slack' as IntegrationKind, { state, code: 'oauth-code' })+ }).toFinishAllListeners()++ expect(createSpy).not.toHaveBeenCalled()+ expect(router.values.location.pathname).toBe('/project/228502/settings/project-integrations')++ document.cookie = 'ph_oauth_state=; expires=Thu, 01 Jan 1970 00:00:00 GMT'+ })+describe('integration create team scoping', () => {let requestedTeamIds: string[]diff --git a/frontend/src/lib/integrations/integrationsLogic.ts b/frontend/src/lib/integrations/integrationsLogic.tsindex 94a357abf873..5b1dfe2b6ea2 100644--- a/frontend/src/lib/integrations/integrationsLogic.ts+++ b/frontend/src/lib/integrations/integrationsLogic.ts@@ -945,11 +945,17 @@ export const integrationsLogic = kea<integrationsLogicType>([return}- try {- if (token !== getCookie('ph_oauth_state')) {- throw new Error('Invalid state token')- }+ // The CSRF state cookie only lives for a few minutes (see ph_oauth_state max_age). If the+ // authorization round-trip outlived it, or the browser dropped the cookie, the token won't+ // match. That's recoverable by simply retrying, so say so instead of surfacing the generic+ // "Something went wrong" that a thrown error would fall back to.+ if (token !== getCookie('ph_oauth_state')) {+ lemonToast.error('This connection attempt expired before it could finish. Please try connecting again.')+ router.actions.replace(replaceUrl)+ return+ }+ try {if (source === 'mcp_store') {replaceUrl += `${replaceUrl.includes('?') ? '&' : '?'}code=${encodeURIComponent(code)}&server_id=${encodeURIComponent(server_id)}&state_token=${encodeURIComponent(token)}`lemonToast.success('Authorization successful.')