Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix: validate resource IDs and harden file storage paths (#47)

Felix-Ayushcef3378f8c78
diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts
index f5fdd91..2ffca91 100644
--- a/apps/server/src/app.ts
+++ b/apps/server/src/app.ts
@@ -188,7 +188,9 @@ export async function createApp(
});
app.get("/api/drafts", async (c) => c.json(await db.list(c.get("owner"), "drafts")));
app.post("/api/drafts", async (c) => {
- const body = emailDraftSchema.extend({ id: z.string().optional() }).parse(await c.req.json());
+ const body = emailDraftSchema
+ .extend({ id: z.string().uuid().optional() })
+ .parse(await c.req.json());
const existing = body.id
? await db.get<{ createdAt: string }>(c.get("owner"), "drafts", body.id)
: null;
diff --git a/apps/server/src/browser.ts b/apps/server/src/browser.ts
index 5675d67..c55b2a6 100644
--- a/apps/server/src/browser.ts
+++ b/apps/server/src/browser.ts
@@ -30,6 +30,13 @@ const failureSchema = z.object({
});
type ChatBrowser = { id: string; sessionId: string };
+const BROWSER_SESSION_ID =
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
+
+function assertSessionId(id: string): void {
+ if (!BROWSER_SESSION_ID.test(id)) throw new AppError("Browser session not found", 404);
+}
+
export class BrowserService {
private readonly queues = new Map<string, Promise<unknown>>();
private health?: { checkedAt: number; reachable: Promise<boolean> };
@@ -99,6 +106,7 @@ export class BrowserService {
return response;
}
async get(owner: string, id: string) {
+ assertSessionId(id);
const value = await this.db.get<BrowserSession>(owner, "browsers", id);
if (!value) throw new AppError("Browser session not found", 404);
return value;
@@ -153,7 +161,9 @@ export class BrowserService {
private async readOwned(owner: string, id: string, signal?: AbortSignal) {
const session = await this.get(owner, id);
const result = readSchema.parse(
- await (await this.request(`/sessions/${id}/read`, undefined, signal)).json(),
+ await (
+ await this.request(`/sessions/${encodeURIComponent(id)}/read`, undefined, signal)
+ ).json(),
);
await this.save(
owner,
@@ -215,19 +225,25 @@ export class BrowserService {
async close(owner: string, id: string) {
return this.serial(id, async () => {
await this.get(owner, id);
- return this.save(owner, await (await this.request(`/sessions/${id}/close`, {})).json(), id);
+ return this.save(
+ owner,
+ await (await this.request(`/sessions/${encodeURIComponent(id)}/close`, {})).json(),
+ id,
+ );
});
}
async preview(owner: string, id: string) {
await this.get(owner, id);
- return this.request(`/sessions/${id}/screenshot`);
+ return this.request(`/sessions/${encodeURIComponent(id)}/screenshot`);
}
async input(owner: string, id: string, value: unknown) {
+ if (!value || typeof value !== "object" || Array.isArray(value))
+ throw new AppError("A JSON object is required for browser input", 422);
return this.serial(id, async () => {
await this.get(owner, id);
return this.save(
owner,
- await (await this.request(`/sessions/${id}/input`, value)).json(),
+ await (await this.request(`/sessions/${encodeURIComponent(id)}/input`, value)).json(),
id,
);
});
@@ -241,7 +257,7 @@ export class BrowserService {
),
failures: z.array(failureSchema),
})
- .parse(await (await this.request(`/sessions/${id}/downloads`)).json());
+ .parse(await (await this.request(`/sessions/${encodeURIComponent(id)}/downloads`)).json());
const saved = [];
for (const download of downloads) {
const existing = await this.db.get<{ fileId: string }>(
@@ -254,7 +270,7 @@ export class BrowserService {
continue;
}
const response = await this.request(
- `/sessions/${id}/downloads/${encodeURIComponent(download.id)}`,
+ `/sessions/${encodeURIComponent(id)}/downloads/${encodeURIComponent(download.id)}`,
);
const file = await this.files.import(
owner,
diff --git a/apps/server/src/files.ts b/apps/server/src/files.ts
index 02e5de4..239985e 100644
--- a/apps/server/src/files.ts
+++ b/apps/server/src/files.ts
@@ -1,6 +1,6 @@
import { randomUUID } from "node:crypto";
import { mkdir, readFile, writeFile } from "node:fs/promises";
-import { join } from "node:path";
+import { join, resolve, sep } from "node:path";
import type { Artifact } from "../../../packages/domain/src/index.ts";
import { fillPdf, inspectPdf } from "../../../packages/integrations/src/pdf.ts";
import type { Auth } from "./auth.ts";
@@ -8,6 +8,21 @@ import type { Config } from "./config.ts";
import type { Store } from "./db.ts";
import { AppError } from "./errors.ts";
+const FILE_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
+
+export function sanitizeFileName(name: string): string {
+ const base = name.split(/[\\/]/).at(-1) ?? "document.pdf";
+ const cleaned = Array.from(base)
+ .filter((character) => character.charCodeAt(0) >= 32 && character.charCodeAt(0) !== 127)
+ .join("")
+ .slice(0, 180);
+ return cleaned || "document.pdf";
+}
+
+function assertFileId(id: string): void {
+ if (!FILE_ID.test(id)) throw new AppError("File not found", 404);
+}
+
export class Files {
constructor(
private readonly db: Store,
@@ -25,10 +40,7 @@ export class Files {
const metadata = await inspectPdf(bytes);
if (metadata.pageCount > 500) throw new AppError("PDFs must have 500 pages or fewer", 422);
const id = randomUUID();
- const safeName = Array.from(name.split(/[\\/]/).at(-1) ?? "document.pdf")
- .filter((character) => character.charCodeAt(0) >= 32 && character.charCodeAt(0) !== 127)
- .join("")
- .slice(0, 180);
+ const safeName = sanitizeFileName(name);
const artifact: Artifact = {
id,
name: safeName,
@@ -54,13 +66,21 @@ export class Files {
return (await this.db.list<Artifact>(owner, "files")).map((file) => this.signed(owner, file));
}
async get(owner: string, id: string) {
+ assertFileId(id);
const file = await this.db.get<Artifact>(owner, "files", id);
if (!file) throw new AppError("File not found", 404);
return file;
}
+ private pathFor(id: string): string {
+ assertFileId(id);
+ const directory = resolve(this.config.dataDir, "files");
+ const resolved = resolve(directory, `${id}.pdf`);
+ if (!resolved.startsWith(`${directory}${sep}`)) throw new AppError("File not found", 404);
+ return resolved;
+ }
async bytes(owner: string, id: string) {
await this.get(owner, id);
- return readFile(join(this.config.dataDir, "files", `${id}.pdf`));
+ return readFile(this.pathFor(id));
}
async fill(owner: string, id: string, values: Record<string, string | boolean>) {
const file = await this.get(owner, id);
diff --git a/tests/resource-ids.test.ts b/tests/resource-ids.test.ts
new file mode 100644
index 0000000..0dd2255
--- /dev/null
+++ b/tests/resource-ids.test.ts
@@ -0,0 +1,139 @@
+import assert from "node:assert/strict";
+import { mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { after, before, test } from "node:test";
+import { createApp } from "../apps/server/src/app.ts";
+import { Auth } from "../apps/server/src/auth.ts";
+import type { BrowserService } from "../apps/server/src/browser.ts";
+import type { Config } from "../apps/server/src/config.ts";
+import { createStore, type Store } from "../apps/server/src/db.ts";
+import { Files, sanitizeFileName } from "../apps/server/src/files.ts";
+import { createSamplePdf } from "../packages/integrations/src/pdf.ts";
+import { browserFixture } from "./helpers/browser.ts";
+
+let db: Store, app: Awaited<ReturnType<typeof createApp>>["app"], token: string, directory: string;
+const headers = () => ({ Authorization: `Bearer ${token}`, "Content-Type": "application/json" });
+
+before(async () => {
+ directory = await mkdtemp(join(tmpdir(), "openmuse-resource-ids-"));
+ db = await createStore();
+ const config: Config = {
+ mode: "sample",
+ port: 8787,
+ host: "127.0.0.1",
+ publicUrl: "http://localhost:8787",
+ dataDir: directory,
+ agentBackend: "sample",
+ intelligenceApiKey: "test-project-key-never-sent",
+ googleRedirectUri: "http://localhost:8787/api/google/callback",
+ allowedOrigins: ["http://localhost:8081"],
+ };
+ ({ app } = await createApp(db, config));
+ const response = await app.request("/api/session", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: "{}",
+ });
+ assert.equal(response.status, 200);
+ token = (await response.json()).token;
+});
+
+after(async () => {
+ await db.close();
+ await rm(directory, { recursive: true, force: true });
+});
+
+test("file names never collapse to an empty display name", () => {
+ assert.equal(sanitizeFileName("///"), "document.pdf");
+ assert.equal(sanitizeFileName("\u0000\u0001\u007f"), "document.pdf");
+ assert.equal(sanitizeFileName(""), "document.pdf");
+ assert.equal(sanitizeFileName("C:\\fakepath\\report.pdf"), "report.pdf");
+ assert.equal(sanitizeFileName("/tmp/../invoice.pdf"), "invoice.pdf");
+ const long = sanitizeFileName(`${"a".repeat(300)}.pdf`);
+ assert.ok(long.length <= 180);
+ assert.ok(long.length > 0);
+});
+
+test("file lookups reject malformed IDs instead of probing storage", async (t) => {
+ const dir = await mkdtemp(join(tmpdir(), "openmuse-file-id-"));
+ t.after(async () => rm(dir, { recursive: true, force: true }));
+ const store = await createStore();
+ t.after(() => store.close());
+ const config: Config = {
+ mode: "sample",
+ port: 8787,
+ host: "127.0.0.1",
+ publicUrl: "http://localhost:8787",
+ dataDir: dir,
+ agentBackend: "sample",
+ intelligenceApiKey: "test-project-key-never-sent",
+ googleRedirectUri: "http://localhost:8787/api/google/callback",
+ allowedOrigins: [],
+ };
+ const files = new Files(store, config, new Auth(store, config, "test-signing-key"));
+ for (const id of ["", "not-a-uuid", "../secret", "..\\secret", "a/b", `${"a".repeat(36)}`]) {
+ await assert.rejects(files.get("owner", id), { message: /File not found/ });
+ await assert.rejects(files.bytes("owner", id), { message: /File not found/ });
+ }
+ const sample = await createSamplePdf();
+ const imported = await files.import("owner", "///", sample, "test");
+ assert.equal(imported.name, "document.pdf");
+ const roundTrip = await files.bytes("owner", imported.id);
+ assert.equal(Buffer.from(roundTrip).subarray(0, 5).toString(), "%PDF-");
+});
+
+test("browser sessions reject malformed IDs and non-object input locally", async (t) => {
+ const calls: string[] = [];
+ const { service } = await browserFixture(t, (path) => {
+ calls.push(path);
+ return { data: {} };
+ });
+ for (const id of ["", "not-a-uuid", "../escape", "a/b", "00000000-0000-4000-8000-0000000000zz"]) {
+ await assert.rejects(service.get("owner", id), { message: /Browser session not found/ });
+ }
+ const validId = "00000000-0000-4000-8000-000000000001";
+ for (const value of [null, undefined, "click", 42, ["x"]]) {
+ await assert.rejects((service as BrowserService).input("owner", validId, value), {
+ message: /JSON object/i,
+ });
+ }
+ assert.equal(calls.length, 0, "validation happens before any worker request");
+});
+
+test("HTTP routes surface malformed resource IDs as client errors", async () => {
+ assert.equal(
+ (await app.request("/api/files/not-a-uuid/content", { headers: headers() })).status,
+ 404,
+ );
+ assert.equal(
+ (await app.request("/api/files/..secret/content", { headers: headers() })).status,
+ 404,
+ );
+ assert.equal(
+ (
+ await app.request("/api/files/not-a-uuid/fill", {
+ method: "POST",
+ headers: headers(),
+ body: JSON.stringify({ fields: {} }),
+ })
+ ).status,
+ 404,
+ );
+ assert.equal((await app.request("/api/browsers/not-a-uuid", { headers: headers() })).status, 404);
+ assert.equal(
+ (await app.request("/api/browsers/not-a-uuid/read", { headers: headers() })).status,
+ 404,
+ );
+ const draft = await app.request("/api/drafts", {
+ method: "POST",
+ headers: headers(),
+ body: JSON.stringify({
+ id: "not-a-uuid",
+ subject: "Hello",
+ body: "World",
+ }),
+ });
+ assert.equal(draft.status, 422);
+});