An open-source, faster Devin alternative
TypeScript
fix: use Workers-compatible CLI auth redirects
Igor Zalutski31d3970077e3
diff --git a/cloudflare-workers/api-edge/src/cli_auth.test.ts b/cloudflare-workers/api-edge/src/cli_auth.test.tsindex 3b1f6849..909d3785 100644--- a/cloudflare-workers/api-edge/src/cli_auth.test.ts+++ b/cloudflare-workers/api-edge/src/cli_auth.test.ts@@ -176,7 +176,24 @@ describe("CLI device authorization edge contract", () => {const [, init] = providerFetch.mock.calls[0];expect(init?.headers).toEqual({ "content-type": "application/x-www-form-urlencoded" });expect(init?.body).toBe("client_id=client_test");- expect(init?.redirect).toBe("error");+ expect(init?.redirect).toBe("manual");+ });++ it("rejects provider redirects without forwarding them to the caller", async () => {+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});+ vi.stubGlobal("fetch", vi.fn(async () => new Response(null, {+ status: 302,+ headers: { location: "https://unexpected.example/device" },+ })));++ const resp = await worker.fetch(request("/auth/cli/device", {+ method: "POST",+ }), testEnv(), ctx);++ expect(resp.status).toBe(503);+ expect(resp.headers.has("location")).toBe(false);+ expect(await resp.json()).toEqual({ error: "auth_provider_unavailable" });+ expect(logSpy.mock.calls.flat().join(" ")).toContain('"provider_status":302');});it("fails closed without exposing a malformed provider response", async () => {@@ -332,6 +349,26 @@ describe("CLI device authorization edge contract", () => {expect(providerFetch.mock.calls[0][1]?.signal?.aborted).toBe(true);});+ it("logs actionable provider exceptions without logging request credentials", async () => {+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});+ vi.stubGlobal("fetch", vi.fn(async () => {+ throw new TypeError("failed for client_test and opaque");+ }));++ const resp = await worker.fetch(request("/auth/cli/device/exchange", {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ device_code: "opaque", credential_name: "oc CLI" }),+ }), testEnv(), ctx);++ expect(resp.status).toBe(503);+ const logs = logSpy.mock.calls.flat().join(" ");+ expect(logs).toContain('"provider_error_name":"TypeError"');+ expect(logs).toContain('"provider_error_message":"failed for [redacted] and [redacted]"');+ expect(logs).not.toContain("client_test");+ expect(logs).not.toContain("opaque");+ });+it.each([["authorization_pending", 202, { status: "authorization_pending", retry_after: 5 }],["slow_down", 202, { status: "authorization_pending", retry_after: 10 }],@@ -400,7 +437,7 @@ describe("CLI device authorization edge contract", () => {expect(providerForm.get("device_code")).toBe("opaque");expect(providerForm.get("client_id")).toBe("client_test");expect(providerForm.has("client_secret")).toBe(false);- expect(providerInit?.redirect).toBe("error");+ expect(providerInit?.redirect).toBe("manual");const logs = logSpy.mock.calls.flat().join(" ");expect(logs).not.toContain("workos-access-secret");expect(logs).not.toContain("workos-refresh-secret");diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.tsindex b9064ccc..a3cc23c2 100644--- a/cloudflare-workers/api-edge/src/index.ts+++ b/cloudflare-workers/api-edge/src/index.ts@@ -1475,13 +1475,22 @@ function cliAuthRateLimitError(): Response {return response;}+interface CLIAuthLogDetails {+ user_id?: string;+ org_id?: string;+ credential_id?: string;+ provider_status?: number;+ provider_error_name?: string;+ provider_error_message?: string;+}+function recordCLIAuth(event: "cli_auth_start" | "cli_auth_exchange" | "cli_auth_revoke",requestID: string,result: string,status: number,startedAt: number,- ids: { user_id?: string; org_id?: string; credential_id?: string } = {},+ details: CLIAuthLogDetails = {},): void {console.log(JSON.stringify({event,@@ -1489,10 +1498,29 @@ function recordCLIAuth(result,http_status: status,duration_ms: Date.now() - startedAt,- ...ids,+ ...details,}));}+function cliAuthProviderExceptionDetails(+ error: unknown,+ redactions: string[],+): Pick<CLIAuthLogDetails, "provider_error_name" | "provider_error_message"> {+ const provider_error_name = error instanceof Error ? error.name : "NonError";+ let provider_error_message =+ error instanceof Error ? error.message : "provider request threw a non-Error value";+ for (const secret of redactions) {+ if (!secret) continue;+ provider_error_message = provider_error_message+ .replaceAll(secret, "[redacted]")+ .replaceAll(encodeURIComponent(secret), "[redacted]");+ }+ provider_error_message = provider_error_message+ .replace(/[\u0000-\u001f\u007f]/g, " ")+ .slice(0, 256);+ return { provider_error_name, provider_error_message };+}+function isHTTPSURL(value: unknown): value is string {if (typeof value !== "string" || value.length === 0 || value.length > 2048) return false;try {@@ -1643,8 +1671,12 @@ function urlContainsOpaqueCode(rawURL: string, code: string): boolean {async function authCLIStart(req: Request, env: Env): Promise<Response> {const requestID = crypto.randomUUID();const startedAt = Date.now();- const finish = (response: Response, result: string): Response => {- recordCLIAuth("cli_auth_start", requestID, result, response.status, startedAt);+ const finish = (+ response: Response,+ result: string,+ details: CLIAuthLogDetails = {},+ ): Response => {+ recordCLIAuth("cli_auth_start", requestID, result, response.status, startedAt, details);return response;};@@ -1674,14 +1706,25 @@ async function authCLIStart(req: Request, env: Env): Promise<Response> {method: "POST",headers: { "content-type": "application/x-www-form-urlencoded" },body: new URLSearchParams({ client_id: env.WORKOS_CLIENT_ID }).toString(),- redirect: "error",+ // Workers rejects RequestRedirect "error" at runtime. "manual" keeps+ // redirects fail-closed: the 3xx response is returned and rejected+ // below, without forwarding the form body to another origin.+ redirect: "manual",},));- } catch {- return finish(cliAuthError("auth_provider_unavailable", 503), "provider_error");+ } catch (error) {+ return finish(+ cliAuthError("auth_provider_unavailable", 503),+ "provider_error",+ cliAuthProviderExceptionDetails(error, [env.WORKOS_CLIENT_ID]),+ );}if (!providerResp.ok) {- return finish(cliAuthError("auth_provider_unavailable", 503), "provider_error");+ return finish(+ cliAuthError("auth_provider_unavailable", 503),+ "provider_error",+ { provider_status: providerResp.status },+ );}if (@@ -1699,7 +1742,11 @@ async function authCLIStart(req: Request, env: Env): Promise<Response> {urlContainsOpaqueCode(bodyJSON.verification_uri, bodyJSON.device_code) ||urlContainsOpaqueCode(bodyJSON.verification_uri_complete, bodyJSON.device_code)) {- return finish(cliAuthError("auth_provider_invalid_response", 502), "provider_error");+ return finish(+ cliAuthError("auth_provider_invalid_response", 502),+ "provider_error",+ { provider_status: providerResp.status, provider_error_name: "InvalidResponse" },+ );}return finish(cliAuthJSON({@@ -1722,9 +1769,9 @@ async function authCLIExchange(req: Request, env: Env): Promise<Response> {const finish = (response: Response,result: string,- ids: { user_id?: string; org_id?: string; credential_id?: string } = {},+ details: CLIAuthLogDetails = {},): Response => {- recordCLIAuth("cli_auth_exchange", requestID, result, response.status, startedAt, ids);+ recordCLIAuth("cli_auth_exchange", requestID, result, response.status, startedAt, details);return response;};@@ -1771,11 +1818,17 @@ async function authCLIExchange(req: Request, env: Env): Promise<Response> {device_code: body.device_code,client_id: env.WORKOS_CLIENT_ID,}).toString(),- redirect: "error",+ // See the start endpoint: manual redirects prevent the device code+ // from being forwarded and are rejected by the status handling below.+ redirect: "manual",},));- } catch {- return finish(cliAuthError("auth_provider_unavailable", 503), "provider_error");+ } catch (error) {+ return finish(+ cliAuthError("auth_provider_unavailable", 503),+ "provider_error",+ cliAuthProviderExceptionDetails(error, [env.WORKOS_CLIENT_ID, body.device_code]),+ );}if (!providerResp.ok) {@@ -1799,9 +1852,17 @@ async function authCLIExchange(req: Request, env: Env): Promise<Response> {return finish(cliAuthError("authorization_expired", 410), "expired");}if (providerResp.status >= 500) {- return finish(cliAuthError("auth_provider_unavailable", 503), "provider_error");+ return finish(+ cliAuthError("auth_provider_unavailable", 503),+ "provider_error",+ { provider_status: providerResp.status },+ );}- return finish(cliAuthError("auth_provider_invalid_response", 502), "provider_error");+ return finish(+ cliAuthError("auth_provider_invalid_response", 502),+ "provider_error",+ { provider_status: providerResp.status },+ );}const profile = providerBody?.user;@@ -1812,7 +1873,11 @@ async function authCLIExchange(req: Request, env: Env): Promise<Response> {typeof (profile as Record<string, unknown>).id !== "string" ||typeof (profile as Record<string, unknown>).email !== "string") {- return finish(cliAuthError("auth_provider_invalid_response", 502), "provider_error");+ return finish(+ cliAuthError("auth_provider_invalid_response", 502),+ "provider_error",+ { provider_status: providerResp.status, provider_error_name: "InvalidResponse" },+ );}const typedProfile = profile as unknown as WorkOSProfile;const workosOrgID =