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 grant of an app or skill to a Bot that does not exist (#677)

Asem ahmedf6a5dcca7ae7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95fd8eba..c382f593 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.
+### An app or skill cannot be granted to a Bot that does not exist
+
+An administrator's `POST /api/plugins/grants` for an app or a skill checked that the app or skill
+existed but not the Bot, so a mistyped Bot id reached the insert, failed on the `plugin_grants`
+foreign key, and answered 500 with no body. It is now refused with "There is no such Bot.", the
+sentence the `bot` kind already used, and nothing is stored. Revoking still checks nothing.
+
### 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
diff --git a/server/src/plugins/routes.ts b/server/src/plugins/routes.ts
index e5770467..ef701e36 100644
--- a/server/src/plugins/routes.ts
+++ b/server/src/plugins/routes.ts
@@ -2312,6 +2312,14 @@ export function createPluginRoutes(
if (!(await store.serverExists(serverId ?? ""))) {
return `${serverId} is not an app this deployment has added, so there is nothing for a Bot to reach. Add it first, and its tools can be granted then.`;
}
+ /*
+ * The Bot has to exist as well, which the `bot` branch below already says in the same words.
+ * `plugin_grants.agent_id` is a foreign key, so a grant naming a Bot nobody has reached the
+ * insert and failed there, and a person's mistyped Bot id answered 500 with no body where
+ * every other refusal on this route is a 403 with a sentence.
+ */
+ if (!(await store.agentIsRegistered(agentId)))
+ return "There is no such Bot.";
return null;
}
@@ -2372,6 +2380,10 @@ export function createPluginRoutes(
if ((await store.skillOwner(ref)) === undefined) {
return `There is no skill called ${ref}.`;
}
+ // And the Bot, for the reason the `mcp` branch gives: an administrator's grant naming one
+ // nobody has failed on the foreign key. Everybody else is asked below, through `agentOwner`.
+ if (!(await store.agentIsRegistered(agentId)))
+ return "There is no such Bot.";
return null;
}
diff --git a/server/tests/plugin-grants-validation.test.ts b/server/tests/plugin-grants-validation.test.ts
index 231501a8..30580cb2 100644
--- a/server/tests/plugin-grants-validation.test.ts
+++ b/server/tests/plugin-grants-validation.test.ts
@@ -17,6 +17,8 @@ function appWith(
servers: string[] = ["tool"],
/** The skills that exist, all of them the deployment's own. */
skills: string[] = [],
+ /** The Bots that exist, for the grant path's check that the grantee is real. */
+ bots: string[] = ["bot-1"],
) {
const store = {
serverExists: async (serverId: string) => {
@@ -27,6 +29,7 @@ function appWith(
calls.skillLookups?.push(slug);
return skills.includes(slug) ? null : undefined;
},
+ agentIsRegistered: async (agentId: string) => bots.includes(agentId),
grant: async (kind: unknown, ref: unknown, agentId: unknown) => {
calls.grants.push({ kind, ref, agentId });
return { ok: true };
@@ -448,3 +451,50 @@ describe("POST /api/plugins/skills", () => {
expect(calls.installs).toHaveLength(1);
});
});
+
+/**
+ * A GRANT NAMING A BOT NOBODY HAS.
+ *
+ * `plugin_grants.agent_id` is a foreign key, so a grant for a mistyped Bot id passed every check an
+ * administrator's `mcp` or `skill` grant makes, reached the insert and failed there: a 500 with no
+ * body, where each other refusal on this route is a 403 with a sentence. The `bot` kind already
+ * refuses it, as "There is no such Bot.".
+ */
+describe("POST /api/plugins/grants, for a Bot that does not exist", () => {
+ const grant = (body: unknown) => ({
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify(body),
+ });
+
+ test.each([
+ ["an app", { kind: "mcp", ref: "tool/SEND", agentId: "typo-bot" }],
+ ["a skill", { kind: "skill", ref: "triage", agentId: "typo-bot" }],
+ ])("refuses a grant of %s and never reaches the store", async (_n, body) => {
+ const calls = { grants: [] as unknown[], toolCalls: [] as unknown[] };
+ const response = await appWith(calls, ["tool"], ["triage"]).request(
+ "http://openbot.test/grants",
+ grant(body),
+ );
+
+ expect(response.status).toBe(403);
+ await expect(response.json()).resolves.toEqual({
+ error: "There is no such Bot.",
+ });
+ expect(calls.grants).toEqual([]);
+ });
+
+ test.each([
+ ["an app", { kind: "mcp", ref: "tool/SEND", agentId: "bot-1" }],
+ ["a skill", { kind: "skill", ref: "triage", agentId: "bot-1" }],
+ ])("grants %s to a Bot that is there, as before", async (_n, body) => {
+ const calls = { grants: [] as unknown[], toolCalls: [] as unknown[] };
+ const response = await appWith(calls, ["tool"], ["triage"]).request(
+ "http://openbot.test/grants",
+ grant(body),
+ );
+
+ expect(response.status).toBe(200);
+ expect(calls.grants).toEqual([body]);
+ });
+});
diff --git a/server/tests/skill-uninstall-grants.integration.test.ts b/server/tests/skill-uninstall-grants.integration.test.ts
index 82298454..562f4fd2 100644
--- a/server/tests/skill-uninstall-grants.integration.test.ts
+++ b/server/tests/skill-uninstall-grants.integration.test.ts
@@ -36,6 +36,8 @@ const personalSlug = `standup-${suite}`;
const keptSlug = `kept-${suite}`;
const deploymentSlug = `triage-${suite}`;
const unwrittenSlug = `unwritten-${suite}`;
+const botlessSlug = `botless-${suite}`;
+const missingBot = `agent_missing_${suite}`;
beforeAll(async () => {
for (const id of [alice, bob]) {
@@ -75,6 +77,7 @@ afterAll(async () => {
keptSlug,
deploymentSlug,
unwrittenSlug,
+ botlessSlug,
]),
);
await database
@@ -202,6 +205,30 @@ describe("a skill name written again after the skill was uninstalled", () => {
expect(await offered(asAdmin(), sharedBot)).toEqual([]);
});
+ /*
+ * The other half of a grant that could never do anything: the skill is there and the Bot is not.
+ * `plugin_grants.agent_id` is a foreign key, so an administrator's grant for a mistyped Bot id used
+ * to fail at the insert and answer 500 with no body. It is a refusal with a sentence now, and
+ * nothing is stored.
+ */
+ test("a skill cannot be granted to a Bot nobody has", async () => {
+ expect(
+ (await writeSkill(asAdmin(), botlessSlug, "Triage by severity.", true))
+ .status,
+ ).toBe(200);
+
+ const refused = await grant(asAdmin(), botlessSlug, missingBot);
+
+ expect(refused.status).toBe(403);
+ expect(await refused.json()).toEqual({ error: "There is no such Bot." });
+ expect(
+ await database
+ .select({ ref: pluginGrants.ref })
+ .from(pluginGrants)
+ .where(eq(pluginGrants.agentId, missingBot)),
+ ).toEqual([]);
+ });
+
test("uninstalling removes that skill's grants and no other", async () => {
const held = await database
.select({ ref: pluginGrants.ref, agentId: pluginGrants.agentId })