Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(scope): apply activation scope to desktop.control session reads

koeria4afca6934ef2
diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.ts
index b55aad147..40099f07e 100644
--- a/apps/desktop/electron/main/plugin-runtime.ts
+++ b/apps/desktop/electron/main/plugin-runtime.ts
@@ -341,6 +341,12 @@ export type PluginHostServices = {
* visible workspace -- for a panel call or an unknown session.
*/
getWorkspacePathForSession?: (sessionId: string) => string | null;
+ /**
+ * Whether a plugin's activation scope covers `projectPath` — the same
+ * project-aware check the UI surfaces apply (#2377). Optional so a headless
+ * test host without scope records keeps every plugin global.
+ */
+ pluginActiveInProject?: (pluginId: string, projectPath: string | null | undefined) => boolean;
readRemoteWorkspaceFiles?: PluginRemoteFilesRead;
/** The set of `contributes.agentExtensions` modules changed (load/unload). */
agentExtensionsChanged?: () => void;
@@ -575,6 +581,27 @@ export type PluginOAuthProvider = {
};
/** Host APIs a plugin process may reach. Anything else does not exist (spec 04 §2). */
+// #2377: desktop.control session reads that reveal which sessions exist are
+// activation-scope boundaries, like `session.getActive` and the #1754 event
+// fan-out. `session/list` is filtered to in-scope sessions; the targeted reads
+// are refused when the target session belongs to an out-of-scope project.
+const SESSION_LIST_DESKTOP_OPERATION = "session/list";
+const SESSION_READ_DESKTOP_OPERATIONS = new Set([
+ SESSION_LIST_DESKTOP_OPERATION,
+ "session/get",
+ "session/listMessages",
+ "session/listRevisions",
+]);
+
+/** The single session a targeted session read names, when its args carry one. */
+function desktopSessionTargetId(args: readonly unknown[]): string | undefined {
+ const first = args[0];
+ if (!first || typeof first !== "object" || Array.isArray(first)) return undefined;
+ const record = first as Record<string, unknown>;
+ const raw = record.sessionId ?? record.id;
+ return typeof raw === "string" ? raw : undefined;
+}
+
const HOST_API_ALLOWLIST = new Set([
"app.getVersion",
"app.getLocale",
@@ -5003,6 +5030,81 @@ export class PluginRuntime {
* actually authorised the call, which is what lets an operator tell an
* imported-session read from a read of the user's own conversation.
*/
+ /**
+ * #2377: refuse a targeted desktop.control session read whose target session
+ * belongs to a project the caller's activation scope excludes. Resolved from
+ * the live session-project map when available; an unresolvable project is
+ * allowed through, matching the workspace-relative fallback the event
+ * fan-out uses. A no-op without a scope service (headless hosts, global
+ * plugins always pass through `pluginActiveInProject` anyway).
+ */
+ private assertDesktopSessionScope(
+ pluginId: string,
+ operation: string,
+ args: readonly unknown[],
+ ): void {
+ if (operation === SESSION_LIST_DESKTOP_OPERATION) return;
+ if (!SESSION_READ_DESKTOP_OPERATIONS.has(operation)) return;
+ const activeInProject = this.services.pluginActiveInProject;
+ if (!activeInProject) return;
+ const sessionId = desktopSessionTargetId(args);
+ if (!sessionId) return;
+ const projectPath = this.services.getWorkspacePathForSession?.(sessionId);
+ if (projectPath === undefined) return;
+ if (activeInProject(pluginId, projectPath)) return;
+ this.services.audit?.({
+ pluginId,
+ api: "desktop.invoke",
+ operation,
+ ok: false,
+ errorCode: "PERMISSION_DENIED",
+ reason: "scope",
+ ts: Date.now(),
+ });
+ throw apiError(
+ "PERMISSION_DENIED",
+ `${operation} targets a session outside this plugin's activation scope`,
+ );
+ }
+
+ /**
+ * #2377: hold the activation-scope line on the results of a desktop.control
+ * session read. `session/list` loses the sessions whose project the caller's
+ * scope excludes, so a project-scoped plugin never learns which sessions
+ * another project has open — the same guarantee `session.getActive` and the
+ * #1754 fan-out enforce on their channels.
+ */
+ private scopeDesktopSessionResult(
+ pluginId: string,
+ operation: string,
+ result: unknown,
+ ): unknown {
+ if (operation !== SESSION_LIST_DESKTOP_OPERATION) return result;
+ const activeInProject = this.services.pluginActiveInProject;
+ if (!activeInProject) return result;
+ const sessions = (result as { sessions?: unknown[] } | null | undefined)?.sessions;
+ if (!Array.isArray(sessions)) return result;
+ const scoped = sessions.filter((session) =>
+ activeInProject(
+ pluginId,
+ (session as { projectPath?: string | null } | null)?.projectPath ?? null,
+ ),
+ );
+ if (scoped.length !== sessions.length) {
+ this.services.audit?.({
+ pluginId,
+ api: "desktop.invoke",
+ operation,
+ ok: true,
+ scope: "filtered",
+ total: sessions.length,
+ returned: scoped.length,
+ ts: Date.now(),
+ });
+ }
+ return { ...(result as Record<string, unknown>), sessions: scoped };
+ }
+
private assertSessionScope(
loaded: LoadedPlugin,
verb: SessionAccessVerb,
@@ -5837,6 +5939,7 @@ export class PluginRuntime {
const operationInfo = this.services.desktopControl.operations.find(
(candidate) => candidate.id === operation,
);
+ this.assertDesktopSessionScope(pluginId, operation, args);
// The controller's `confirm` flag is an acknowledgement by the
// caller, not a decision by the user. A plugin can set it at will,
// so a dangerous operation additionally needs the host's native
@@ -5895,6 +5998,7 @@ export class PluginRuntime {
},
...(invocation ? { signal: invocation.signal } : {}),
} satisfies McpControlInvokeInput);
+ const scopedResult = this.scopeDesktopSessionResult(pluginId, operation, result);
this.services.audit?.({
pluginId,
api: "desktop.invoke",
@@ -5903,7 +6007,7 @@ export class PluginRuntime {
ok: true,
ts: Date.now(),
});
- return result;
+ return scopedResult;
} catch (error) {
this.services.audit?.({
pluginId,
diff --git a/apps/desktop/electron/main/services/plugin-services.ts b/apps/desktop/electron/main/services/plugin-services.ts
index 7dc4ac1c5..5eef7caf7 100644
--- a/apps/desktop/electron/main/services/plugin-services.ts
+++ b/apps/desktop/electron/main/services/plugin-services.ts
@@ -757,6 +757,12 @@ export function createPluginServices({
* and revealing a view stay inside the project the plugin was activated in.
*/
isPluginActiveInProject: (pluginId) => isPluginActiveInProject(pluginId),
+ /**
+ * #2377: the project-aware form backs the desktop.control session-read
+ * gate, so a scoped plugin's `session/list` (including the reads a
+ * background service makes) never crosses its activation scope.
+ */
+ pluginActiveInProject,
/**
* `pi.ui.revealView`: the runtime validated the view and the session; the
* shell owns work-panel tabs, so it is the surface that opens the tab.
diff --git a/apps/desktop/test/plugin-desktop-control.test.mjs b/apps/desktop/test/plugin-desktop-control.test.mjs
index b3f87f400..c88b901c0 100644
--- a/apps/desktop/test/plugin-desktop-control.test.mjs
+++ b/apps/desktop/test/plugin-desktop-control.test.mjs
@@ -192,3 +192,93 @@ test("desktop control fails closed without its permission", async (t) => {
(error) => error.code === "PERMISSION_DENIED",
);
});
+
+test("project-scoped plugins get session/list filtered by activation scope", async (t) => {
+ const calls = [];
+ const runtime = createRuntime(t, calls, {
+ desktopControl: {
+ operations: [
+ { id: "session/list", channel: "sessionList", description: "List durable sessions", risk: "read" },
+ ],
+ invoke: async (input) => {
+ calls.push(input);
+ return {
+ sessions: [
+ { id: "in-scope", projectPath: "/workspace/allowed" },
+ { id: "out-of-scope", projectPath: "/workspace/other" },
+ { id: "no-project" },
+ ],
+ };
+ },
+ },
+ pluginActiveInProject: (pluginId, projectPath) => projectPath === "/workspace/allowed",
+ });
+ const dir = writePlugin("demo.scoped.list", ["desktop.control"], `
+ module.exports = {
+ onPanelInvoke: async () => pi.desktop.invoke({ operation: "session/list", args: [] }),
+ };
+ `);
+ await runtime.loadFromPath(dir, ["desktop.control"]);
+ const result = await runtime.invokePanelBridge("demo.scoped.list", "desktop.test");
+ assert.deepEqual(result.sessions, [{ id: "in-scope", projectPath: "/workspace/allowed" }]);
+});
+
+test("project-scoped plugins cannot read an out-of-scope session via desktop.control", async (t) => {
+ const calls = [];
+ const runtime = createRuntime(t, calls, {
+ desktopControl: {
+ operations: [
+ { id: "session/get", channel: "sessionGet", description: "Read a session", risk: "read" },
+ ],
+ invoke: async (input) => {
+ calls.push(input);
+ return { session: { id: "secret", projectPath: "/workspace/other" } };
+ },
+ },
+ pluginActiveInProject: (pluginId, projectPath) => projectPath !== "/workspace/other",
+ getWorkspacePathForSession: (sessionId) => sessionId === "secret" ? "/workspace/other" : null,
+ });
+ const dir = writePlugin("demo.scoped.get", ["desktop.control"], `
+ module.exports = {
+ onPanelInvoke: async () => {
+ try {
+ return { ok: true, result: await pi.desktop.invoke({
+ operation: "session/get",
+ args: [{ sessionId: "secret" }],
+ }) };
+ } catch (error) {
+ return { ok: false, code: error.code, message: error.message };
+ }
+ },
+ };
+ `);
+ await runtime.loadFromPath(dir, ["desktop.control"]);
+ const result = await runtime.invokePanelBridge("demo.scoped.get", "desktop.test");
+ assert.equal(result.ok, false);
+ assert.equal(result.code, "PERMISSION_DENIED");
+ assert.deepEqual(calls, [], "an out-of-scope targeted read never reaches the controller");
+});
+
+test("an unscoped plugin keeps full desktop.control session reads", async (t) => {
+ const calls = [];
+ const runtime = createRuntime(t, calls, {
+ desktopControl: {
+ operations: [
+ { id: "session/list", channel: "sessionList", description: "List durable sessions", risk: "read" },
+ ],
+ invoke: async (input) => {
+ calls.push(input);
+ return { sessions: [{ id: "any", projectPath: "/anywhere" }] };
+ },
+ },
+ pluginActiveInProject: () => true,
+ });
+ const dir = writePlugin("demo.global.list", ["desktop.control"], `
+ module.exports = {
+ onPanelInvoke: async () => pi.desktop.invoke({ operation: "session/list", args: [] }),
+ };
+ `);
+ await runtime.loadFromPath(dir, ["desktop.control"]);
+ const result = await runtime.invokePanelBridge("demo.global.list", "desktop.test");
+ assert.deepEqual(result.sessions, [{ id: "any", projectPath: "/anywhere" }]);
+});
diff --git a/docs/spec/07-plugins/13-plugin-permissions-matrix.md b/docs/spec/07-plugins/13-plugin-permissions-matrix.md
index 1237fca3f..5dcc3e3fb 100644
--- a/docs/spec/07-plugins/13-plugin-permissions-matrix.md
+++ b/docs/spec/07-plugins/13-plugin-permissions-matrix.md
@@ -40,7 +40,7 @@ Provide a permission–capability–risk–default-policy reference table for re
| `bus.publish` | medium | `bus.publish` to declared topics | Confirm at install | Other plugins can act on the message |
| `bus.subscribe` | medium | `bus.subscribe` to declared patterns | Confirm at install | Can observe another plugin's messages |
| `browser.cdp` | high | `pi.browser.*` against the host work-panel guest | Confirm at install | Guest bounds are clamped to the calling plugin view; CDP is allowlisted |
-| `desktop.control` | high | `pi.desktop.listOperations`, `pi.desktop.invoke`, including the reviewed `session/collaboration/*` operations | Confirm at install | Shared with the local MCP control plane's reviewed operation catalog, except for operations marked plugin-only: the six `session/collaboration/*` operations reach the plugin gateway but are deliberately absent from the MCP-visible catalog and have no renderer mutation channel; collaboration `spawn`/`send` additionally require an active plugin Agent tool invocation, whose source Session/turn/invocation identity is injected by the host; panel cancellation is limited to that plugin's own deliveries; a `dangerous` operation needs `confirm: true` from the plugin **and** the user's answer to a host-owned native dialog that names the catalog operation; the MCP bearer token and Electron channel names are never exposed |
+| `desktop.control` | high | `pi.desktop.listOperations`, `pi.desktop.invoke`, including the reviewed `session/collaboration/*` operations | Confirm at install | Shared with the local MCP control plane's reviewed operation catalog, except for operations marked plugin-only: the six `session/collaboration/*` operations reach the plugin gateway but are deliberately absent from the MCP-visible catalog and have no renderer mutation channel; collaboration `spawn`/`send` additionally require an active plugin Agent tool invocation, whose source Session/turn/invocation identity is injected by the host; panel cancellation is limited to that plugin's own deliveries; a `dangerous` operation needs `confirm: true` from the plugin **and** the user's answer to a host-owned native dialog that names the catalog operation; session reads (`session/list`, `session/get`, `session/listMessages`, `session/listRevisions`) are filtered by activation scope like `session.getActive` (#2377); the MCP bearer token and Electron channel names are never exposed |
| `ui.microphone` | medium | `navigator.mediaDevices.getUserMedia({ audio: true })` inside the plugin's isolated panel | Confirm at install | Audio only; camera and every other device permission stay denied; no native handle or host secret reaches the plugin |
| `audio.capture.background` | high | `pi.audio.getInputDevices`, `openInput`, `closeInput`, `getCaptureState`, `onInputFrame` / `offInputFrame` (registered in the plugin API and gated by this permission; the two synchronous registration helpers throw the coded refusal) | Deny by default | Host owns the device; PCM16 frames only, no device handle or `MediaStream`. The host has no device backend yet, so an authorized call is refused with a coded `UNSUPPORTED` (audited); no device is opened |
| `audio.playback.background` | medium | `pi.audio.openOutput`, `writeOutput`, `stopOutput`, `closeOutput` (registered in the plugin API and gated by this permission) | Confirm at install | Host-owned playback queue, PCM16 only. The host has no device backend yet, so an authorized call is refused with a coded `UNSUPPORTED` (audited); no device is opened |