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(desktop): stop background auth calls bypassing the scope-reauth gate (#94116)

Paul D'Ambra973e2c101d78
diff --git a/products/desktop/packages/core/src/auth/auth.test.ts b/products/desktop/packages/core/src/auth/auth.test.ts
index e2ddf19294ad..487eee19a8a4 100644
--- a/products/desktop/packages/core/src/auth/auth.test.ts
+++ b/products/desktop/packages/core/src/auth/auth.test.ts
@@ -476,6 +476,28 @@ describe("AuthService", () => {
});
});
+ it("does not let a caller silently refresh past a stale scope version", async () => {
+ seedStoredSession({
+ refreshToken: "refresh-token",
+ selectedProjectId: 123,
+ scopeVersion: OAUTH_SCOPE_VERSION - 1,
+ });
+ oauthFlow.refreshToken.mockResolvedValue(mockTokenResponse());
+ stubAuthFetch();
+
+ await service.initialize();
+ expect(service.getState().needsScopeReauth).toBe(true);
+
+ // A caller that only wants a token (e.g. a background usage/telemetry
+ // fetch) must not resurrect the session on the old scope grant behind
+ // the reauth prompt's back.
+ await expect(service.getValidAccessToken()).rejects.toThrow(
+ NotAuthenticatedError,
+ );
+ expect(oauthFlow.refreshToken).not.toHaveBeenCalled();
+ expect(service.getState().needsScopeReauth).toBe(true);
+ });
+
it.each(["us", "dev", "dev-cloud"] as const)(
"restores an authenticated stored %s session",
async (cloudRegion) => {
diff --git a/products/desktop/packages/core/src/auth/auth.ts b/products/desktop/packages/core/src/auth/auth.ts
index 83ee9670d541..d12af94e5793 100644
--- a/products/desktop/packages/core/src/auth/auth.ts
+++ b/products/desktop/packages/core/src/auth/auth.ts
@@ -1514,6 +1514,13 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {
private async resolveStoredSession(): Promise<StoredSessionInput | null> {
const stored = this.authSession.getCurrent();
if (!stored) return null;
+ // A stale scope version means the stored refresh token was granted under
+ // permissions the app no longer requests. Refusing it here, at the one
+ // place every session-refresh path resolves the stored token, stops a
+ // caller that skips the explicit reauth checks (doInitialize,
+ // attemptSessionRecovery) from resurrecting the old-scope session behind
+ // the reauth prompt's back.
+ if (stored.scopeVersion < OAUTH_SCOPE_VERSION) return null;
const refreshToken = await this.cipher.decrypt(
stored.refreshTokenEncrypted,