Skip to content

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.

Harden admin people search and credential input with 400s (#545)

Felix-Ayushf899c4d2924c
diff --git a/server/src/app.ts b/server/src/app.ts
index 4326e154..fb3c333a 100644
--- a/server/src/app.ts
+++ b/server/src/app.ts
@@ -628,6 +628,15 @@ export function createApp(
if (!parsed.ok) {
return context.json({ error: parsed.error }, 400);
}
+ // An unbounded `search` becomes a `%...% ILIKE` full scan. Cap it so a multi-megabyte
+ // query cannot be used as a cheap denial of service against the people table.
+ const search = url.searchParams.get("search");
+ if (search !== null && search.length > 200) {
+ return context.json(
+ { error: "A search of at most 200 characters is required." },
+ 400,
+ );
+ }
return context.json(
await peopleStore.list({
@@ -1505,20 +1514,23 @@ function credentialInput(
body.kind !== "connector" &&
body.kind !== "mcp") ||
typeof body.provider !== "string" ||
+ !body.provider.trim() ||
typeof body.keyId !== "string" ||
+ !body.keyId.trim() ||
typeof body.plaintext !== "string" ||
!body.plaintext ||
!body.metadata ||
typeof body.metadata !== "object" ||
- Array.isArray(body.metadata)
+ Array.isArray(body.metadata) ||
+ Object.getPrototypeOf(body.metadata) !== Object.prototype
) {
return null;
}
return {
kind: body.kind,
- provider: body.provider,
- keyId: body.keyId,
+ provider: body.provider.trim(),
+ keyId: body.keyId.trim(),
metadata: body.metadata as Record<string, unknown>,
plaintext: body.plaintext,
actorUserId,
diff --git a/server/src/people/store.ts b/server/src/people/store.ts
index e62ec182..7d598e04 100644
--- a/server/src/people/store.ts
+++ b/server/src/people/store.ts
@@ -103,14 +103,28 @@ function encodeCursor(cursor: Cursor): string {
*
* A malformed one is treated as no cursor rather than as an error: it means the first page, which is
* a sensible answer to a stale or hand-edited link, and there is nothing here worth refusing over.
+ *
+ * Exported for regression tests: a well-formed cursor carrying a non-date `lastSignedInAt` must
+ * also fall back instead of reaching `::timestamptz` in SQL and answering 500.
*/
-function decodeCursor(value: string | undefined): Cursor | undefined {
+export function decodeCursor(value: string | undefined): Cursor | undefined {
if (!value) return undefined;
try {
const parsed = JSON.parse(
Buffer.from(value, "base64url").toString("utf8"),
) as Cursor;
if (typeof parsed?.email !== "string") return undefined;
+ // A well-formed cursor with a non-date `lastSignedInAt` would reach
+ // `${cursor.lastSignedInAt}::timestamptz` in SQL and answer 500. Treat it as no cursor
+ // (first page), consistent with how a stale or hand-edited cursor is handled above.
+ if (
+ parsed.lastSignedInAt !== null &&
+ parsed.lastSignedInAt !== undefined &&
+ (typeof parsed.lastSignedInAt !== "string" ||
+ Number.isNaN(Date.parse(parsed.lastSignedInAt)))
+ ) {
+ return undefined;
+ }
return {
email: parsed.email,
lastSignedInAt:
diff --git a/server/tests/admin-people-credentials-validation.test.ts b/server/tests/admin-people-credentials-validation.test.ts
new file mode 100644
index 00000000..f1a825eb
--- /dev/null
+++ b/server/tests/admin-people-credentials-validation.test.ts
@@ -0,0 +1,154 @@
+import { describe, expect, test } from "bun:test";
+import { createApp } from "../src/app";
+import { loadConfig } from "../src/config";
+import { decodeCursor } from "../src/people/store";
+import { testEnvironment } from "./support/environment";
+
+const ADMIN = {
+ id: "admin-1",
+ email: "[email protected]",
+ name: "An Administrator",
+ image: null,
+};
+
+function appWith(opts: {
+ peopleList?: (query: unknown) => Promise<unknown>;
+ credentialCreate?: (input: unknown) => Promise<unknown>;
+}) {
+ const calls: { people: unknown[]; credentials: unknown[] } = {
+ people: [],
+ credentials: [],
+ };
+ const peopleStore = {
+ list: async (query: unknown) => {
+ calls.people.push(query);
+ if (opts.peopleList) return opts.peopleList(query);
+ return { people: [], nextCursor: null };
+ },
+ };
+ const credentialService = {
+ list: async () => [],
+ create: async (input: unknown) => {
+ calls.credentials.push(input);
+ if (opts.credentialCreate) return opts.credentialCreate(input);
+ return { credential: input };
+ },
+ };
+ // createApp takes positional stores; peopleStore is the 18th parameter. Build the argument
+ // list by index so a future appended store cannot silently shift it.
+ const args: never[] = Array.from({ length: 18 }) as never[];
+ args[0] = loadConfig(testEnvironment()) as never;
+ args[1] = {
+ handler: () => new Response(null, { status: 204 }),
+ api: { getSession: async () => ({ user: ADMIN }) },
+ } as never;
+ args[2] = { rolesForUser: async () => ["admin"] } as never;
+ args[4] = credentialService as never;
+ args[17] = peopleStore as never;
+ const app = createApp(...args);
+ return { app, calls };
+}
+
+/**
+ * `GET /api/admin/people` capped `limit` strictly but passed `search` straight to a `%...% ILIKE`
+ * full scan. A multi-megabyte search is a cheap denial of service; over 200 characters is now a
+ * 400 before the store is touched.
+ */
+describe("GET /api/admin/people search", () => {
+ test("refuses an oversized search with 400 and never reaches the store", async () => {
+ const { app, calls } = appWith({});
+ const response = await app.request(
+ `http://openbot.test/api/admin/people?search=${"x".repeat(201)}`,
+ { method: "GET" },
+ );
+ expect(response.status).toBe(400);
+ expect(await response.json()).toEqual({
+ error: "A search of at most 200 characters is required.",
+ });
+ expect(calls.people).toEqual([]);
+ });
+
+ test("passes a normal search to the store", async () => {
+ const { app, calls } = appWith({});
+ const response = await app.request(
+ "http://openbot.test/api/admin/people?search=ada",
+ { method: "GET" },
+ );
+ expect(response.status).toBe(200);
+ expect(calls.people).toHaveLength(1);
+ });
+});
+
+/**
+ * `credentialInput` accepted empty `provider`/`keyId` strings and any non-array object as
+ * `metadata`, so `{"provider":"","keyId":""}` reached the vault as a junk row (or a unique
+ * violation 500) and `{"metadata":{"__proto__":{...}}}` passed the shape check.
+ */
+describe("POST /api/admin/credentials input", () => {
+ const good = {
+ kind: "mcp",
+ provider: "acme",
+ keyId: "key-1",
+ plaintext: "secret",
+ metadata: {},
+ };
+ test.each([
+ ["an empty provider", { ...good, provider: "" }],
+ ["a whitespace provider", { ...good, provider: " " }],
+ ["a number provider", { ...good, provider: 123 }],
+ ["an empty keyId", { ...good, keyId: "" }],
+ ["a whitespace keyId", { ...good, keyId: " " }],
+ ["an array metadata", { ...good, metadata: [] }],
+ ])("refuses %s with 400 and never reaches the vault", async (_n, body) => {
+ const { app, calls } = appWith({});
+ const response = await app.request(
+ "http://openbot.test/api/admin/credentials",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify(body),
+ },
+ );
+ expect(response.status).toBe(400);
+ expect(calls.credentials).toEqual([]);
+ });
+
+ test("trims provider and keyId on the happy path", async () => {
+ const { app, calls } = appWith({});
+ const response = await app.request(
+ "http://openbot.test/api/admin/credentials",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ ...good, provider: " acme " }),
+ },
+ );
+ expect(response.status).toBe(201);
+ expect(calls.credentials[0]).toMatchObject({ provider: "acme" });
+ });
+});
+
+/**
+ * A well-formed cursor carrying a non-date `lastSignedInAt` used to reach
+ * `${cursor.lastSignedInAt}::timestamptz` in SQL and answer 500. It now falls back to the
+ * first page like any other stale cursor.
+ */
+describe("decodeCursor", () => {
+ function encode(value: unknown): string {
+ return Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
+ }
+
+ test("falls back on a non-date lastSignedInAt", () => {
+ expect(
+ decodeCursor(encode({ email: "[email protected]", lastSignedInAt: "not-a-date" })),
+ ).toBeUndefined();
+ });
+
+ test("keeps a valid cursor", () => {
+ expect(
+ decodeCursor(
+ encode({ email: "[email protected]", lastSignedInAt: "2026-01-01T00:00:00Z" }),
+ ),
+ ).toEqual({ email: "[email protected]", lastSignedInAt: "2026-01-01T00:00:00Z" });
+ });
+});