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.

Refuse a malformed OAuth client with a 400, not a 500 (#674)

Asem ahmedeaff9ac57ddd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4716411b..cdff9030 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -131,6 +131,13 @@ its model asked for a skill by a name the snapshot does not hold, for a file the
list, or sent arguments that were not JSON. A built-in Bot's model is handed that sentence as the
call's result and carries on. A remote Bot's model now gets the same result and carries on too.
+### A malformed OAuth client is refused with a 400, not a 500
+
+`POST /api/plugins/servers/:id/oauth-client` called `.trim()` on the client id and secret without
+checking they were strings, so `{"clientId": 12345, "clientSecret": "s"}`, or a secret of `{}`, threw
+outside the route's try and answered 500. It now answers the same 400 as an empty value, as the
+other plugin routes do for their own fields, before the store or the audit trail is touched.
+
### Browser challenges can be handed to a person without losing the Bot's page
Bots pause for actionable browser challenges and resume from a fresh page snapshot after an explicit
diff --git a/server/src/plugins/routes.ts b/server/src/plugins/routes.ts
index 3088868f..e5770467 100644
--- a/server/src/plugins/routes.ts
+++ b/server/src/plugins/routes.ts
@@ -441,10 +441,20 @@ export function createPluginRoutes(
if (forbidden) return forbidden;
const body = (await context.req.json().catch(() => null)) as {
- clientId?: string;
- clientSecret?: string;
+ clientId?: unknown;
+ clientSecret?: unknown;
} | null;
- if (!body?.clientId?.trim() || !body.clientSecret?.trim()) {
+ /*
+ * `typeof` before `.trim()`, as `POST /servers` and `/servers/custom` do: the body is JSON, so
+ * `{"clientId": 12345}` or a secret of `{}` used to reach `.trim()` here, outside the try, and
+ * answer 500 for what is a person's malformed request.
+ */
+ if (
+ typeof body?.clientId !== "string" ||
+ typeof body.clientSecret !== "string" ||
+ !body.clientId.trim() ||
+ !body.clientSecret.trim()
+ ) {
return context.json(
{ error: "A client id and a client secret are both required." },
400,
diff --git a/server/tests/plugin-servers-validation.test.ts b/server/tests/plugin-servers-validation.test.ts
index 9e53ecf4..7481ee4e 100644
--- a/server/tests/plugin-servers-validation.test.ts
+++ b/server/tests/plugin-servers-validation.test.ts
@@ -142,3 +142,80 @@ describe("POST /api/plugins/servers/custom", () => {
expect(seen.addCustomServer).toEqual([]);
});
});
+
+/**
+ * `POST /servers/:id/oauth-client` cast the body to `{ clientId?: string; clientSecret?: string }`
+ * and then called `.trim()` on it. The body is JSON, so a number or an object passed the annotation
+ * and threw a TypeError outside the try, and a person's malformed request answered 500. Every other
+ * route in this file checks `typeof` first; this one is the same 400 now, before the store or the
+ * audit trail is touched.
+ */
+describe("POST /api/plugins/servers/:id/oauth-client", () => {
+ function oauthApp(registered: unknown[]) {
+ const store = {
+ registerOAuthClient: async (input: unknown) => {
+ registered.push(input);
+ },
+ } as unknown as PluginStore;
+ const requireUser: MiddlewareHandler<{ Variables: AppVariables }> = async (
+ context,
+ next,
+ ) => {
+ context.set("actor", {
+ id: "user-1",
+ email: "[email protected]",
+ role: "admin",
+ });
+ await next();
+ };
+ const canUseBot: BotAccessCheck = async () => true;
+ return createPluginRoutes(store, requireUser, canUseBot);
+ }
+
+ const send = (registered: unknown[], body: unknown) =>
+ oauthApp(registered).request(
+ "http://openbot.test/servers/github/oauth-client",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify(body),
+ },
+ );
+
+ test.each([
+ ["a number client id", { clientId: 12345, clientSecret: "secret" }],
+ ["an object client id", { clientId: {}, clientSecret: "secret" }],
+ ["a number client secret", { clientId: "id", clientSecret: 12345 }],
+ ["an object client secret", { clientId: "id", clientSecret: {} }],
+ ["an array client secret", { clientId: "id", clientSecret: [] }],
+ ["a missing client secret", { clientId: "id" }],
+ ["a whitespace client id", { clientId: " ", clientSecret: "secret" }],
+ ["a whitespace client secret", { clientId: "id", clientSecret: " " }],
+ ])("refuses %s with 400 and never reaches the store", async (_n, body) => {
+ const registered: unknown[] = [];
+ const response = await send(registered, body);
+
+ expect(response.status).toBe(400);
+ expect(await response.json()).toEqual({
+ error: "A client id and a client secret are both required.",
+ });
+ expect(registered).toEqual([]);
+ });
+
+ test("trims both values on the happy path", async () => {
+ const registered: unknown[] = [];
+ const response = await send(registered, {
+ clientId: " id ",
+ clientSecret: " secret ",
+ });
+
+ expect(response.status).toBe(200);
+ expect(registered).toEqual([
+ {
+ serverId: "github",
+ client: { clientId: "id", clientSecret: "secret" },
+ },
+ ]);
+ });
+});