Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(mcp): preserve managed configuration behind credential guards

Abdurrahman Fakhrulcb956fa3d035
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 014c6802..a4e931b1 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -8,6 +8,7 @@ this is the *what*, and it is what Settings → Account → About shows as “Wh
**Fixed**
+- `mcp` preserve managed configuration behind credential guards
- `security` bind receipts to authority and pin browser artifacts
- `security` keep connected app bootstrap code static
- `security` protect public admission and OAuth registration trust
diff --git a/lib/host/path-credentials.ts b/lib/host/path-credentials.ts
index b6932fce..5a23d9e6 100644
--- a/lib/host/path-credentials.ts
+++ b/lib/host/path-credentials.ts
@@ -102,7 +102,7 @@ export function isCredentialPath(real: string, roots = credentialRoots()): boole
if (roots.some((root) => isUnderRoot(real, root))) return true;
const names = path.resolve(real).split(path.sep);
if (names.some((name) => name.startsWith(".env") && name !== ".env.example")) return true;
- if (names.some((name) => [".mcp.json", ".npmrc", ".pypirc", ".netrc"].includes(name))) return true;
+ if (names.some((name) => [".mcp.json", ".npmrc", ".pypirc", ".netrc"].some((credential) => name === credential || name.startsWith(credential + ".")))) return true;
const base = path.basename(real);
// Private keys land anywhere (heredoc dumps, deploy keys, downloaded service
// accounts). Their basename/extension is the reliable marker outside fixed ~/ paths.
@@ -120,6 +120,6 @@ export function looseCredentialExcludes(): string[] {
: PRIVATE_KEY_BASENAMES.flatMap((name) => [name, `*/${name}`]);
// *.pem has historically been a hard credential boundary, independent of the
// SENSITIVE_HOME escape hatch; keep that contract while extending id_* safely.
- return ["*.pem", ".env*", "*/.env*", ...[".mcp.json", ".npmrc", ".pypirc", ".netrc"].flatMap((name) => [name, `*/${name}`]), ...privateNames];
+ return ["*.pem", ".env*", "*/.env*", ...[".mcp.json", ".npmrc", ".pypirc", ".netrc"].flatMap((name) => [name, `*/${name}`, `${name}.*`, `*/${name}.*`]), ...privateNames];
}
diff --git a/lib/host/project-mcp-manage.test.ts b/lib/host/project-mcp-manage.test.ts
index 1807a1f5..d53216a3 100644
--- a/lib/host/project-mcp-manage.test.ts
+++ b/lib/host/project-mcp-manage.test.ts
@@ -6,6 +6,25 @@ vi.mock("@/lib/infra/connection-service", () => ({ directConnectionValues: async
? { endpoint: "https://site.batonly.site/mcp", accessToken: "batonly-private-value" }
: { endpoint: "https://example.test/mcp", accessToken: "private-value" } }));
let root = "";
+it("preserves private literal configuration only through the dedicated manager", async () => {
+ root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-mcp-literals-"));
+ vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
+ const file = path.join(root, ".mcp.json"), secret = "fixture-private-literal";
+ await fs.writeFile(file, JSON.stringify({ mcpServers: { private: { command: "node", env: { ACCESS_TOKEN: secret } } } }), { mode: 0o600 });
+ const { inspectProjectMcp, manageProjectMcp } = await import("./project-mcp-manage");
+ const inventory = await inspectProjectMcp(root);
+ expect(JSON.stringify(inventory)).not.toContain(secret);
+ await manageProjectMcp(root, { action: "upsert", server: "public", revision: inventory.revision, url: "https://example.test/mcp" });
+ expect(await fs.readFile(file, "utf8")).toContain(secret);
+ const { readFile } = await import("./fs");
+ await expect(readFile(file)).rejects.toThrow(/credential/);
+ const temporary = file + ".fixture.tmp"; await fs.writeFile(temporary, secret);
+ await expect(readFile(temporary)).rejects.toThrow(/credential/);
+ await fs.writeFile(file, '{"mcpServers":"' + secret);
+ await expect(inspectProjectMcp(root)).rejects.toThrow("invalid MCP manifest JSON");
+ await fs.unlink(file); await fs.symlink(temporary, file);
+ await expect(inspectProjectMcp(root)).rejects.toThrow("unsafe project MCP manifest");
+});
afterEach(async () => { vi.unstubAllEnvs(); if (root) await fs.rm(root, { recursive: true, force: true }); });
it("preserves existing servers, refuses stale revisions and stores only private connection references", async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-mcp-manage-"));
diff --git a/lib/host/project-mcp-manage.ts b/lib/host/project-mcp-manage.ts
index 3478fc65..0c6cb973 100644
--- a/lib/host/project-mcp-manage.ts
+++ b/lib/host/project-mcp-manage.ts
@@ -1,8 +1,9 @@
import path from "node:path";
-import { promises as fs } from "node:fs";
-import { readBoundedRegularFile } from "./bounded-read";
-import { resolveReadable } from "./paths";
-import { writeFileGuarded, sha256Text } from "./fs-api";
+import { constants, promises as fs } from "node:fs";
+import { randomUUID } from "node:crypto";
+import { resolveReadable, safeWritePath } from "./paths";
+import { sha256Text } from "./hash";
+import { descriptorPath, pinDirectory, readBoundedBytes } from "./fs-descriptors";
import { readProjectMcpServers, publicProjectMcpServers } from "./project-mcp-config";
import { normalizeMcpEndpoint } from "@/lib/infra/mcp-policy";
import { BUILT_IN_PLUGINS } from "@/lib/plugins/manifest";
@@ -11,15 +12,31 @@ import { directConnectionValues } from "@/lib/infra/connection-service";
import { withSecurityStoreLock } from "@/lib/security-store-lock";
const object = (v: unknown): v is Record<string, unknown> => Boolean(v && typeof v === "object" && !Array.isArray(v));
+// Only this dedicated manager accesses the fixed credential-bearing manifest basename.
+async function manifestText(file: string): Promise<string | null> {
+ const handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK).catch((error: NodeJS.ErrnoException) => { if (error.code === "ENOENT") return null; throw error; });
+ if (!handle) return null;
+ try {
+ const before = await handle.stat();
+ if (!before.isFile() || before.size > 64 * 1024) throw new Error("unsafe or oversized project MCP manifest");
+ const raw = (await readBoundedBytes(handle, 64 * 1024)).toString("utf8");
+ const after = await handle.stat();
+ if (after.size !== before.size || after.mtimeMs !== before.mtimeMs) throw new Error("MCP manifest changed while reading");
+ return raw;
+ } finally { await handle.close(); }
+}
async function manifest(projectPath: string) {
const file = path.join(projectPath, ".mcp.json");
const stat = await fs.lstat(file).catch((e: NodeJS.ErrnoException) => { if (e.code === "ENOENT") return null; throw e; });
if (!stat) return { file, data: { mcpServers: {} } as Record<string, unknown>, revision: "new" };
if (!stat.isFile() || stat.isSymbolicLink()) throw new Error("unsafe project MCP manifest");
- await resolveReadable(file);
- const raw = await readBoundedRegularFile(file, 64 * 1024);
- if (raw === null) throw new Error("MCP manifest exceeds 64 KiB");
- const data: unknown = JSON.parse(raw);
+ const held = await pinDirectory(await resolveReadable(projectPath), false);
+ let raw: string | null;
+ try { raw = await manifestText(`/proc/self/fd/${held.handle.fd}/.mcp.json`); await descriptorPath(held.handle, false); }
+ finally { await held.handle.close(); }
+ if (raw === null) throw new Error("MCP manifest disappeared; inspect before editing");
+ let data: unknown;
+ try { data = JSON.parse(raw); } catch { throw new Error("invalid MCP manifest JSON"); }
if (!object(data)) throw new Error("invalid MCP manifest");
if (data.mcpServers !== undefined && data.servers !== undefined) throw new Error("ambiguous MCP manifest; keep one servers key");
if (!object(data.mcpServers ?? data.servers ?? {})) throw new Error("invalid MCP servers");
@@ -67,7 +84,19 @@ export async function manageProjectMcp(projectPath: string, input: { action: "up
if (Object.keys(servers).length > 16) throw new Error("project MCP limit is 16 servers");
const content = JSON.stringify({ ...stored.data, [key]: servers }, null, 2) + "\n";
if (Buffer.byteLength(content) > 64 * 1024) throw new Error("MCP manifest exceeds 64 KiB");
- const result = await writeFileGuarded({ path: stored.file, content, ...(stored.revision === "new" ? {} : { expectedSha256: stored.revision }) });
+ const held = await pinDirectory(await safeWritePath(projectPath, true), true);
+ const destination = `/proc/self/fd/${held.handle.fd}/.mcp.json`, temporary = `${destination}.${randomUUID()}.tmp`;
+ let handle;
+ try {
+ await descriptorPath(held.handle, false);
+ const current = await manifestText(destination);
+ if ((current === null ? "new" : sha256Text(current)) !== stored.revision) throw new Error("MCP revision changed; inspect before editing");
+ handle = await fs.open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
+ await handle.writeFile(content, "utf8"); await handle.close(); handle = undefined;
+ await descriptorPath(held.handle, true);
+ await fs.rename(temporary, destination);
+ } finally { await handle?.close(); await fs.unlink(temporary).catch(() => undefined); await held.handle.close(); }
+ const result = { sha256: sha256Text(content) };
return { action: input.action, server: input.server, revision: result.sha256, next: input.action === "delete"
? "Removed only from the selected project; other projects and Integrations credentials are unchanged."
: "Installed only in the selected project; use project_mcp_tools for discovery evidence." };