π OpenHands: AI-Driven Development
feat: track authenticated Canvas arrivals (#17083)
Graham Neubig60877198aa4b
diff --git a/.github/pr-assets/pr-17083-test-results.svg b/.github/pr-assets/pr-17083-test-results.svgnew file mode 100644index 0000000000..3a39cfb51d--- /dev/null+++ b/.github/pr-assets/pr-17083-test-results.svg@@ -0,0 +1,17 @@+<svg xmlns="http://www.w3.org/2000/svg" width="720" height="260" viewBox="0 0 720 260" font-family="monospace">+ <rect width="720" height="260" fill="#1e1e1e" rx="8"/>+ <rect x="0" y="0" width="720" height="32" fill="#2d2d2d" rx="8"/>+ <circle cx="16" cy="16" r="6" fill="#ff5f56"/>+ <circle cx="36" cy="16" r="6" fill="#ffbd2e"/>+ <circle cx="56" cy="16" r="6" fill="#27c93f"/>+ <text x="360" y="21" fill="#888" font-size="12" text-anchor="middle">Terminal β vitest run __tests__/api/main-app-auth.test.ts</text>+ <text x="16" y="56" fill="#d4d4d4" font-size="13" xml:space="preserve">RUN v4.1.10</text>+ <text x="16" y="80" fill="#888" font-size="13" xml:space="preserve"> </text>+ <text x="16" y="104" fill="#d4d4d4" font-size="13" xml:space="preserve"> β main app cookie authentication</text>+ <text x="16" y="128" fill="#d4d4d4" font-size="13" xml:space="preserve"> β reports the Canvas version after authentication succeeds</text>+ <text x="16" y="152" fill="#d4d4d4" font-size="13" xml:space="preserve"> β does not report unauthenticated arrivals</text>+ <text x="16" y="176" fill="#d4d4d4" font-size="13" xml:space="preserve"> β isolates analytics failures from application startup</text>+ <text x="16" y="200" fill="#d4d4d4" font-size="13" xml:space="preserve"> β fires the analytics event at most once per page load</text>+ <text x="16" y="228" fill="#27c93f" font-size="13" xml:space="preserve"> Test Files 1 passed (1)</text>+ <text x="16" y="248" fill="#27c93f" font-size="13" xml:space="preserve"> Tests 4 passed (4)</text>+</svg>diff --git a/__tests__/api/main-app-auth.test.ts b/__tests__/api/main-app-auth.test.tsnew file mode 100644index 0000000000..6705872d0f--- /dev/null+++ b/__tests__/api/main-app-auth.test.ts@@ -0,0 +1,80 @@+import axios from "axios";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { AGENT_CANVAS_CLIENT_VERSION } from "#/api/client-source";+import {+ _resetCanvasAuthReported,+ authenticateWithMainAppCookie,+ MAIN_APP_ANALYTICS_EVENTS_PATH,+ MAIN_APP_AUTHENTICATE_PATH,+ reportCanvasAuthenticated,+} from "#/api/main-app-auth";++vi.mock("axios", () => ({+ default: {+ post: vi.fn(),+ isAxiosError: vi.fn(),+ },+}));++const postMock = vi.mocked(axios.post);+const isAxiosErrorMock = vi.mocked(axios.isAxiosError);++describe("main app cookie authentication", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ _resetCanvasAuthReported();+ });++ it("reports the Canvas version after authentication succeeds", async () => {+ postMock.mockResolvedValue({});++ await expect(authenticateWithMainAppCookie()).resolves.toBe(true);+ await vi.waitFor(() => expect(postMock).toHaveBeenCalledTimes(2));++ expect(postMock).toHaveBeenNthCalledWith(+ 1,+ MAIN_APP_AUTHENTICATE_PATH,+ null,+ { withCredentials: true },+ );+ expect(postMock).toHaveBeenNthCalledWith(+ 2,+ MAIN_APP_ANALYTICS_EVENTS_PATH,+ {+ event_type: "canvas_authenticated",+ client_version: AGENT_CANVAS_CLIENT_VERSION,+ },+ { withCredentials: true },+ );+ });++ it("does not report unauthenticated arrivals", async () => {+ const error = { response: { status: 401 } };+ postMock.mockRejectedValue(error);+ isAxiosErrorMock.mockReturnValue(true);++ await expect(authenticateWithMainAppCookie()).resolves.toBe(false);++ expect(postMock).toHaveBeenCalledOnce();+ expect(postMock).toHaveBeenCalledWith(+ MAIN_APP_AUTHENTICATE_PATH,+ null,+ { withCredentials: true },+ );+ });++ it("isolates analytics failures from application startup", async () => {+ postMock.mockRejectedValue(new Error("analytics unavailable"));++ await expect(reportCanvasAuthenticated()).resolves.toBeUndefined();+ });++ it("fires the analytics event at most once per page load", async () => {+ postMock.mockResolvedValue({});++ await reportCanvasAuthenticated();+ await reportCanvasAuthenticated();++ expect(postMock).toHaveBeenCalledOnce();+ });+});diff --git a/src/api/main-app-auth.ts b/src/api/main-app-auth.tsindex e01ac34706..dcbb3de9f3 100644--- a/src/api/main-app-auth.ts+++ b/src/api/main-app-auth.ts@@ -1,7 +1,9 @@import axios from "axios";+import { AGENT_CANVAS_CLIENT_VERSION } from "./client-source";import { getLockedCloudAuthMode } from "./agent-server-config";export const MAIN_APP_AUTHENTICATE_PATH = "/api/authenticate";+export const MAIN_APP_ANALYTICS_EVENTS_PATH = "/api/analytics/events";export const MAIN_APP_LOGIN_PATH = "/login";export const MAIN_APP_LOGIN_REDIRECT_PARAM = "returnTo";@@ -21,11 +23,35 @@ export function shouldUseMainAppCookieAuth(): boolean {return getLockedCloudAuthMode() === "cookie" && !isLocalBrowserHost();}+let canvasAuthenticatedReported = false;++export function _resetCanvasAuthReported(): void {+ canvasAuthenticatedReported = false;+}++export async function reportCanvasAuthenticated(): Promise<void> {+ if (canvasAuthenticatedReported) return;+ canvasAuthenticatedReported = true;+ try {+ await axios.post(+ MAIN_APP_ANALYTICS_EVENTS_PATH,+ {+ event_type: "canvas_authenticated",+ client_version: AGENT_CANVAS_CLIENT_VERSION,+ },+ { withCredentials: true },+ );+ } catch {+ // Analytics must never affect authentication or application startup.+ }+}+export async function authenticateWithMainAppCookie(): Promise<boolean> {try {await axios.post(MAIN_APP_AUTHENTICATE_PATH, null, {withCredentials: true,});+ void reportCanvasAuthenticated();return true;} catch (error) {if (axios.isAxiosError(error) && error.response?.status === 401) {