Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mcp): keep replacement personal keys out of shared headers

Sebastian B4bec110a6d60
diff --git a/server/cloud-team-gateway-mcp.e2e.test.ts b/server/cloud-team-gateway-mcp.e2e.test.ts
index 75f30844bc..a32c07730b 100644
--- a/server/cloud-team-gateway-mcp.e2e.test.ts
+++ b/server/cloud-team-gateway-mcp.e2e.test.ts
@@ -131,8 +131,8 @@ it("lets a member's turn list and call the gateway's tool, with both values sent
}, hostTimeout(120_000));
it("keeps a one-key server Only for me: the admin's own chat gets it with their key, a member's chat doesn't", async () => {
- const own = "Bearer ben-own-key-5521";
- const mail = await startFakeHttpMcp({ tools: [{ name: "read_inbox" }], requireHeader: { name: "Authorization", value: own } });
+ let own = "Bearer ben-own-key-5521";
+ const mail = await startFakeHttpMcp({ tools: [{ name: "read_inbox" }], acceptBearer: (authorization) => authorization === own });
try {
const shared = { type: "http", url: mail.url, headers: { Authorization: "" }, enabled: false };
expect((await as(admin, "POST", "/api/mcp/servers", { name: "mail", ...shared })).status).toBe(201);
@@ -144,6 +144,16 @@ it("keeps a one-key server Only for me: the admin's own chat gets it with their
expect((await as(admin, "GET", "/api/team/my-connections")).body.mcp).toContainEqual(expect.objectContaining({ name: "mail", connected: true, how: "key" }));
expect((await as(admin, "POST", "/api/mcp/servers/mail/test")).body).toMatchObject({ ok: true });
expect((await as(admin, "POST", "/api/mcp/servers/mail/test")).body).toMatchObject({ ok: true });
+ for (const replacement of ["Bearer edited-own-key-6612", "Bearer fixed-own-key-7734"]) {
+ own = replacement;
+ expect((await as(admin, "PUT", "/api/mcp/servers/mail", shared)).status).toBe(200);
+ expect((await as(admin, "PUT", "/api/team/my-connections/mcp/mail/key", { value: replacement })).body).toEqual({ ok: true });
+ expect(saved().mcpServers.mail.headers).toEqual({ Authorization: "" });
+ const seen = mail.seenHeaders.length;
+ expect((await as(admin, "POST", "/api/mcp/servers/mail/test")).body).toMatchObject({ ok: true });
+ expect(mail.seenHeaders.length).toBeGreaterThan(seen);
+ expect(mail.seenHeaders.slice(seen).every((headers) => headers.authorization === replacement)).toBe(true);
+ }
const otherAdmin = (await team.pair({ email: "[email protected]", role: "admin" })).token!;
expect((await as(otherAdmin, "POST", "/api/mcp/servers/mail/test")).body).toMatchObject({ ok: false });
expect((await as(admin, "PATCH", "/api/mcp/servers/mail", { enabled: true })).status).toBe(200);
@@ -154,7 +164,7 @@ it("keeps a one-key server Only for me: the admin's own chat gets it with their
expect(mail.calls).toHaveLength(1);
const adas = await turn(ada, "Check the inbox.", [{ name: "read_inbox", server: "mail", input: {} }], "no mail MCP server was launched for this turn");
expect(Object.keys(adas.mcpConfig.mcpServers)).not.toContain("mail");
- expect(JSON.stringify(adas)).not.toContain("ben-own-key-5521");
+ expect(JSON.stringify(adas)).not.toContain(own);
expect(mail.calls).toHaveLength(1);
expect((await as(admin, "PUT", "/api/team/connection-scopes", { kind: "mcp", id: "mail", scope: "team" })).status).toBe(200);
const seen = mail.seenHeaders.length;
diff --git a/src/components/McpServersPanel.add.test.ts b/src/components/McpServersPanel.add.test.ts
index 1b6f404108..f8a6d753aa 100644
--- a/src/components/McpServersPanel.add.test.ts
+++ b/src/components/McpServersPanel.add.test.ts
@@ -10,6 +10,8 @@ const fixture = vi.hoisted(() => ({
config: null as null | { team?: { id: string; name: string } },
servers: [] as Array<Record<string, unknown>>,
sharedHeaders: {} as Record<string, Record<string, string>>,
+ personalMcp: [] as string[],
+ personalKeys: {} as Record<string, string>,
test: { ok: true, tools: [{ name: "a" }, { name: "b" }], total: 12 } as Record<string, unknown>,
}));
vi.mock("@/state/store", () => ({
@@ -51,11 +53,14 @@ beforeEach(async () => {
fixture.config = null;
fixture.servers = [];
fixture.sharedHeaders = {};
+ fixture.personalMcp = [];
+ fixture.personalKeys = {};
fixture.test = { ok: true, tools: [{ name: "a" }, { name: "b" }], total: 12 };
fixture.api.mockImplementation(async (path: string, init?: RequestInit) => {
const method = init?.method ?? "GET";
const body = init?.body ? JSON.parse(String(init.body)) : undefined;
if (method === "GET" && path === "/api/mcp/servers") return { servers: fixture.servers };
+ if (method === "GET" && path === "/api/team/my-connections") return { mcp: fixture.personalMcp.map((name) => ({ name })) };
if (method === "POST" && path === "/api/mcp/servers") {
fixture.sharedHeaders[body.name] = body.headers ?? {};
fixture.servers = [...fixture.servers, { name: body.name, type: body.type, url: body.url, headerKeys: Object.keys(body.headers ?? {}), enabled: false }];
@@ -73,7 +78,14 @@ beforeEach(async () => {
fixture.servers = fixture.servers.map((server) => server.name === name ? { ...server, headerKeys: Object.keys(body.headers ?? {}), enabled: body.enabled } : server);
return { servers: fixture.servers };
}
- if (method === "PUT") return path.includes("connection-scopes") ? { scopes: { apps: [], mcp: [body.id] } } : { ok: true };
+ if (method === "PUT" && path === "/api/team/connection-scopes") {
+ fixture.personalMcp = [body.id];
+ return { scopes: { apps: [], mcp: fixture.personalMcp } };
+ }
+ if (method === "PUT" && path.startsWith("/api/team/my-connections/mcp/")) {
+ fixture.personalKeys[decodeURIComponent(path.split("/").at(-2)!)] = body.value;
+ return { ok: true };
+ }
throw new Error(`unexpected ${method} ${path}`);
});
host = document.createElement("div");
@@ -258,6 +270,91 @@ describe("Adding an MCP server on a Team Cloud", () => {
expect(host.textContent).toContain("Connected: 12 tools");
});
+ it.each([
+ ["edit", "Authorization"], ["edit", "X-Api-Key"],
+ ["fix", "Authorization"], ["fix", "X-Api-Key"],
+ ])("keeps a replacement personal key private on %s with %s", async (action, header) => {
+ await click("Add server");
+ await type("textarea[aria-label='Server link or setup command']", `claude mcp add --transport http mail https://mail.example.com/mcp -H "${header}: Bearer $MAIL_KEY"`);
+ await type(`input[aria-label='${header}']`, "mail-key-12345");
+ await act(async () => host.querySelector<HTMLInputElement>("[data-mcp-team-scope] input[type=checkbox]")!.click());
+ if (action === "fix") fixture.test = { ok: false, reason: "keys", status: 401 };
+ await click("Add");
+ fixture.api.mockClear();
+ if (action === "fix") await click("Fix the keys");
+ else await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Edit mail']")!.click());
+ await type(`input[aria-label='${header}']`, "Bearer replacement-key-67890");
+ fixture.test = { ok: true, tools: [{ name: "read_inbox" }] };
+ await click("Save");
+
+ expect(calls("PUT", "/api/mcp/servers/mail")[0]?.body.headers).toEqual({ [header]: "" });
+ expect(calls("PUT", "/api/team/my-connections/mcp/mail/key")).toEqual([
+ { url: "/api/team/my-connections/mcp/mail/key", body: { value: "Bearer replacement-key-67890" } },
+ ]);
+ expect(fixture.sharedHeaders.mail).toEqual({ [header]: "" });
+ expect(fixture.personalKeys.mail).toBe("Bearer replacement-key-67890");
+ const writes = fixture.api.mock.calls.map(([path]) => path);
+ expect(writes.indexOf("/api/team/my-connections/mcp/mail/key")).toBeLessThan(writes.indexOf("/api/mcp/servers/mail/test"));
+ expect(calls("POST", "/api/mcp/servers/mail/test")).toHaveLength(1);
+ expect(host.textContent).toContain("Connected: 1 tool");
+ expect(host.querySelector("[data-mcp-editor]")).toBeNull();
+ });
+
+ it("keeps a saved personal key when an edit leaves its value blank", async () => {
+ fixture.servers = [{ name: "mail", type: "http", url: "https://mail.example.com/mcp", headerKeys: ["Authorization"], enabled: true }];
+ fixture.sharedHeaders.mail = { Authorization: "" };
+ fixture.personalMcp = ["mail"];
+ fixture.personalKeys.mail = "Bearer kept-key-12345";
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Refresh MCP servers']")!.click());
+ await settle();
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Edit mail']")!.click());
+ await click("Save");
+
+ expect(calls("PUT", "/api/mcp/servers/mail")[0]?.body.headers).toEqual({ Authorization: "" });
+ expect(calls("PUT", "/api/team/my-connections/mcp/mail/key")).toEqual([]);
+ expect(fixture.personalKeys.mail).toBe("Bearer kept-key-12345");
+ });
+
+ it("still writes replacement team keys to shared headers", async () => {
+ await click("Add server");
+ await type("textarea[aria-label='Server link or setup command']", 'claude mcp add --transport http mail https://mail.example.com/mcp -H "Authorization: Bearer $MAIL_KEY"');
+ await type("input[aria-label='Authorization']", "team-key-12345");
+ await click("Add");
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Edit mail']")!.click());
+ await type("input[aria-label='Authorization']", "Bearer replacement-team-key");
+ await click("Save");
+
+ expect(calls("PUT", "/api/mcp/servers/mail")[0]?.body.headers).toEqual({ Authorization: "Bearer replacement-team-key" });
+ expect(fixture.sharedHeaders.mail).toEqual({ Authorization: "Bearer replacement-team-key" });
+ expect(calls("PUT", "/api/team/my-connections/mcp/mail/key")).toEqual([]);
+ });
+
+ it.each(["/api/team/my-connections", "/api/team/my-connections/mcp/mail/key"])(
+ "does not share a replacement key or test the connection when %s fails", async (failedPath) => {
+ fixture.servers = [{ name: "mail", type: "http", url: "https://mail.example.com/mcp", headerKeys: ["Authorization"], enabled: true }];
+ fixture.sharedHeaders.mail = { Authorization: "" };
+ fixture.personalMcp = ["mail"];
+ fixture.personalKeys.mail = "Bearer kept-key-12345";
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Refresh MCP servers']")!.click());
+ await settle();
+ const previous = fixture.api.getMockImplementation()!;
+ fixture.api.mockImplementation(async (path: string, init?: RequestInit) => {
+ if (path === failedPath) throw new Error("Personal connection unavailable.");
+ return previous(path, init);
+ });
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Edit mail']")!.click());
+ await type("input[aria-label='Authorization']", "Bearer replacement-key-67890");
+ await click("Save");
+
+ expect(fixture.sharedHeaders.mail).toEqual({ Authorization: "" });
+ expect(fixture.personalKeys.mail).toBe("Bearer kept-key-12345");
+ expect(calls("PUT", "/api/mcp/servers/mail").every(({ body }) => body.headers.Authorization === "")).toBe(true);
+ expect(calls("POST", "/api/mcp/servers/mail/test")).toEqual([]);
+ expect(host.textContent).toContain("Personal connection unavailable.");
+ expect(host.querySelector("[data-mcp-editor]")).not.toBeNull();
+ },
+ );
+
it.each(["/api/team/connection-scopes", "/api/team/my-connections/mcp/mail/key"])(
"keeps Only for me disabled and never saves the shared key when %s fails", async (failedPath) => {
const previous = fixture.api.getMockImplementation()!;
diff --git a/src/components/McpServersPanel.tsx b/src/components/McpServersPanel.tsx
index 864129255f..a439ea4b64 100644
--- a/src/components/McpServersPanel.tsx
+++ b/src/components/McpServersPanel.tsx
@@ -217,7 +217,19 @@ export function McpServersPanel({ embedded = false, whopCard = false, hideWhop =
setError(null);
setNotice(null);
try {
- listed((await api(`/api/mcp/servers/${encodeURIComponent(item.name)}`, { method: "PUT", body: JSON.stringify({ ...item.body, enabled: existing.enabled }) })).servers);
+ let body = item.body;
+ let ownKey: string | undefined;
+ if (store.config?.team && isRemoteMcpListing(existing) && !existing.oauth) {
+ const connections = await api("/api/team/my-connections");
+ if (connections.mcp.some((connection: { name: string }) => connection.name === item.name)) {
+ const headers = (body.headers ?? {}) as Record<string, string | true>;
+ const value = Object.values(headers)[0];
+ ownKey = typeof value === "string" ? value : undefined;
+ body = { ...body, headers: Object.fromEntries(Object.keys(headers).map((name) => [name, ""])) };
+ }
+ }
+ listed((await api(`/api/mcp/servers/${encodeURIComponent(item.name)}`, { method: "PUT", body: JSON.stringify({ ...body, enabled: existing.enabled }) })).servers);
+ if (ownKey) await api(`/api/team/my-connections/mcp/${encodeURIComponent(item.name)}/key`, { method: "PUT", body: JSON.stringify({ value: ownKey }) });
closeEditor();
} catch (cause) {
setError(cause instanceof Error ? cause.message : String(cause));