Skip to content

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

fix(mcp): prevent accidental loss of saved environment values

David Neyraa12d5de95fa4
diff --git a/server/routes/internal-mcp.test.ts b/server/routes/internal-mcp.test.ts
index 6d62ab1641..4df78c8d25 100644
--- a/server/routes/internal-mcp.test.ts
+++ b/server/routes/internal-mcp.test.ts
@@ -326,6 +326,22 @@ describe("internal mcp_server route, Full access", () => {
expect(body.dropped).toBeUndefined();
});
+ it("refuses a partial literal environment update without clearing saved credentials", async () => {
+ const probe = probeWith({ ok: true, tools: [] });
+ const { post, stored, ref } = fixture({ servers: { notes } });
+ const rejected = await post({ action: "update", name: "notes", env: { npm_config_cache: "/tmp/npm-cache" } });
+ expect(rejected.status).toBe(409);
+ expect(rejected.body.error).toContain("env NOTES_TOKEN");
+ expect(rejected.body.error).not.toContain("saved-token");
+ expect(stored("notes")).toMatchObject({ env: { NOTES_TOKEN: "saved-token" } });
+ expect(probe).not.toHaveBeenCalled();
+
+ const accepted = await post({ action: "update", name: "notes", env: { npm_config_cache: "/tmp/npm-cache", NOTES_TOKEN: ref("replacement") } });
+ expect(accepted.status).toBe(200);
+ expect(stored("notes")).toMatchObject({ env: { npm_config_cache: "/tmp/npm-cache" } });
+ expect(accepted.body.dropped).toBeUndefined();
+ });
+
it("a new address drops the sign-in only when there was one to drop", async () => {
probeWith({ ok: true, tools: [] });
const signedIn = fixture({ servers: { linear } });
@@ -883,6 +899,15 @@ describe("internal mcp_server route, below Full access", () => {
expect(JSON.stringify(remote.chat)).not.toMatch(/opaque-value-123|abc123/);
});
+ it("allows clearing saved credentials when the person approves the card", async () => {
+ probeWith({ ok: true, tools: [] });
+ const { post, card, respond, stored } = fixture({ full: false, servers: { notes } });
+ expect((await post({ action: "update", name: "notes", env: { npm_config_cache: "/tmp/npm-cache" } })).status).toBe(202);
+ expect(card()?.subtitle).toContain("env NOTES_TOKEN");
+ expect(await respond("allow")).toMatchObject({ status: 200, body: { result: { state: "applied" } } });
+ expect(stored("notes")).toMatchObject({ env: { npm_config_cache: "/tmp/npm-cache" } });
+ });
+
it("the card's targetHash is the hash of the display target, so a secret in the args or url cannot be guessed from it", async () => {
const value = "sk-live-hunter2-secret";
const keyed = { command: "npx", args: ["-y", "notes-mcp", "--api-key", value], enabled: false };
diff --git a/server/routes/internal-mcp.ts b/server/routes/internal-mcp.ts
index 715d04d769..317643d756 100644
--- a/server/routes/internal-mcp.ts
+++ b/server/routes/internal-mcp.ts
@@ -275,6 +275,11 @@ async function update(deps: Ctx, name: string, existing: StoredMcpServer, fields
const planned = planUpdate(deps, name, existing, fields);
if ("error" in planned) return planned;
const { dropped } = planned;
+ // A card can explicitly approve clearing values; a direct partial update cannot.
+ // Literal environment values can redirect a command, so keeping credentials is unsafe.
+ if (!isRemoteMcpServer(existing) && fields.env !== undefined && dropped.length && !deps.approved) {
+ return { status: 409, error: `Changing the environment of ${name} would clear saved values: ${dropped.join(", ")}. Send all listed values in the same update (use fresh secretRefs for secrets), or change it in Settings.` };
+ }
let server = planned.server;
let moved: Array<{ from: string; to: string }> = [];
if (!isRemoteMcpServer(server) && !isRemoteMcpServer(existing) && !sameRun(server, existing)) {