Skip to content

An open-source, faster Devin alternative

TypeScript

SDK: never follow a redirect with the API key

Igor Zalutski16b6796c22e3
diff --git a/docs/reference/typescript-sdk/managed-agents.mdx b/docs/reference/typescript-sdk/managed-agents.mdx
index 15119d46..75e7275f 100644
--- a/docs/reference/typescript-sdk/managed-agents.mdx
+++ b/docs/reference/typescript-sdk/managed-agents.mdx
@@ -65,6 +65,11 @@ try {
The client does not retry. The API's idempotency keys make a retry safe, and
the caller decides which calls to repeat and with which key.
+The client does not follow redirects. The API key is sent to `baseUrl` and
+to no other origin: a redirect answer fails the call with code `redirected`
+and the redirect's status, and nothing is sent to the address it named.
+Check `baseUrl` when you see it.
+
## Sessions
| Method | Route | Returns |
diff --git a/sdks/typescript/src/managed-agents/http.ts b/sdks/typescript/src/managed-agents/http.ts
index dae8bf60..4c6da8b9 100644
--- a/sdks/typescript/src/managed-agents/http.ts
+++ b/sdks/typescript/src/managed-agents/http.ts
@@ -4,8 +4,13 @@
// Workers, Deno and browsers-with-a-proxy. No retries: the API's idempotency
// keys make a caller's retry safe, and the caller knows which calls to
// repeat; a client that retried on its own would hide that decision.
+//
+// No redirects either. The key is sent to the configured origin and nowhere
+// else: fetch runs with `redirect: "manual"`, and a 3xx answer is an error
+// with code `redirected`, because following it would carry the key to
+// whatever origin the response names.
-import { errorFromResponse } from "./errors.js";
+import { errorFromResponse, OpenComputerError } from "./errors.js";
export const DEFAULT_BASE_URL = "https://app.opencomputer.dev/api/managed-agents";
@@ -63,12 +68,20 @@ export class Http {
accept: "application/json",
...options.headers,
};
- const init: RequestInit = { method, headers, signal: options.signal };
+ const init: RequestInit = { method, headers, signal: options.signal, redirect: "manual" };
if (options.body !== undefined) {
headers["content-type"] = "application/json";
init.body = JSON.stringify(options.body);
}
const response = await this.doFetch(this.url(path, options.query), init);
+ if (isRedirect(response)) {
+ throw new OpenComputerError(
+ response.status,
+ "redirected",
+ `${method} ${path} was answered with a redirect (${String(response.status)}); ` +
+ "the client does not follow redirects with the API key. Check baseUrl.",
+ );
+ }
const body = await readJson(response);
if (!response.ok) throw errorFromResponse(response.status, body, response.headers);
return { status: response.status, body: body as T, headers: response.headers };
@@ -80,6 +93,15 @@ export class Http {
}
}
+/**
+ * A redirect as fetch reports it under `redirect: "manual"`: the 3xx answer
+ * itself, or on browsers an opaque response of type `opaqueredirect` whose
+ * status reads 0.
+ */
+function isRedirect(response: Response): boolean {
+ return response.type === "opaqueredirect" || (response.status >= 300 && response.status < 400);
+}
+
async function readJson(response: Response): Promise<unknown> {
if (response.status === 204) return undefined;
const text = await response.text();
diff --git a/sdks/typescript/src/managed-agents/redirect.test.ts b/sdks/typescript/src/managed-agents/redirect.test.ts
new file mode 100644
index 00000000..15a6b4db
--- /dev/null
+++ b/sdks/typescript/src/managed-agents/redirect.test.ts
@@ -0,0 +1,69 @@
+// The API key stays on the origin the client was configured with. A
+// redirect is answered with an error, never followed: following it would
+// hand the key to whatever origin the redirect names. Two real HTTP origins
+// on the loopback interface and the real global fetch, as the review
+// reproduced it; this file is a test and not part of the portable subpath.
+
+import { createServer, type IncomingMessage, type Server } from "node:http";
+import type { AddressInfo } from "node:net";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+
+import { OpenComputer } from "./client.js";
+import { OpenComputerError } from "./errors.js";
+
+interface Seen { method: string; url: string; headers: IncomingMessage["headers"] }
+
+function listen(server: Server): Promise<string> {
+ return new Promise((resolve) => {
+ server.listen(0, "127.0.0.1", () => {
+ const { port } = server.address() as AddressInfo;
+ resolve(`http://127.0.0.1:${String(port)}`);
+ });
+ });
+}
+
+const session = {
+ id: "ses_1", agentId: "worker", deploymentId: "dep_1", status: "idle", source: "api",
+ turns: [], createdAt: "2026-09-15T00:00:00.000Z", updatedAt: "2026-09-15T00:00:00.000Z",
+};
+
+describe("redirects", () => {
+ const elsewhere: Seen[] = [];
+ const configured: Seen[] = [];
+ let origin = "";
+ let other = "";
+ const target = createServer((request, response) => {
+ elsewhere.push({ method: request.method ?? "", url: request.url ?? "", headers: request.headers });
+ response.setHeader("content-type", "application/json");
+ response.end(JSON.stringify(session));
+ });
+ const redirecting = createServer((request, response) => {
+ configured.push({ method: request.method ?? "", url: request.url ?? "", headers: request.headers });
+ response.statusCode = 307;
+ response.setHeader("location", `${other}${request.url ?? ""}`);
+ response.end();
+ });
+
+ beforeAll(async () => {
+ other = await listen(target);
+ origin = await listen(redirecting);
+ });
+ afterAll(async () => {
+ await new Promise((resolve) => redirecting.close(resolve));
+ await new Promise((resolve) => target.close(resolve));
+ });
+
+ it("refuses a redirect instead of carrying the API key to another origin", async () => {
+ const oc = new OpenComputer({ apiKey: "osb_dummy_key", baseUrl: `${origin}/api/managed-agents` });
+ const outcome = await oc.sessions.get("ses_1").catch((cause: unknown) => cause);
+ expect(outcome).toBeInstanceOf(OpenComputerError);
+ expect(outcome).toMatchObject({ code: "redirected", status: 307 });
+ expect((outcome as Error).message).toMatch(/GET \/sessions\/ses_1/);
+ expect((outcome as Error).message).not.toContain(other);
+ // The configured origin saw the request with the key; the redirect's
+ // target saw nothing at all.
+ expect(configured).toHaveLength(1);
+ expect(configured[0]?.headers["x-api-key"]).toBe("osb_dummy_key");
+ expect(elsewhere).toEqual([]);
+ });
+});