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 tool grant naming an app this deployment has not added (#616)

Hotragn Pettugani25c5e3d17dc2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 33d238f2..61b20f85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,16 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### A tool cannot be granted for an app this deployment has not added
+
+Granting a Bot a connector's tool checked only that the person asking was an administrator, so a
+grant naming an app that was never added was stored and then invisible — the page that reports a
+grant nothing advertises is built from the connector's own row, and there was none. Adding that app
+later put every such grant straight onto its Bots, with nobody having granted anything and nothing in
+the trail saying so. The grant is now refused, naming the app. Taking a grant away is unaffected, so
+a dead row an administrator can see is still one they can remove, and a tool a connector has stopped
+advertising can still be granted: what a vendor lists today is not what somebody decided yesterday.
+
### A coworker can be a file of its own
The example package declared every coworker in one `agents.yaml`, so adding one meant editing a file
diff --git a/server/src/plugins/routes.ts b/server/src/plugins/routes.ts
index fc0df725..3df356c3 100644
--- a/server/src/plugins/routes.ts
+++ b/server/src/plugins/routes.ts
@@ -2271,9 +2271,37 @@ export function createPluginRoutes(
const actor = skillActor(context);
if (kind === "mcp") {
- return actor.isAdmin
- ? null
- : "An administrator decides which Bots may reach a tool.";
+ if (!actor.isAdmin) {
+ return "An administrator decides which Bots may reach a tool.";
+ }
+ // Taking something away is always allowed: see the note on `intent`. It matters more here
+ // than anywhere else, because the rows this check exists to prevent are the same shape as the
+ // rows #572's migration had to delete, and an administrator has to be able to remove one by
+ // hand rather than wait for a migration.
+ if (intent === "revoke") return null;
+
+ /*
+ * A grant that could never do anything is refused rather than stored, which is the rule the
+ * `bot` branch below already states and this one did not follow.
+ *
+ * Nothing checked that the app existed. `store.grant` is a bare upsert, so a ref naming an
+ * app this deployment has not added was stored and then invisible: the surface that reports a
+ * grant nothing advertises is built per server row, and there was no row. #572 fixed the way
+ * these rows were MADE — a removal used to leave its grants behind — and added the migration
+ * that deleted the ones already there. This is the other door into the same room. Add the app
+ * afterwards and the id is the same, the action names are the same, and every one of those
+ * grants resolves, with nobody having granted anything and no row in the trail saying so.
+ *
+ * THE SERVER HALF ONLY. A grant naming a tool the server has stopped advertising is a
+ * supported state, held and not offered, because what a vendor lists today is not what
+ * somebody decided yesterday — checking the tool here would refuse a re-grant of exactly the
+ * tool an administrator is trying to restore. A grant naming no app at all is not that state.
+ */
+ const [serverId] = ref.split("/");
+ 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.`;
+ }
+ return null;
}
if (kind === "bot") {
@@ -2367,16 +2395,23 @@ export function createPluginRoutes(
400,
);
}
+ /*
+ * Trimmed before it is checked and before it is stored, the way the DELETE twin below already
+ * does. Without this a padded ref refuses with the padding quoted back (`" app" is not an app`),
+ * and a grant that did pass would be stored under a ref no revoke could match.
+ */
+ const grantRef = body.ref.trim();
+ const grantAgentId = body.agentId.trim();
const refusal = await enablementRefusal(
context,
kind,
- body.ref,
- body.agentId,
+ grantRef,
+ grantAgentId,
"grant",
);
if (refusal) return context.json({ error: refusal }, 403);
- await store.grant(kind, body.ref, body.agentId, actorEmail(context));
+ await store.grant(kind, grantRef, grantAgentId, actorEmail(context));
return context.json({ ok: true });
});
diff --git a/server/src/plugins/store.ts b/server/src/plugins/store.ts
index ecdc6d16..bbd73ba3 100644
--- a/server/src/plugins/store.ts
+++ b/server/src/plugins/store.ts
@@ -4253,6 +4253,28 @@ export function createPluginStore(options: PluginStoreOptions) {
return row !== undefined;
},
+ /**
+ * Whether this deployment has an app by this id.
+ *
+ * The narrowest question a caller can ask about a server, and deliberately not `listServers`,
+ * which materialises every tool and every grant in the deployment to answer. One row, one
+ * column, one limit — this runs on the grant path, which is a person waiting on a switch.
+ *
+ * Existence only. Whether the app currently ADVERTISES a given tool is a different question and
+ * is not asked here: a grant naming a tool a server has stopped offering is a supported state
+ * ({@link GrantOnWithdrawnTool}), held and not offered, because what a vendor advertises today
+ * is not what somebody decided yesterday. A grant naming no app at all is not that state.
+ */
+ async serverExists(serverId: string): Promise<boolean> {
+ if (!serverId) return false;
+ const [row] = await database
+ .select({ id: mcpServers.id })
+ .from(mcpServers)
+ .where(eq(mcpServers.id, serverId))
+ .limit(1);
+ return row !== undefined;
+ },
+
async agentOwner(agentId: string): Promise<string | null | undefined> {
const [row] = await database
.select({ ownerUserId: agentProfiles.ownerUserId })
diff --git a/server/tests/plugin-grants-validation.test.ts b/server/tests/plugin-grants-validation.test.ts
index e3fbe3f0..3760c5a2 100644
--- a/server/tests/plugin-grants-validation.test.ts
+++ b/server/tests/plugin-grants-validation.test.ts
@@ -5,12 +5,21 @@ import type { BotAccessCheck } from "../src/plugins/routes";
import { createPluginRoutes } from "../src/plugins/routes";
import type { PluginStore } from "../src/plugins/store";
-function appWith(calls: {
- grants: unknown[];
- toolCalls: unknown[];
- revokes?: unknown[];
-}) {
+function appWith(
+ calls: {
+ grants: unknown[];
+ toolCalls: unknown[];
+ revokes?: unknown[];
+ serverLookups?: unknown[];
+ },
+ /** The apps this deployment has added, for the grant path's existence check. */
+ servers: string[] = ["tool"],
+) {
const store = {
+ serverExists: async (serverId: string) => {
+ calls.serverLookups?.push(serverId);
+ return servers.includes(serverId);
+ },
grant: async (kind: unknown, ref: unknown, agentId: unknown) => {
calls.grants.push({ kind, ref, agentId });
return { ok: true };
@@ -72,6 +81,131 @@ describe("POST /api/plugins/grants", () => {
});
});
+/**
+ * A GRANT NAMING NO APP IS A GRANT THAT COULD NEVER DO ANYTHING.
+ *
+ * `store.grant` is a bare upsert and the mcp branch of `enablementRefusal` checked only the role, so
+ * a ref naming an app this deployment had not added was stored and then invisible — the surface that
+ * reports a grant nothing advertises is built per server row, and there was no row. #572 closed the
+ * way these rows were MADE, by taking an app's grants when the app is removed, and its migration
+ * deleted the ones already there. This is the other door into the same room: add the app afterwards
+ * and the id is the same, the action names are the same, and every such grant resolves, with nobody
+ * having granted anything and no row in the trail saying so.
+ */
+describe("POST /api/plugins/grants, for an app this deployment does not have", () => {
+ test("refuses the grant and never reaches the store", async () => {
+ const calls = {
+ grants: [] as unknown[],
+ toolCalls: [] as unknown[],
+ serverLookups: [] as unknown[],
+ };
+ const response = await appWith(calls, ["added-app"]).request(
+ "http://openbot.test/grants",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ kind: "mcp",
+ ref: "missing-app/SEND_MESSAGE",
+ agentId: "bot-1",
+ }),
+ },
+ );
+
+ expect(response.status).toBe(403);
+ await expect(response.json()).resolves.toEqual({
+ error:
+ "missing-app 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.",
+ });
+ expect(calls.grants).toEqual([]);
+ // The server half, not the whole ref: the tool is not what is being looked up.
+ expect(calls.serverLookups).toEqual(["missing-app"]);
+ });
+
+ test("an app that is here is granted as before", async () => {
+ const calls = {
+ grants: [] as unknown[],
+ toolCalls: [] as unknown[],
+ serverLookups: [] as unknown[],
+ };
+ const response = await appWith(calls, ["added-app"]).request(
+ "http://openbot.test/grants",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ kind: "mcp",
+ ref: "added-app/SEND_MESSAGE",
+ agentId: "bot-1",
+ }),
+ },
+ );
+
+ expect(response.status).toBe(200);
+ expect(calls.grants).toEqual([
+ { kind: "mcp", ref: "added-app/SEND_MESSAGE", agentId: "bot-1" },
+ ]);
+ });
+
+ /*
+ * THE SERVER HALF ONLY, and this is the test that says so.
+ *
+ * A grant naming a tool the server has stopped advertising is a supported state — held and not
+ * offered, because what a vendor lists today is not what somebody decided yesterday. Checking the
+ * tool here would refuse a re-grant of exactly the tool an administrator is trying to restore.
+ */
+ test("a tool the app no longer advertises is still grantable", async () => {
+ const calls = {
+ grants: [] as unknown[],
+ toolCalls: [] as unknown[],
+ serverLookups: [] as unknown[],
+ };
+ const response = await appWith(calls, ["added-app"]).request(
+ "http://openbot.test/grants",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ kind: "mcp",
+ ref: "added-app/A_TOOL_IT_WITHDREW",
+ agentId: "bot-1",
+ }),
+ },
+ );
+
+ expect(response.status).toBe(200);
+ expect(calls.grants).toHaveLength(1);
+ });
+
+ /*
+ * TAKING SOMETHING AWAY IS STILL ALWAYS ALLOWED, which matters more here than anywhere else.
+ *
+ * The rows this check exists to prevent are the same shape as the rows #572's migration had to
+ * delete. Applying the check to a revoke would mean the reason a dead row is wrong is the reason
+ * it can never be removed, and an administrator looking at one in the UI would have to wait for
+ * somebody to write another migration.
+ */
+ test("a grant naming no app can still be revoked by hand", async () => {
+ const calls = {
+ grants: [] as unknown[],
+ toolCalls: [] as unknown[],
+ revokes: [] as unknown[],
+ serverLookups: [] as unknown[],
+ };
+ const response = await appWith(calls, ["added-app"]).request(
+ "http://openbot.test/grants?kind=mcp&ref=missing-app%2FSEND_MESSAGE&agentId=bot-1",
+ { method: "DELETE" },
+ );
+
+ expect(response.status).toBe(200);
+ expect(calls.revokes).toEqual([
+ { kind: "mcp", ref: "missing-app/SEND_MESSAGE", agentId: "bot-1" },
+ ]);
+ // Not even asked: a revoke has nothing to check.
+ expect(calls.serverLookups).toEqual([]);
+ });
+});
+
describe("DELETE /api/plugins/grants", () => {
/**
* Query params are always strings, so truthiness is not enough.