An open-source, faster Devin alternative
TypeScript
address api edge websocket auth review
Igor Zalutski0ddfd89070a1
diff --git a/cloudflare-workers/api-edge/src/index.test.ts b/cloudflare-workers/api-edge/src/index.test.tsindex 8f5208a1..a33d5650 100644--- a/cloudflare-workers/api-edge/src/index.test.ts+++ b/cloudflare-workers/api-edge/src/index.test.ts@@ -115,4 +115,57 @@ describe("api-edge WebSocket auth", () => {expect(await resp.json()).toEqual({ error: "missing or invalid API key" });expect(fetchSpy).not.toHaveBeenCalled();});++ it("strips api_key query params from proxied HTTP requests authenticated by header", async () => {+ const fetchSpy = vi.fn(async (_url: string, _init?: RequestInit) => new Response("proxied", { status: 200 }));+ vi.stubGlobal("fetch", fetchSpy);++ const resp = await worker.fetch(+ new Request(+ "https://app.opencomputer.dev/api/sandboxes/sb-123/exec?api_key=osb_query&stream=1",+ {+ headers: {+ "X-API-Key": "osb_header",+ },+ },+ ),+ env,+ ctx,+ );++ expect(resp.status).toBe(200);+ expect(fetchSpy).toHaveBeenCalledTimes(1);++ const forwardedURL = new URL(fetchSpy.mock.calls[0][0] as string);+ expect(forwardedURL.origin).toBe("https://cp-us-east-2.opencomputer.dev");+ expect(forwardedURL.pathname).toBe("/api/sandboxes/sb-123/exec");+ expect(forwardedURL.searchParams.get("stream")).toBe("1");+ expect(forwardedURL.searchParams.has("api_key")).toBe(false);++ const forwardedHeaders = new Headers(fetchSpy.mock.calls[0][1]?.headers);+ expect(forwardedHeaders.get("authorization")).toMatch(/^Bearer /);+ expect(forwardedHeaders.get("x-api-key")).toBeNull();+ });++ it("rejects WebSocket proxy requests without header or query auth", async () => {+ const fetchSpy = vi.fn(async (_req: Request) => new Response("proxied", { status: 200 }));+ vi.stubGlobal("fetch", fetchSpy);++ const resp = await worker.fetch(+ new Request(+ "https://app.opencomputer.dev/api/sandboxes/sb-123/exec/es-123",+ {+ headers: {+ Upgrade: "websocket",+ },+ },+ ),+ env,+ ctx,+ );++ expect(resp.status).toBe(401);+ expect(await resp.json()).toEqual({ error: "missing or invalid API key" });+ expect(fetchSpy).not.toHaveBeenCalled();+ });});diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.tsindex b7bb418d..bcc01400 100644--- a/cloudflare-workers/api-edge/src/index.ts+++ b/cloudflare-workers/api-edge/src/index.ts@@ -115,7 +115,7 @@ function apiKeyFromRequest(req: Request): string | null {return null;}-function stripEdgeAuthQueryParam(target: string): string {+function stripApiKeyQueryParam(target: string): string {const url = new URL(target);url.searchParams.delete("api_key");return url.toString();@@ -574,7 +574,7 @@ async function proxyToCellSDK(req: Request, env: Env, ctx: ExecutionContext, calif (!cell) return json({ error: `cell ${row.cell_id} not registered` }, 503);const url = new URL(req.url);- const target = cell.base_url.replace(/\/$/, "") + url.pathname + url.search;+ const target = stripApiKeyQueryParam(cell.base_url.replace(/\/$/, "") + url.pathname + url.search);// Look up the org's plan so the cap-token carries it (worker resolver uses// plan to tag usage_tick events; without it free-tier debit fan-out skips// the org).@@ -637,7 +637,7 @@ async function proxyToCellSDK(req: Request, env: Env, ctx: ExecutionContext, cal// upgrades transparently when you pass a Request clone — same pattern// handlePreviewURL uses and that's verified to work end-to-end with WS.if (isWebSocketUpgrade(req)) {- const fwd = new Request(stripEdgeAuthQueryParam(target), req);+ const fwd = new Request(target, req);fwd.headers.set("authorization", "Bearer " + token);fwd.headers.delete("x-api-key");return await fetch(fwd);