Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(mcp): reject a pane record whose paneId is not its own filename

Jack Rudenkobc0bebb04cd9
diff --git a/packages/cli/src/pane/pane-launch.ts b/packages/cli/src/pane/pane-launch.ts
index c0fd82ae..5bf6f5e4 100644
--- a/packages/cli/src/pane/pane-launch.ts
+++ b/packages/cli/src/pane/pane-launch.ts
@@ -25,7 +25,7 @@ import { findMagmuxBinaryOrNull } from "../launcher/magmux-binary.js";
import { STRIPPED_CHILD_VARS } from "../launcher/magmux-wrapper.js";
import { resolveClaudishSpawn } from "../spawn-claudish.js";
import { isPaneShellManagedKey } from "./child-env.js";
-import { type WatcherArgs, sockPathOf, watcherArgv } from "./process-identity.js";
+import { type WatcherArgs, isValidPaneId, sockPathOf, watcherArgv } from "./process-identity.js";
/* ───────────────────────────── socket root ───────────────────────────── */
@@ -89,11 +89,7 @@ export function ensureSockRoot(given: string = sockRootFor()): string {
/** This process's start time in ms, base 36: with the pid it makes a reused owner pid detectable. */
export const OWNER_START36 = Math.round(Date.now() - process.uptime() * 1000).toString(36);
-const ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
-
-export function isValidPaneId(id: string): boolean {
- return ID_RE.test(id) && !/^\d+$/.test(id);
-}
+export { isValidPaneId };
/** `c<pid>-<ownerStart36>-<kind><label>-<6 hex>`, at most 40 characters. */
export function mintPaneId(kind: "t" | "s", label: string): string {
diff --git a/packages/cli/src/pane/pane-registry.test.ts b/packages/cli/src/pane/pane-registry.test.ts
index c0f4c77b..6dcf11a1 100644
--- a/packages/cli/src/pane/pane-registry.test.ts
+++ b/packages/cli/src/pane/pane-registry.test.ts
@@ -18,15 +18,25 @@ import {
livePaneCount,
ownerStartOfSelf,
readRecord,
+ readRecords,
reapAllPanes,
releasePaneReservations,
+ removePaneFiles,
reservePanes,
reservedPaneCount,
sweepOrphanPanes,
writeRecord,
} from "./pane-registry.js";
import { startPaneSession } from "./pane-session.js";
-import { formatGroupFile, isPaneWatcherCommand, readProcessTable } from "./process-identity.js";
+import {
+ formatGroupFile,
+ isPaneRecordPath,
+ isPaneSockPath,
+ isPaneWatcherCommand,
+ readProcessTable,
+ recordPathOf,
+ sockPathOf,
+} from "./process-identity.js";
import {
MAGMUX,
NO_MAGMUX_MESSAGE,
@@ -486,6 +496,45 @@ describe("records", () => {
JSON.parse(readFileSync(join(t.sockRoot, "panes", `${rec.paneId}.json`), "utf8")).paneId
).toBe(rec.paneId);
});
+ test("R3-M2: a record is trusted only when its paneId is valid and names its own file", () => {
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ const victim = join(t.tmp, "victim.json");
+ writeFileSync(victim, "{}");
+ const base = {
+ ownerPid: 1,
+ ownerStart: "x",
+ watcherPid: null,
+ magmuxPid: null,
+ panePid: null,
+ sessionUuid: "u",
+ sockPath: "/s",
+ launcherDir: "/l",
+ turnDir: "/t",
+ createdAt: "now",
+ };
+ // a planted record whose paneId climbs out of the root, and one naming another file
+ const hostile = `../../${t.tmp.split("/").pop()}/victim`;
+ writeFileSync(
+ join(root, "panes", "c1-zz-tbad-000001.json"),
+ JSON.stringify({ ...base, paneId: hostile })
+ );
+ writeFileSync(
+ join(root, "panes", "c1-zz-tbad-000002.json"),
+ JSON.stringify({ ...base, paneId: "c1-zz-tother-000003" })
+ );
+ expect(readRecord(root, "c1-zz-tbad-000001")).toBeNull();
+ expect(readRecord(root, "c1-zz-tbad-000002")).toBeNull();
+ expect(readRecords(root)).toEqual([]);
+ // the path predicates compare against the root, never a path against itself
+ expect(isPaneRecordPath(root, recordPathOf(root, hostile), hostile)).toBe(false);
+ expect(isPaneSockPath(root, sockPathOf(root, "../x"), "../x")).toBe(false);
+ expect(isPaneRecordPath(root, recordPathOf(root, "c1-ok-t-000004"), "c1-ok-t-000004")).toBe(
+ true
+ );
+ removePaneFiles(root, { paneId: hostile, sockPath: "/s", launcherDir: "", turnDir: "" });
+ expect(existsSync(victim)).toBe(true);
+ });
});
describe("reservations", () => {
diff --git a/packages/cli/src/pane/pane-registry.ts b/packages/cli/src/pane/pane-registry.ts
index 6d202c16..644e1b21 100644
--- a/packages/cli/src/pane/pane-registry.ts
+++ b/packages/cli/src/pane/pane-registry.ts
@@ -40,6 +40,7 @@ import {
isPaneRecordPath,
isPaneSockPath,
isPaneWatcherCommand,
+ isValidPaneId,
mergeSnapshots,
parseGroupFile,
readProcessTable,
@@ -107,8 +108,9 @@ export function writeRecord(root: string, rec: PaneRecord): void {
}
export function readRecord(root: string, paneId: string): PaneRecord | null {
+ if (!isValidPaneId(paneId)) return null;
try {
- return validRecord(JSON.parse(readFileSync(recordPathOf(root, paneId), "utf8")));
+ return validRecord(JSON.parse(readFileSync(recordPathOf(root, paneId), "utf8")), paneId);
} catch {
return null;
}
@@ -127,10 +129,12 @@ export function deleteRecord(root: string, paneId: string): void {
}
}
-function validRecord(v: unknown): PaneRecord | null {
+/** A record is trusted only when its pane id is a valid id AND the file it was read from (R3-M2). */
+function validRecord(v: unknown, stem: string): PaneRecord | null {
if (!v || typeof v !== "object") return null;
const r = v as Record<string, unknown>;
if (typeof r.paneId !== "string" || typeof r.ownerPid !== "number") return null;
+ if (!isValidPaneId(r.paneId) || r.paneId !== stem) return null;
if (typeof r.sockPath !== "string" || typeof r.launcherDir !== "string") return null;
return {
paneId: r.paneId,
diff --git a/packages/cli/src/pane/process-identity.ts b/packages/cli/src/pane/process-identity.ts
index 8282bb48..8a487032 100644
--- a/packages/cli/src/pane/process-identity.ts
+++ b/packages/cli/src/pane/process-identity.ts
@@ -225,6 +225,13 @@ export function isLaunchDirPath(root: string, p: string): boolean {
}
}
+const PANE_ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
+
+/** A pane id is one path component: `[A-Za-z0-9_-]`, at most 64, never all digits. */
+export function isValidPaneId(id: string): boolean {
+ return PANE_ID_RE.test(id) && !/^\d+$/.test(id);
+}
+
export function sockPathOf(root: string, paneId: string): string {
return join(root, `magmux-${paneId}.sock`);
}
@@ -233,12 +240,19 @@ export function recordPathOf(root: string, paneId: string): string {
return join(root, "panes", `${paneId}.json`);
}
+/**
+ * The checks below compare a path against the ROOT and a validated pane id, never a
+ * path against itself: a pane id read from a record is data, and `join` would turn a
+ * `../` in it into a path outside the root.
+ */
export function isPaneSockPath(root: string, p: string, paneId: string): boolean {
- return p === sockPathOf(root, paneId);
+ return isValidPaneId(paneId) && dirname(p) === root && basename(p) === `magmux-${paneId}.sock`;
}
export function isPaneRecordPath(root: string, p: string, paneId: string): boolean {
- return p === recordPathOf(root, paneId);
+ return (
+ isValidPaneId(paneId) && dirname(p) === join(root, "panes") && basename(p) === `${paneId}.json`
+ );
}
/* ───────────────────────────── the watcher, as shell ───────────────────────────── */