π OpenHands: AI-Driven Development
fix: use backend registry apiKey for automation auth instead of build-time env var (#830)
Rohit Malhotra5f5311272fdc
diff --git a/__tests__/api/automation-service.test.ts b/__tests__/api/automation-service.test.tsindex a7e89820b4..57ccbf1878 100644--- a/__tests__/api/automation-service.test.ts+++ b/__tests__/api/automation-service.test.ts@@ -7,6 +7,7 @@ import type {AutomationRunsResponse,} from "#/types/automation";import type { Backend } from "#/api/backend-registry/types";+import type { InternalAxiosRequestConfig } from "axios";// Use vi.hoisted to define mocks that will be available during vi.mock hoistingconst {@@ -16,14 +17,21 @@ const {mockDelete,mockCallCloudProxy,mockGetActive,-} = vi.hoisted(() => ({- mockGet: vi.fn(),- mockPatch: vi.fn(),- mockPost: vi.fn(),- mockDelete: vi.fn(),- mockCallCloudProxy: vi.fn(),- mockGetActive: vi.fn(),-}));+ mockGetEffectiveLocal,+ capturedInterceptors,+} = vi.hoisted(() => {+ const interceptors: Array<(config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig> = [];+ return {+ mockGet: vi.fn(),+ mockPatch: vi.fn(),+ mockPost: vi.fn(),+ mockDelete: vi.fn(),+ mockCallCloudProxy: vi.fn(),+ mockGetActive: vi.fn(),+ mockGetEffectiveLocal: vi.fn(),+ capturedInterceptors: interceptors,+ };+});vi.mock("axios", () => ({default: {@@ -35,7 +43,9 @@ vi.mock("axios", () => ({delete: mockDelete,interceptors: {request: {- use: vi.fn(),+ use: (fn: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig) => {+ capturedInterceptors.push(fn);+ },},},}),@@ -48,6 +58,7 @@ vi.mock("#/api/cloud/proxy", () => ({vi.mock("#/api/backend-registry/active-store", () => ({getActiveBackend: mockGetActive,+ getEffectiveLocalBackend: mockGetEffectiveLocal,}));// Import after mocking@@ -69,6 +80,20 @@ const cloudBackend: Backend = {kind: "cloud",};+/** Build a minimal InternalAxiosRequestConfig for interceptor tests. */+function makeAxiosConfig(+ overrides: Partial<InternalAxiosRequestConfig> = {},+): InternalAxiosRequestConfig {+ const headers = {+ set: vi.fn(),+ get: vi.fn(),+ } as unknown as InternalAxiosRequestConfig["headers"];+ return {+ headers,+ ...overrides,+ } as unknown as InternalAxiosRequestConfig;+}+const mockAutomation: Automation = {id: "1",name: "Test Automation",@@ -106,6 +131,8 @@ describe("AutomationService", () => {// Default: active backend is local. Cloud-routing tests override this.mockGetActive.mockReset();mockGetActive.mockReturnValue({ backend: localBackend, orgId: null });+ mockGetEffectiveLocal.mockReset();+ mockGetEffectiveLocal.mockReturnValue(localBackend);});describe("listAutomations", () => {@@ -418,4 +445,75 @@ describe("AutomationService", () => {expect(result).toEqual(run);});});++ // The interceptor must read the session API key from the active backend+ // registry rather than the build-time VITE_SESSION_API_KEY env var so that+ // the published npm package picks up the runtime-injected key (issue #829).+ describe("localAutomationAxios interceptor", () => {+ it("sets X-Session-API-Key from the effective local backend apiKey", () => {+ const interceptor = capturedInterceptors[0];+ expect(interceptor).toBeDefined();++ const backendWithKey: Backend = {+ ...localBackend,+ apiKey: "runtime-injected-key",+ };+ mockGetEffectiveLocal.mockReturnValue(backendWithKey);++ const config = makeAxiosConfig();+ interceptor(config);++ expect(config.headers.set).toHaveBeenCalledWith(+ "X-Session-API-Key",+ "runtime-injected-key",+ );+ });++ it("does not set X-Session-API-Key when backend apiKey is empty", () => {+ const interceptor = capturedInterceptors[0];+ expect(interceptor).toBeDefined();++ mockGetEffectiveLocal.mockReturnValue({+ ...localBackend,+ apiKey: "",+ });++ const config = makeAxiosConfig();+ interceptor(config);++ expect(config.headers.set).not.toHaveBeenCalled();+ });++ it("sets baseURL from effective local backend host when not already set", () => {+ const interceptor = capturedInterceptors[0];+ expect(interceptor).toBeDefined();++ mockGetEffectiveLocal.mockReturnValue({+ ...localBackend,+ host: "http://custom-host:9000",+ apiKey: "key",+ });++ const config = makeAxiosConfig();+ interceptor(config);++ expect(config.baseURL).toBe("http://custom-host:9000");+ });++ it("does not overwrite an already-set baseURL", () => {+ const interceptor = capturedInterceptors[0];+ expect(interceptor).toBeDefined();++ mockGetEffectiveLocal.mockReturnValue({+ ...localBackend,+ host: "http://should-not-use:9000",+ apiKey: "key",+ });++ const config = makeAxiosConfig({ baseURL: "http://already-set:8000" });+ interceptor(config);++ expect(config.baseURL).toBe("http://already-set:8000");+ });+ });});diff --git a/src/api/automation-service/automation-service.api.ts b/src/api/automation-service/automation-service.api.tsindex b9b7cf51ee..282cd19a2d 100644--- a/src/api/automation-service/automation-service.api.ts+++ b/src/api/automation-service/automation-service.api.ts@@ -20,19 +20,24 @@ export interface AutomationHealthResponse {// Local automation calls go to the automation sidecar that// `scripts/dev-with-automation.mjs` mounts behind the local agent-server.-// Both backends use the same session API key (`VITE_SESSION_API_KEY`)-// and the same `X-Session-API-Key` header for consistency.+// Both backends use the same session API key and the same `X-Session-API-Key`+// header for consistency.const localAutomationAxios = axios.create();localAutomationAxios.interceptors.request.use((config) => {- // Resolve the local backend host on every call so it tracks the- // currently-active local backend (and any host edits made via the+ // Resolve the local backend on every call so it tracks the+ // currently-active local backend (and any host/key edits made via the// manage-backends UI), rather than freezing whatever value the// agent-server-config produced at module load time.+ // Using the backend registry (rather than the build-time VITE_SESSION_API_KEY+ // env var) ensures the published npm package picks up the runtime-injected+ // session key that scripts/static-server.mjs seeds into localStorage, fixing+ // the 401 errors reported in issue #829.+ const backend = getEffectiveLocalBackend();// eslint-disable-next-line no-param-reassign- if (!config.baseURL) config.baseURL = getEffectiveLocalBackend().host;+ if (!config.baseURL) config.baseURL = backend.host;- const apiKey = import.meta.env.VITE_SESSION_API_KEY?.trim();+ const apiKey = backend.apiKey?.trim();if (apiKey) {config.headers.set("X-Session-API-Key", apiKey);}