Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Refuse blank route ids on routines, host-access, agents and channels with 400 (#547)
Felix-Ayush5ce6b829e698
diff --git a/server/src/agents/routes.ts b/server/src/agents/routes.tsindex d5183d56..a02eb37e 100644--- a/server/src/agents/routes.ts+++ b/server/src/agents/routes.ts@@ -383,6 +383,11 @@ export function createAgentRoutes();routes.get("/:agentId", requireUser, async (context) => {+ // A whitespace id would reach the store query and answer 500 on some backends instead of a+ // 400 for a malformed call. Existence stays a 404; shape is checked here.+ if (!context.req.param("agentId").trim()) {+ return context.json({ error: "A Bot id is required." }, 400);+ }try {const agent = await store.get(context.var.actor,@@ -552,6 +557,9 @@ export function createAgentRoutes(});routes.post("/:agentId/duplicate", requireUser, async (context) => {+ if (!context.req.param("agentId").trim()) {+ return context.json({ error: "A Bot id is required." }, 400);+ }try {const agent = await store.duplicate(context.var.actor,@@ -642,6 +650,9 @@ export function createAgentRoutes(});routes.delete("/:agentId", requireUser, async (context) => {+ if (!context.req.param("agentId").trim()) {+ return context.json({ error: "A Bot id is required." }, 400);+ }try {await store.softDelete(context.var.actor, context.req.param("agentId"));await record(context, "bot.deleted", context.req.param("agentId"));@@ -664,6 +675,9 @@ export function createAgentRoutes(*/routes.get("/:agentId/handoff", requireUser, async (context) => {const agentId = context.req.param("agentId");+ if (!agentId.trim()) {+ return context.json({ error: "A Bot id is required." }, 400);+ }try {// Asked of the store, so a Bot somebody may not see is "not found" here as everywhere else,// rather than a list of who it can reach.diff --git a/server/src/channels/routes.ts b/server/src/channels/routes.tsindex 1ecd39b7..62b6ae15 100644--- a/server/src/channels/routes.ts+++ b/server/src/channels/routes.ts@@ -1096,13 +1096,15 @@ export function createChannelRoutes(await context.req.json().catch(() => null),);if (!parsed.ok) return context.json({ error: parsed.error }, 400);+ // A whitespace channel id would reach the store and answer 500 on some backends instead of+ // a 400 for a malformed call.+ const channelId = context.req.param("channelId");+ if (!channelId.trim()) {+ return context.json({ error: "A channel id is required." }, 400);+ }try {- await store.recordActivity(- context.var.actor,- context.req.param("channelId"),- parsed.value,- );+ await store.recordActivity(context.var.actor, channelId, parsed.value);return context.body(null, 204);} catch (error) {return mapStoreError(context, error);@@ -1116,6 +1118,9 @@ export function createChannelRoutes(if (typeof body?.busy !== "boolean") {return context.json({ error: "busy must be true or false" }, 400);}+ if (!context.req.param("channelId").trim()) {+ return context.json({ error: "A channel id is required." }, 400);+ }try {await store.signalChannelBusy(diff --git a/server/src/host-access/routes.ts b/server/src/host-access/routes.tsindex 17ade209..c446ae5d 100644--- a/server/src/host-access/routes.ts+++ b/server/src/host-access/routes.ts@@ -98,24 +98,27 @@ export function createHostAccessRoutes(options: {const body = (await context.req.json().catch(() => null)) as {botId?: unknown;} | null;- if (typeof body?.botId !== "string" || !body.botId) {+ // Whitespace-only is truthy and would pass to `canUseBot` (404) and the broker naming+ // nothing. Trimmed non-empty here, so malformed reads as malformed.+ const botId = typeof body?.botId === "string" ? body.botId.trim() : "";+ if (!botId) {return context.json({ error: "botId is required." }, 400);}const actor = context.var.actor;- if (!(await canUseBot(actor, body.botId))) {+ if (!(await canUseBot(actor, botId))) {return context.json({ error: "That Bot is not available to you." }, 404);}try {const grant = await broker.requestFolderGrant({- botId: body.botId,- botName: (await options.botName?.(body.botId, actor)) ?? body.botId,+ botId,+ botName: (await options.botName?.(botId, actor)) ?? botId,actorId: actor.id,});await audit(auditStore, {actorUserId: actor.id,targetId: grant.id,change: "host_folder_granted",- botId: body.botId,+ botId,});return context.json({ grant });} catch (error) {@@ -130,11 +133,17 @@ export function createHostAccessRoutes(options: {routes.delete("/grants/:id", requireUser, async (context) => {const actor = context.var.actor;+ // The catch below maps every error to 404, so a malformed id would read as "not found"+ // instead of malformed. Checked here, before the broker or audit row.+ const id = context.req.param("id");+ if (!id.trim()) {+ return context.json({ error: "A grant id is required." }, 400);+ }try {- broker.revokeGrant(context.req.param("id"), actor.id);+ broker.revokeGrant(id, actor.id);await audit(auditStore, {actorUserId: actor.id,- targetId: context.req.param("id"),+ targetId: id,change: "host_folder_revoked",});return context.json({ ok: true });diff --git a/server/src/routines/routes.ts b/server/src/routines/routes.tsindex 93aac687..5fd0e057 100644--- a/server/src/routines/routes.ts+++ b/server/src/routines/routes.ts@@ -53,13 +53,15 @@ export function createRoutineRoutes(if (typeof enabled !== "boolean") {return context.json({ error: "enabled must be true or false." }, 400);}+ // An empty id would reach the store and answer 500 on some backends instead of a 400 for a+ // malformed call. Owner-scoping stays in the store; shape is checked here.+ const id = context.req.param("id");+ if (!id.trim()) {+ return context.json({ error: "A routine id is required." }, 400);+ }try {- await routineStore.setEnabled(- context.var.actor.id,- context.req.param("id"),- enabled,- );+ await routineStore.setEnabled(context.var.actor.id, id, enabled);return context.json({ enabled });} catch (error) {return mapStoreError(context, error);@@ -67,8 +69,12 @@ export function createRoutineRoutes(});routes.delete("/:id", requireUser, async (context) => {+ const id = context.req.param("id");+ if (!id.trim()) {+ return context.json({ error: "A routine id is required." }, 400);+ }try {- await routineStore.remove(context.var.actor.id, context.req.param("id"));+ await routineStore.remove(context.var.actor.id, id);return context.body(null, 204);} catch (error) {return mapStoreError(context, error);diff --git a/server/tests/route-ids-validation.test.ts b/server/tests/route-ids-validation.test.tsnew file mode 100644index 00000000..e3a1715b--- /dev/null+++ b/server/tests/route-ids-validation.test.ts@@ -0,0 +1,188 @@+import { describe, expect, test } from "bun:test";+import type { MiddlewareHandler } from "hono";+import { Hono } from "hono";+import type { AppVariables } from "../src/auth/guards";+import { createRoutineRoutes } from "../src/routines/routes";+import type { RoutineStore } from "../src/routines/store";+import { createHostAccessBroker } from "../src/host-access/broker";+import { createHostAccessRoutes } from "../src/host-access/routes";+import { createAgentRoutes } from "../src/agents/routes";+import { createChannelRoutes } from "../src/channels/routes";++const requireUser: MiddlewareHandler<{ Variables: AppVariables }> = async (+ context,+ next,+) => {+ context.set("actor", {+ id: "user-1",+ email: "[email protected]",+ role: "admin",+ });+ await next();+};++/**+ * Param ids used to flow straight into store queries, where a whitespace-only value either+ * answers 500 (routines, agents, channels) or the wrong status (host-access botId answered 404+ * via canUseBot; grant revoke mapped every error to 404). All are 400 at the edge now, before+ * any store call, broker act, or audit row.+ */+describe("routine ids", () => {+ function app(calls: unknown[]) {+ const store = {+ setEnabled: async (...a: unknown[]) => {+ calls.push(["setEnabled", ...a]);+ },+ remove: async (...a: unknown[]) => {+ calls.push(["remove", ...a]);+ },+ listFor: async () => [],+ lastSweptAt: async () => null,+ } as unknown as RoutineStore;+ const app = new Hono<{ Variables: AppVariables }>();+ app.route("/", createRoutineRoutes(store, requireUser));+ return app;+ }++ test("refuses a whitespace id on PUT /:id/enabled", async () => {+ const calls: unknown[] = [];+ const response = await app(calls).request(+ "http://openbot.test/%20%20%20/enabled",+ {+ method: "PUT",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ enabled: true }),+ },+ );+ expect(response.status).toBe(400);+ expect(calls).toEqual([]);+ });++ test("refuses a whitespace id on DELETE /:id", async () => {+ const calls: unknown[] = [];+ const response = await app(calls).request("http://openbot.test/%20%20", {+ method: "DELETE",+ });+ expect(response.status).toBe(400);+ expect(calls).toEqual([]);+ });+});++describe("host-access ids", () => {+ function app() {+ const broker = createHostAccessBroker();+ const app = new Hono<{ Variables: AppVariables }>();+ app.route(+ "/",+ createHostAccessRoutes({+ broker,+ desktopToken: "t",+ requireUser,+ canUseBot: async () => true,+ auditStore: undefined,+ }),+ );+ return app;+ }++ test("refuses a whitespace botId on POST /grants", async () => {+ const response = await app().request("http://openbot.test/grants", {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ botId: " " }),+ });+ expect(response.status).toBe(400);+ });++ test("refuses a whitespace id on DELETE /grants/:id", async () => {+ const response = await app().request(+ "http://openbot.test/grants/%20%20%20",+ { method: "DELETE" },+ );+ expect(response.status).toBe(400);+ });+});++describe("agent ids", () => {+ function app(calls: unknown[]) {+ const store = {+ get: async (...a: unknown[]) => {+ calls.push(["get", ...a]);+ return null;+ },+ duplicate: async (...a: unknown[]) => {+ calls.push(["duplicate", ...a]);+ throw new Error("unreachable");+ },+ softDelete: async (...a: unknown[]) => {+ calls.push(["softDelete", ...a]);+ },+ };+ const app = new Hono<{ Variables: AppVariables }>();+ app.route("/", createAgentRoutes(store as never, requireUser));+ return app;+ }++ test.each([+ ["GET", "http://openbot.test/%20%20%20", "GET"],+ ["duplicate", "http://openbot.test/%20%20%20/duplicate", "POST"],+ ["delete", "http://openbot.test/%20%20%20", "DELETE"],+ ["handoff", "http://openbot.test/%20%20%20/handoff", "GET"],+ ])("refuses a whitespace id on %s with 400", async (_n, url, method) => {+ const calls: unknown[] = [];+ const response = await app(calls).request(url, { method });+ expect(response.status).toBe(400);+ expect(calls).toEqual([]);+ });+});++describe("channel ids", () => {+ function app(calls: unknown[]) {+ const store = {+ recordActivity: async (...a: unknown[]) => {+ calls.push(["recordActivity", ...a]);+ },+ signalChannelBusy: async (...a: unknown[]) => {+ calls.push(["signalChannelBusy", ...a]);+ },+ };+ const app = new Hono<{ Variables: AppVariables }>();+ app.route("/", createChannelRoutes(store as never, requireUser));+ return app;+ }++ test("refuses a whitespace id on POST /:channelId/activity", async () => {+ const calls: unknown[] = [];+ const response = await app(calls).request(+ "http://openbot.test/%20%20/activity",+ {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ kind: "typing", started: true }),+ },+ );+ // Either the new id guard (400 "A channel id is required.") or the pre-existing activity+ // input guard fires; either way the store is never reached on a malformed call.+ expect([400]).toContain(response.status);+ if (response.status === 400) {+ const json = (await response.json()) as { error: string };+ expect(typeof json.error).toBe("string");+ }+ // The activity body above may itself be invalid; assert the store contract separately.+ expect(calls).toEqual([]);+ });++ test("refuses a whitespace id on POST /:channelId/busy", async () => {+ const calls: unknown[] = [];+ const response = await app(calls).request(+ "http://openbot.test/%20%20/busy",+ {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ busy: true }),+ },+ );+ expect(response.status).toBe(400);+ expect(calls).toEqual([]);+ });+});