: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): bind dev OAuth and MCP callback servers to loopback (#78082)
Charles Vienc1366f036f37
diff --git a/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.test.ts b/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.test.tsnew file mode 100644index 000000000000..adb523d285ec--- /dev/null+++ b/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.test.ts@@ -0,0 +1,45 @@+import { describe, expect, it, vi } from "vitest";++const listenMock = vi.hoisted(() => vi.fn());++vi.mock("node:http", () => {+ const createServer = vi.fn(() => ({+ listen: listenMock,+ close: vi.fn(),+ on: vi.fn(),+ }));+ return { createServer, default: { createServer } };+});++import { McpCallbackServer } from "./mcp-callback-server";++describe("McpCallbackServer.waitForCallback", () => {+ it("binds the callback server to the loopback interface only", async () => {+ listenMock.mockImplementation(+ (_port: number, _host: string, cb: () => void) => cb(),+ );+ const controller = new AbortController();+ const onListening = vi.fn();++ const promise = new McpCallbackServer().waitForCallback({+ port: 23456,+ path: "/mcp-oauth-complete",+ timeoutMs: 1000,+ signal: controller.signal,+ onListening,+ successWhen: () => true,+ });+ // The flow never resolves in this test; swallow the cancel rejection.+ promise.catch(() => {});++ expect(listenMock).toHaveBeenCalledWith(+ 23456,+ "127.0.0.1",+ expect.any(Function),+ );+ expect(onListening).toHaveBeenCalledTimes(1);++ controller.abort();+ await expect(promise).rejects.toThrow("MCP OAuth flow cancelled");+ });+});diff --git a/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.ts b/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.tsindex 444ec2232ce1..8d5228ffa261 100644--- a/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.ts+++ b/products/desktop/packages/workspace-server/src/services/mcp-callback/mcp-callback-server.ts@@ -95,7 +95,9 @@ export class McpCallbackServer {);});- server.listen(port, () => {+ // Bind to loopback only so the dev callback endpoint is reachable+ // solely from this machine and not from other hosts on the LAN.+ server.listen(port, "127.0.0.1", () => {onListening?.();});});diff --git a/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.test.ts b/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.test.tsnew file mode 100644index 000000000000..07b9f08c73e2--- /dev/null+++ b/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.test.ts@@ -0,0 +1,43 @@+import { describe, expect, it, vi } from "vitest";++const listenMock = vi.hoisted(() => vi.fn());++vi.mock("node:http", () => {+ const createServer = vi.fn(() => ({+ listen: listenMock,+ close: vi.fn(),+ on: vi.fn(),+ }));+ return { createServer, default: { createServer } };+});++import { OAuthCallbackServer } from "./oauth-callback";++describe("OAuthCallbackServer.waitForCode", () => {+ it("binds the callback server to the loopback interface only", async () => {+ listenMock.mockImplementation(+ (_port: number, _host: string, cb: () => void) => cb(),+ );+ const controller = new AbortController();+ const onListening = vi.fn();++ const promise = new OAuthCallbackServer().waitForCode({+ port: 12345,+ timeoutMs: 1000,+ signal: controller.signal,+ onListening,+ });+ // The flow never resolves in this test; swallow the cancel rejection.+ promise.catch(() => {});++ expect(listenMock).toHaveBeenCalledWith(+ 12345,+ "127.0.0.1",+ expect.any(Function),+ );+ expect(onListening).toHaveBeenCalledTimes(1);++ controller.abort();+ await expect(promise).rejects.toThrow("OAuth flow cancelled");+ });+});diff --git a/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.ts b/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.tsindex 502e21c3cff5..3060519f799c 100644--- a/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.ts+++ b/products/desktop/packages/workspace-server/src/services/oauth-callback/oauth-callback.ts@@ -108,7 +108,9 @@ export class OAuthCallbackServer {);});- server.listen(port, () => {+ // Bind to loopback only so the dev callback endpoint is reachable+ // solely from this machine and not from other hosts on the LAN.+ server.listen(port, "127.0.0.1", () => {onListening?.();});});