: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(oauth): don't authorize the wrong project while a team_id hint is pending (#66438)
Edwin Lim323fe96d2d7e
diff --git a/frontend/src/scenes/oauth/oauthAuthorizeLogic.test.ts b/frontend/src/scenes/oauth/oauthAuthorizeLogic.test.tsindex caf6571a7d72..a97b7ba4802d 100644--- a/frontend/src/scenes/oauth/oauthAuthorizeLogic.test.ts+++ b/frontend/src/scenes/oauth/oauthAuthorizeLogic.test.ts@@ -194,4 +194,80 @@ describe('oauthAuthorizeLogic', () => {expect(logic.values.deniedScopeObjects).toEqual([])expect(logic.values.effectiveScopes).toEqual(['openid', 'insight:write'])})++ const HINT_TEAMS = [+ { id: 11, organization: 'org-a', name: 'A project' },+ { id: 22, organization: 'org-b', name: 'B project' },+ ]+ const CURRENT_TEAM = {+ id: MOCK_DEFAULT_USER.team?.id,+ organization: MOCK_DEFAULT_USER.organization?.id,+ name: 'Current project',+ }++ // Once teams load, a team_id hint resolves to that project + its org; an+ // inaccessible hint falls back to the user's current org/team.+ const hintResolutionCases: {+ name: string+ hint: number+ teams: any[]+ expectedOrg: string | undefined+ expectedTeams: (number | undefined)[]+ }[] = [+ {+ name: 'resolves the hinted project and its org from the team_id param',+ hint: 22,+ teams: HINT_TEAMS,+ expectedOrg: 'org-b',+ expectedTeams: [22],+ },+ {+ name: 'falls back to the current org/team when the hinted team is inaccessible',+ hint: 999,+ teams: [...HINT_TEAMS, CURRENT_TEAM],+ expectedOrg: MOCK_DEFAULT_USER.organization?.id,+ expectedTeams: [MOCK_DEFAULT_USER.team?.id],+ },+ ]++ it.each(hintResolutionCases)('team_id hint $name', ({ hint, teams, expectedOrg, expectedTeams }) => {+ logic.actions.setTeamHint(hint)+ logic.actions.setRequiredAccessLevel('team')+ logic.actions.loadAllTeamsSuccess(teams as any)+ expect(logic.values.teamHint).toBeNull()+ expect(logic.values.selectedOrganization).toBe(expectedOrg)+ expect(logic.values.oauthAuthorization.scoped_teams).toEqual(expectedTeams)+ })++ // Before teams load, a pending hint suppresses the eager current-org selection+ // (so a fast CTA click can't authorize the wrong project), while the no-hint+ // path keeps pre-selecting the current org/team.+ const eagerSelectionCases: {+ name: string+ hint: number | null+ expectedOrg: string | null | undefined+ expectedTeams: (number | undefined)[]+ }[] = [+ {+ name: 'leaves the selection empty while a team_id hint is pending',+ hint: 22,+ expectedOrg: null,+ expectedTeams: [],+ },+ {+ name: 'pre-selects the current org/team when no team_id hint is given',+ hint: null,+ expectedOrg: MOCK_DEFAULT_USER.organization?.id,+ expectedTeams: [MOCK_DEFAULT_USER.team?.id],+ },+ ]++ it.each(eagerSelectionCases)('before teams load, $name', ({ hint, expectedOrg, expectedTeams }) => {+ if (hint !== null) {+ logic.actions.setTeamHint(hint)+ }+ logic.actions.setRequiredAccessLevel('team')+ expect(logic.values.selectedOrganization).toBe(expectedOrg)+ expect(logic.values.oauthAuthorization.scoped_teams).toEqual(expectedTeams)+ })})diff --git a/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts b/frontend/src/scenes/oauth/oauthAuthorizeLogic.tsindex 6ce7ace7fb9c..9f40469e9cba 100644--- a/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts+++ b/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts@@ -339,9 +339,15 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([actions.setOauthAuthorizationValue('access_type', 'organizations')} else if (requiredAccessLevel === 'team') {actions.setOauthAuthorizationValue('access_type', 'teams')- const user = userLogic.values.user- if (user?.organization?.id) {- actions.setSelectedOrganization(user.organization.id, user?.team?.id)+ // With a team_id hint pending, let it drive org+project selection once+ // teams load — don't pre-select the user's current org/team, or a CTA+ // link could authorize the wrong project before the hint resolves. The+ // empty project keeps the submit blocked until the hint fills it in.+ if (!values.teamHint) {+ const user = userLogic.values.user+ if (user?.organization?.id) {+ actions.setSelectedOrganization(user.organization.id, user?.team?.id)+ }}}},@@ -360,23 +366,36 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([},loadAllTeamsSuccess: () => {const teams = values.sortedTeams+ if (!teams) {+ return+ }// A team_id hint from the authorize URL (e.g. the wizard's --project-id) wins:// pre-select that project and its org so the user just clicks Authorize. We only// honor it if the user has access to that team (it's in their loaded teams), and// consume it once — so it can't override a later manual change or a project the// user creates here (both also re-fire this listener).- if (values.teamHint && teams && values.requiredAccessLevel === 'team') {+ if (values.teamHint) {const hinted = teams.find((t) => t.id === values.teamHint)actions.setTeamHint(null)- if (hinted) {+ if (hinted && values.requiredAccessLevel === 'team') {actions.setSelectedOrganization(hinted.organization, hinted.id)return}+ // Hint didn't resolve (inaccessible team, or not a team-level grant). Fall+ // back to the user's current org/team — setRequiredAccessLevel skipped this+ // while the hint was pending, so without it the screen would be left empty.+ if (values.requiredAccessLevel === 'team' && !values.selectedOrganization) {+ const user = userLogic.values.user+ if (user?.organization?.id) {+ actions.setSelectedOrganization(user.organization.id, user?.team?.id)+ return+ }+ }}// After teams load, auto-select first project if org is set but no project selectedconst orgId = values.selectedOrganizationconst currentTeams = values.oauthAuthorization.scoped_teams- if (orgId && (!currentTeams || currentTeams.length === 0) && teams) {+ if (orgId && (!currentTeams || currentTeams.length === 0)) {const orgTeams = teams.filter((t) => t.organization === orgId)if (orgTeams.length > 0) {actions.setOauthAuthorizationValue('scoped_teams', [orgTeams[0].id])