Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: accept Composio upload links on Cloudflare R2
MaxWang00763bd6356cfe8
diff --git a/cloudflare/composio-broker/src/files.test.ts b/cloudflare/composio-broker/src/files.test.tsindex 0ad17c6101..19fa7b6ec7 100644--- a/cloudflare/composio-broker/src/files.test.ts+++ b/cloudflare/composio-broker/src/files.test.ts@@ -75,12 +75,27 @@ describe("file uploads for connected-app tools", () => {const refused = await worker.fetch(post(upload), env() as never, ctx as never);expect(refused.status).toBe(502);await expect(refused.json()).resolves.toEqual({ error: "Toolkit not found" });- for (const link of ["http://storage.composio.dev/put", "https://169.254.169.254/latest", "https://attacker.example/put", "https://storage.composio.dev.attacker.example/put"]) {+ const r2 = `temp.${"0123456789abcdef".repeat(2)}.r2.cloudflarestorage.com`;+ for (const link of [+ "http://storage.composio.dev/put",+ "https://169.254.169.254/latest",+ "https://attacker.example/put",+ "https://storage.composio.dev.attacker.example/put",+ `http://${r2}/put`,+ `https://user:pass@${r2}/put`,+ `https://${r2}:444/put`,+ `https://${r2}.attacker.example/put`,+ ]) {vi.stubGlobal("fetch", composio({ key: "k", new_presigned_url: link }));expect((await worker.fetch(post(upload), env() as never, ctx as never)).status, link).toBe(502);}vi.stubGlobal("fetch", composio({ key: "k", new_presigned_url: "https://composio-files.s3.us-east-1.amazonaws.com/k?X-Amz-Signature=1" }));expect((await worker.fetch(post(upload), env() as never, ctx as never)).status).toBe(200);+ const r2Link = `https://${r2}/k?X-Amz-Signature=1`;+ vi.stubGlobal("fetch", composio({ key: "k", new_presigned_url: r2Link }));+ const uploaded = await worker.fetch(post(upload), env() as never, ctx as never);+ expect(uploaded.status).toBe(200);+ await expect(uploaded.json()).resolves.toEqual({ key: "k", url: r2Link });});it("hands out no link for a toolkit the installation has no working connection to", async () => {diff --git a/docs/composio.md b/docs/composio.mdindex 3e36d6e2fe..51e85a1110 100644--- a/docs/composio.md+++ b/docs/composio.md@@ -126,7 +126,7 @@ Composio runs app tools on its own servers, so a tool that takes a file (Google- **Which files.** Only a file in the bot's working folder or Local VM folder, or one attached to this conversation (by its exact path: never the attachment store as a whole, which holds every conversation's and every team member's files). Never a file in a hidden folder (`.ssh`, `.aws`, `.config`, any dotfile) below the bot's folder or the home folder, nor in the app's own data (`DATA_DIR`, the desktop app's data folder) unless the bot's own folder is inside it, as its workspace is. Such a file is refused with the reason and the call does not go out.- **Anything else goes as it is.** A value that is not an existing file in those folders (a Dropbox path such as `/Apps/report.pdf`, `/images/logo.png`, text that starts with `/`, anything with a line break) is sent unchanged, with no refusal.- **When.** The files are read before the call's checks and listed in full (name, path, size) on a "Send on your behalf?" card, ahead of the arguments it cuts at 400 characters. They are uploaded only after the last check before sending, so a call refused there, or whose permission or turn ended meanwhile, uploads nothing.-- **Where.** The harness asks for an upload link (`POST /v1/files/upload` on the managed service, which gives one only for a toolkit the installation has a working connection to; Composio's `POST /files/upload/request` with your own project key). It puts the bytes only to an HTTPS link on Composio's storage (`*.composio.dev` or AWS S3; with `OMB_COMPOSIO_API`, that backend's origin), never following a redirect. The call then carries `{ name, mimetype, s3key }`.+- **Where.** The harness asks for an upload link (`POST /v1/files/upload` on the managed service, which gives one only for a toolkit the installation has a working connection to; Composio's `POST /files/upload/request` with your own project key). It puts the bytes only to an HTTPS link on Composio's storage (`*.composio.dev`, AWS S3, or Cloudflare R2; with `OMB_COMPOSIO_API`, that backend's origin), never following a redirect. The call then carries `{ name, mimetype, s3key }`.- **Failures.** A failed upload is refused with what failed, and nothing is written to the app. When Composio still cannot fetch a call's file (`Failed to download file with s3key …`), the bot is told to give a local path instead of an invented key. At most 10 files per call. Plain text needs no file: `GOOGLEDRIVE_CREATE_FILE_FROM_TEXT` takes the text itself.The managed service needs the updated broker as well as the updated app for this.diff --git a/server/composio-files.test.ts b/server/composio-files.test.tsindex 8428db4d6a..64b4d6a15f 100644--- a/server/composio-files.test.ts+++ b/server/composio-files.test.ts@@ -22,14 +22,36 @@ describe("upload links for connected-app files", () => {expect(calls[0]!.url).toBe("https://backend.composio.dev/api/v3.1/files/upload/request");expect(new Headers(calls[0]!.init?.headers).get("x-api-key")).toBe("ak_test");vi.stubGlobal("fetch", answering({ key: "k", url: "https://bucket.s3.us-east-1.amazonaws.com/k?X-Amz-Signature=1" }, calls));- await expect(requestFileUpload(withKey, request, service)).resolves.toMatchObject({ key: "k" });+ await expect(requestFileUpload(withKey, request, service)).resolves.toMatchObject({ key: "k", url: "https://bucket.s3.us-east-1.amazonaws.com/k?X-Amz-Signature=1" });expect(calls[1]!.url).toBe("https://connect.example/v1/files/upload");expect(new Headers(calls[1]!.init?.headers).get("authorization")).toBe(`Bearer ${service.token}`);});+ it("accepts a presigned upload link on Composio's Cloudflare R2 storage", async () => {+ vi.stubEnv("OMB_COMPOSIO_API", undefined);+ const link = `https://temp.${"0123456789abcdef".repeat(2)}.r2.cloudflarestorage.com/517680/googledrive/report.pdf?X-Amz-Signature=1`;+ vi.stubGlobal("fetch", answering({ key: "projects/p/requests/googledrive/report.pdf", new_presigned_url: link }));+ await expect(requestFileUpload(withKey, request)).resolves.toEqual({ key: "projects/p/requests/googledrive/report.pdf", url: link });+ vi.stubGlobal("fetch", answering({ key: "k", url: link }));+ await expect(requestFileUpload(withKey, request, service)).resolves.toEqual({ key: "k", url: link });+ });+it("refuses a link anywhere but Composio's storage over HTTPS", async () => {vi.stubEnv("OMB_COMPOSIO_API", undefined);- for (const link of ["https://attacker.example/put", "http://storage.composio.dev/put", "http://127.0.0.1:8787/put", "https://169.254.169.254/x", "https://storage.composio.dev.evil.example/x"]) {+ const account = "0123456789abcdef".repeat(2);+ const r2 = `temp.${account}.r2.cloudflarestorage.com`;+ for (const link of [+ "https://attacker.example/put",+ "http://storage.composio.dev/put",+ "http://127.0.0.1:8787/put",+ "https://169.254.169.254/x",+ "https://storage.composio.dev.evil.example/x",+ `http://${r2}/put`,+ `https://user:pass@${r2}/put`,+ `https://${r2}:444/put`,+ `https://${r2}.attacker.example/x`,+ `https://r2.cloudflarestorage.com/put`,+ ]) {vi.stubGlobal("fetch", answering({ key: "k", new_presigned_url: link }));await expect(requestFileUpload(withKey, request), link).rejects.toThrow("not on its storage");vi.stubGlobal("fetch", answering({ key: "k", url: link }));diff --git a/shared/composio-storage.test.ts b/shared/composio-storage.test.tsnew file mode 100644index 0000000000..d721836a14--- /dev/null+++ b/shared/composio-storage.test.ts@@ -0,0 +1,72 @@+import { describe, expect, it } from "vitest";++import { composioStorageHost, composioUploadLink } from "./composio-storage.ts";++const account = "0123456789abcdef".repeat(2);+const r2 = `temp.${account}.r2.cloudflarestorage.com`;++describe("composioStorageHost", () => {+ it("allows Composio, S3, and Cloudflare R2 storage hosts", () => {+ for (const host of [+ "composio.dev",+ "storage.composio.dev",+ "bucket.s3.us-east-1.amazonaws.com",+ "composio-files.s3.amazonaws.com",+ r2,+ `${account}.r2.cloudflarestorage.com`,+ `temp.${account}.eu.r2.cloudflarestorage.com`,+ `temp.${account}.us.r2.cloudflarestorage.com`,+ `${account}.fedramp.r2.cloudflarestorage.com`,+ r2.toUpperCase(),+ ]) {+ expect(composioStorageHost(host), host).toBe(true);+ }+ });++ it("rejects other hosts, including suffix lookalikes", () => {+ for (const host of [+ "attacker.example",+ "storage.composio.dev.evil.example",+ "not-r2.cloudflarestorage.com",+ "r2.cloudflarestorage.com",+ "www.r2.cloudflarestorage.com",+ `temp.not-an-account.r2.cloudflarestorage.com`,+ `evil.temp.${account}.r2.cloudflarestorage.com`,+ `${r2}.attacker.example`,+ `temp.${account}.zz.r2.cloudflarestorage.com`,+ `ab.${account}.r2.cloudflarestorage.com`,+ `-temp.${account}.r2.cloudflarestorage.com`,+ `temp-.${account}.r2.cloudflarestorage.com`,+ "169.254.169.254",+ "127.0.0.1",+ ]) {+ expect(composioStorageHost(host), host).toBe(false);+ }+ });+});++describe("composioUploadLink", () => {+ it("returns an HTTPS link on R2, composio.dev, and S3", () => {+ const link = `https://${r2}/517680/googledrive/report.pdf?X-Amz-Signature=1`;+ expect(composioUploadLink(link)?.href).toBe(link);+ expect(composioUploadLink("https://storage.composio.dev/p/report.pdf?X-Amz-Signature=1")?.hostname).toBe("storage.composio.dev");+ expect(composioUploadLink("https://bucket.s3.us-east-1.amazonaws.com/k?X-Amz-Signature=1")?.hostname).toBe("bucket.s3.us-east-1.amazonaws.com");+ });++ it("rejects HTTP, userinfo, a non-default port, and a host that only looks like R2", () => {+ for (const link of [+ undefined,+ "",+ "not a url",+ `http://${r2}/put`,+ `https://user:pass@${r2}/put`,+ `https://${r2}:444/put`,+ `https://${r2}.attacker.example/x`,+ "https://storage.composio.dev.evil.example/x",+ "https://attacker.example/put",+ "http://127.0.0.1:8787/put",+ ]) {+ expect(composioUploadLink(link), String(link)).toBeNull();+ }+ });+});diff --git a/shared/composio-storage.ts b/shared/composio-storage.tsindex 2e8d84ffda..80bdd84c33 100644--- a/shared/composio-storage.ts+++ b/shared/composio-storage.ts@@ -1,11 +1,15 @@-// The hosts Composio hands out file upload links on: its own storage (storage.composio.dev in its API reference) and the-// AWS S3 storage behind it (its upload answer names `storage_backend: "s3"`). A file's bytes are put only to one of-// these, over HTTPS, so a link pointing anywhere else (this computer, a private address, another site) is never used.+// The hosts Composio hands out file upload links on: its own storage (storage.composio.dev in its API reference), the+// AWS S3 storage behind it (its upload answer names `storage_backend: "s3"`), and Cloudflare R2's S3 API. R2 presigns+// to `<bucket>.<account-id>.r2.cloudflarestorage.com` (Composio's temporary bucket is `temp`) or the account endpoint+// itself. The account id is 32 hex digits; an optional jurisdiction (`eu`, `us`, `fedramp`) sits in front of `r2`.+// A file's bytes are put only to one of these, over HTTPS, so a link pointing anywhere else (this computer, a private+// address, another site) is never used.const S3_HOST = /^(?:[a-z0-9][a-z0-9.-]*\.)?s3(?:[.-][a-z0-9-]+)*\.amazonaws\.com$/;+const R2_HOST = /^(?:[a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])\.)?[a-f0-9]{32}(?:\.(?:eu|us|fedramp))?\.r2\.cloudflarestorage\.com$/;export function composioStorageHost(hostname: string): boolean {const host = hostname.toLowerCase();- return host === "composio.dev" || host.endsWith(".composio.dev") || S3_HOST.test(host);+ return host === "composio.dev" || host.endsWith(".composio.dev") || S3_HOST.test(host) || R2_HOST.test(host);}/** The link as a URL when it is HTTPS on Composio's storage, else null. */