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(security): block delegated writes to control plane

Abdurrahman Fakhrul0f7d9737e7a2
diff --git a/lib/host/delegated-write.test.ts b/lib/host/delegated-write.test.ts
new file mode 100644
index 00000000..86e68d0e
--- /dev/null
+++ b/lib/host/delegated-write.test.ts
@@ -0,0 +1,22 @@
+import os from "node:os";
+import path from "node:path";
+import { describe, expect, it } from "vitest";
+import { appDir } from "./path-credentials";
+import { assertDelegatedWritePath } from "./delegated-write";
+
+describe("delegated write control-plane guard", () => {
+ it("blocks the live MSO checkout and prospective children", async () => {
+ await expect(assertDelegatedWritePath(path.join(appDir(), "package.json"))).rejects.toThrow(/control-plane/);
+ await expect(assertDelegatedWritePath(path.join(appDir(), "future", "script.sh"))).rejects.toThrow(/control-plane/);
+ });
+ it("blocks Git control files in any project", async () => {
+ await expect(assertDelegatedWritePath(path.join(os.tmpdir(), "project", ".git", "config"))).rejects.toThrow(/control-plane/);
+ await expect(assertDelegatedWritePath(path.join(os.tmpdir(), "project", ".git", "hooks", "post-checkout"))).rejects.toThrow(/control-plane/);
+ });
+ it("blocks user-systemd unit inputs", async () => {
+ await expect(assertDelegatedWritePath(path.join(os.homedir(), ".config/systemd/user/evil.service"))).rejects.toThrow(/control-plane/);
+ });
+ it("allows ordinary project content", async () => {
+ await expect(assertDelegatedWritePath(path.join(os.tmpdir(), "project", "src", "index.ts"))).resolves.toBeUndefined();
+ });
+});
diff --git a/lib/host/delegated-write.ts b/lib/host/delegated-write.ts
new file mode 100644
index 00000000..fa2d2752
--- /dev/null
+++ b/lib/host/delegated-write.ts
@@ -0,0 +1,49 @@
+import { promises as fs } from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { HostError } from "./host-error";
+import { appDir } from "./path-credentials";
+import { isUnderRoot } from "./path-roots";
+
+function absolute(raw: string): string {
+ if (raw === "~") return os.homedir();
+ if (raw.startsWith("~/")) return path.join(os.homedir(), raw.slice(2));
+ return path.resolve(raw);
+}
+
+async function canonicalProspective(raw: string): Promise<string> {
+ const target = absolute(raw);
+ let probe = target;
+ for (;;) {
+ try {
+ const real = await fs.realpath(probe);
+ const remainder = path.relative(probe, target);
+ return remainder ? path.join(real, remainder) : real;
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code !== "ENOENT" && code !== "ENOTDIR") throw error;
+ const parent = path.dirname(probe);
+ if (parent === probe) return target;
+ probe = parent;
+ }
+ }
+}
+
+function containsGitControl(target: string): boolean {
+ return target.split(path.sep).filter(Boolean).includes(".git");
+}
+
+export async function assertDelegatedWritePath(raw: string): Promise<void> {
+ const target = await canonicalProspective(raw);
+ const app = appDir();
+ const systemdUser = path.join(os.homedir(), ".config", "systemd", "user");
+ if (
+ target === app ||
+ isUnderRoot(target, app) ||
+ containsGitControl(target) ||
+ target === systemdUser ||
+ isUnderRoot(target, systemdUser)
+ ) {
+ throw new HostError("write scope cannot modify executable control-plane inputs; exec authority required");
+ }
+}
diff --git a/lib/host/fs-api.ts b/lib/host/fs-api.ts
index d7cf20c6..2ae8849e 100644
--- a/lib/host/fs-api.ts
+++ b/lib/host/fs-api.ts
@@ -7,4 +7,5 @@ export { parseMultipart, boundaryFromContentType, UploadTooLargeError } from "./
export type { MultipartPart } from "./multipart";
export { sha256Text, utf8Bytes } from "./hash";
export { writeFileGuarded } from "./guarded-write";
+export { assertDelegatedWritePath } from "./delegated-write";
export { readFileBytes } from "./fs-read-bytes";
diff --git a/lib/mcp/tools-mutate.ts b/lib/mcp/tools-mutate.ts
index 3b3a8a44..39d9b683 100644
--- a/lib/mcp/tools-mutate.ts
+++ b/lib/mcp/tools-mutate.ts
@@ -1,11 +1,11 @@
import { sessionArtifactEnvironment } from "@/lib/agent/artifact-session";
import { cancelExecJob, getExecJob, runCommand, startExecJob } from "@/lib/host/exec-api";
-import { copy, makeDir, move, remove, writeFileGuarded } from "@/lib/host/fs-api";
+import { assertDelegatedWritePath, copy, makeDir, move, remove, writeFileGuarded } from "@/lib/host/fs-api";
import { resolveProjectHint, runProjectFunction } from "@/lib/host/projects-api";
import { importOpenAiProvidedFile } from "./openai-file-upload";
import { type McpTool, opt, PATH_P, S, str } from "./tool-kit";
import { requireWorkflowExecCwd, requireWorkflowMutationPath, requireWorkflowProjectTarget } from "./workflow-workspace-guard";
-// Write/exec tools carry audit descriptors; the dispatcher records the route-independent trail.
+async function requireDelegatedPath(context: Parameters<McpTool["run"]>[1], target: string) { if (context.scope !== "exec") await assertDelegatedWritePath(target); }
export const MUTATE_TOOLS: McpTool[] = [
{
name: "fs_write",
@@ -23,6 +23,7 @@ export const MUTATE_TOOLS: McpTool[] = [
run: async (a, context) => {
const target = str(a, "path");
await requireWorkflowMutationPath(context, target);
+ await requireDelegatedPath(context, target);
return { ok: true, ...(await writeFileGuarded({
path: target,
content: typeof a.content === "string" ? a.content : "",
@@ -62,6 +63,7 @@ export const MUTATE_TOOLS: McpTool[] = [
run: async (a, context) => {
const dest = str(a, "dest");
await requireWorkflowMutationPath(context, dest);
+ await requireDelegatedPath(context, dest);
return importOpenAiProvidedFile({
file: a.file, dest, filename: opt(a, "filename"),
conflict: opt(a, "conflict") as "error" | "rename" | "replace" | undefined,
@@ -77,7 +79,7 @@ export const MUTATE_TOOLS: McpTool[] = [
scope: "write",
annotations: { idempotentHint: true },
inputSchema: S(PATH_P, ["path"]),
- run: async (a, context) => { const target = str(a, "path"); await requireWorkflowMutationPath(context, target); await makeDir(target); return { ok: true, path: a.path }; },
+ run: async (a, context) => { const target = str(a, "path"); await requireWorkflowMutationPath(context, target); await requireDelegatedPath(context, target); await makeDir(target); return { ok: true, path: a.path }; },
},
{
name: "fs_move",
@@ -86,7 +88,7 @@ export const MUTATE_TOOLS: McpTool[] = [
description: "Move or rename a file or directory. Refuses when the source holds credential paths.",
scope: "write",
inputSchema: S({ from: { type: "string" }, to: { type: "string" } }, ["from", "to"]),
- run: async (a, context) => { const from = str(a, "from"), to = str(a, "to"); await requireWorkflowMutationPath(context, from); await requireWorkflowMutationPath(context, to); await move(from, to); return { ok: true }; },
+ run: async (a, context) => { const from = str(a, "from"), to = str(a, "to"); await requireWorkflowMutationPath(context, from); await requireWorkflowMutationPath(context, to); await requireDelegatedPath(context, from); await requireDelegatedPath(context, to); await move(from, to); return { ok: true }; },
},
{
name: "fs_copy",
@@ -95,7 +97,7 @@ export const MUTATE_TOOLS: McpTool[] = [
description: "Copy a file or directory. The cockpit's own secrets are skipped rather than duplicated.",
scope: "write",
inputSchema: S({ from: { type: "string" }, to: { type: "string" } }, ["from", "to"]),
- run: async (a, context) => { const from = str(a, "from"), to = str(a, "to"); await requireWorkflowMutationPath(context, to); await copy(from, to); return { ok: true }; },
+ run: async (a, context) => { const from = str(a, "from"), to = str(a, "to"); await requireWorkflowMutationPath(context, to); await requireDelegatedPath(context, to); await copy(from, to); return { ok: true }; },
},
{
name: "fs_delete",
@@ -105,9 +107,8 @@ export const MUTATE_TOOLS: McpTool[] = [
scope: "write",
annotations: { destructiveHint: true },
inputSchema: S(PATH_P, ["path"]),
- run: async (a, context) => { const target = str(a, "path"); await requireWorkflowMutationPath(context, target); await remove(target); return { ok: true, path: a.path }; },
+ run: async (a, context) => { const target = str(a, "path"); await requireWorkflowMutationPath(context, target); await requireDelegatedPath(context, target); await remove(target); return { ok: true, path: a.path }; },
},
-
{
name: "project_function_call",
limit: { key: "projects.function", max: 60, windowMs: 60_000 },