: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(desktop): send auth requests through electron net.fetch (#115869)
posthog[bot]dfc4d963e2de
diff --git a/products/desktop/apps/code/src/main/di/bindings.ts b/products/desktop/apps/code/src/main/di/bindings.tsindex 2489bfb14c53..bd63e1d5a9e8 100644--- a/products/desktop/apps/code/src/main/di/bindings.ts+++ b/products/desktop/apps/code/src/main/di/bindings.ts@@ -1,7 +1,8 @@-import type { AuthService } from "@posthog/core/auth/auth";+import type { AuthService, FetchLike } from "@posthog/core/auth/auth";import type { AUTH_SERVICE } from "@posthog/core/auth/auth.module";import type {AUTH_CONNECTIVITY,+ AUTH_FETCH,AUTH_FETCH_EXTRA_ORIGINS,AUTH_OAUTH_FLOW_SERVICE,AUTH_PREFERENCE_STORE,@@ -400,6 +401,7 @@ export interface MainBindings {[AUTH_TOKEN_CIPHER]: TokenCipherPortAdapter;[AUTH_CONNECTIVITY]: ConnectivityService;[AUTH_TOKEN_OVERRIDE]: string | null;+ [AUTH_FETCH]: FetchLike;[AUTH_FETCH_EXTRA_ORIGINS]: readonly string[];[MAIN_AUTH_SERVICE]: AuthService;[AUTH_SERVICE]: AuthService;diff --git a/products/desktop/apps/code/src/main/di/container.ts b/products/desktop/apps/code/src/main/di/container.tsindex 06c823fa04c7..fae23708d91f 100644--- a/products/desktop/apps/code/src/main/di/container.ts+++ b/products/desktop/apps/code/src/main/di/container.ts@@ -15,6 +15,7 @@ import { AuthService } from "@posthog/core/auth/auth";import { AUTH_SERVICE } from "@posthog/core/auth/auth.module";import {AUTH_CONNECTIVITY,+ AUTH_FETCH,AUTH_FETCH_EXTRA_ORIGINS,AUTH_OAUTH_FLOW_SERVICE,AUTH_PREFERENCE_STORE,@@ -264,6 +265,7 @@ import { ElectronFileIcon } from "../platform-adapters/electron-file-icon";import { ElectronImageProcessor } from "../platform-adapters/electron-image-processor";import { ElectronMainWindow } from "../platform-adapters/electron-main-window";import { MissionControlService } from "../platform-adapters/electron-mission-control";+import { electronNetFetch } from "../platform-adapters/electron-net-fetch";import { ElectronNotifier } from "../platform-adapters/electron-notifier";import { ElectronPowerManager } from "../platform-adapters/electron-power-manager";import { ElectronScreenCapture } from "../platform-adapters/electron-screen-capture";@@ -440,6 +442,7 @@ container(value): value is string => Boolean(value),),);+container.bind(AUTH_FETCH).toConstantValue(electronNetFetch);container.bind(MAIN_AUTH_SERVICE).to(AuthService);container.bind(AUTH_SERVICE).toService(MAIN_AUTH_SERVICE);container.load(feedbackCoreModule);diff --git a/products/desktop/packages/core/src/auth/auth.test.ts b/products/desktop/packages/core/src/auth/auth.test.tsindex d899e87c53f6..c8d3f2940b7f 100644--- a/products/desktop/packages/core/src/auth/auth.test.ts+++ b/products/desktop/packages/core/src/auth/auth.test.ts@@ -6,7 +6,7 @@ import {OAUTH_SCOPE_VERSION,} from "@posthog/shared";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { AuthService } from "./auth";+import { AuthService, type FetchLike } from "./auth";import type {AuthOrgProjectPreferenceRecord,AuthPreferenceRecord,@@ -220,7 +220,10 @@ describe("AuthService", () => {);};- function createService(extraFetchOrigins: string[] = []): AuthService {+ function createService(+ extraFetchOrigins: string[] = [],+ fetchImpl?: FetchLike,+ ): AuthService {return new AuthService(preferencePort,sessionPort,@@ -231,6 +234,7 @@ describe("AuthService", () => {mockLogger,null,extraFetchOrigins,+ fetchImpl,);}@@ -2453,6 +2457,25 @@ describe("AuthService", () => {});});+ it("checks access through the injected fetch when the global fetch fails", async () => {+ stubAuthFetch();+ const injectedFetch = globalThis.fetch;+ vi.stubGlobal(+ "fetch",+ vi.fn().mockRejectedValue(new TypeError("fetch failed")),+ );+ service = createService([], injectedFetch);++ await service.initialize();++ expect(service.getState().desktopAccess).toEqual({+ projectId: 42,+ status: "allowed",+ reason: null,+ });+ expect(globalThis.fetch).not.toHaveBeenCalled();+ });+it("retries after a technical failure", async () => {let available = false;stubAuthFetch({diff --git a/products/desktop/packages/core/src/auth/auth.ts b/products/desktop/packages/core/src/auth/auth.tsindex 755e08e8d02e..f56d1581f4e1 100644--- a/products/desktop/packages/core/src/auth/auth.ts+++ b/products/desktop/packages/core/src/auth/auth.ts@@ -25,6 +25,7 @@ import {import { z } from "zod";import {AUTH_CONNECTIVITY,+ AUTH_FETCH,AUTH_FETCH_EXTRA_ORIGINS,AUTH_OAUTH_FLOW_SERVICE,AUTH_PREFERENCE_STORE,@@ -142,6 +143,9 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {@inject(AUTH_FETCH_EXTRA_ORIGINS)@optional()private readonly extraFetchOrigins: readonly string[] | undefined = [],+ @inject(AUTH_FETCH)+ @optional()+ private readonly fetchImpl: FetchLike = (input, init) => fetch(input, init),) {super();}@@ -187,7 +191,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {return JSON.stringify([apiHost, session.accountKey]);}const response = await this.authenticatedFetch(- fetch,+ this.fetchImpl,`${apiHost}/api/users/@me/`,{redirect: "error",@@ -518,7 +522,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {private async patchCurrentOrganization(orgId: string): Promise<void> {const { apiHost } = await this.getValidAccessToken();const response = await this.authenticatedFetch(- fetch,+ this.fetchImpl,`${apiHost}/api/users/@me/`,{method: "PATCH",@@ -1017,7 +1021,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {teamIds.map(async (teamId) => {try {const res = await this.executeAuthenticatedFetch(- fetch,+ this.fetchImpl,`${apiHost}/api/projects/${teamId}/`,{},accessToken,@@ -1125,7 +1129,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {const apiHost = getCloudUrlFromRegion(cloudRegion);try {const res = await this.executeAuthenticatedFetch(- fetch,+ this.fetchImpl,`${apiHost}/api/organizations/${orgId}/`,{},accessToken,@@ -1297,7 +1301,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {}> {try {const response = await this.executeAuthenticatedFetch(- fetch,+ this.fetchImpl,`${getCloudUrlFromRegion(cloudRegion)}/api/users/@me/`,{},accessToken,@@ -1454,7 +1458,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {try {const response = await this.executeAuthenticatedFetch(- fetch,+ this.fetchImpl,url,{},session.accessToken,diff --git a/products/desktop/packages/core/src/auth/identifiers.ts b/products/desktop/packages/core/src/auth/identifiers.tsindex 540305f6cb2e..3e3aff635b31 100644--- a/products/desktop/packages/core/src/auth/identifiers.ts+++ b/products/desktop/packages/core/src/auth/identifiers.ts@@ -123,6 +123,13 @@ export const AUTH_TOKEN_OVERRIDE = Symbol.for("posthog.core.auth.tokenOverride",);+/**+ * Fetch for the auth service's own PostHog requests. Desktop binds Electron's+ * `net.fetch` so these requests use system proxies and VPN routing like the+ * browser does. Defaults to the global fetch when unbound.+ */+export const AUTH_FETCH = Symbol.for("posthog.core.auth.fetch");+export const AUTH_FETCH_EXTRA_ORIGINS = Symbol.for("posthog.core.auth.fetchExtraOrigins",);