Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(relay): strip exec fields from project documents sent to non-editor peers

review077c74761acd
diff --git a/src/core/relay/access-policy.test.ts b/src/core/relay/access-policy.test.ts
index 22f147e69..c591ae604 100644
--- a/src/core/relay/access-policy.test.ts
+++ b/src/core/relay/access-policy.test.ts
@@ -381,6 +381,31 @@ describe('outbound filter: deny by default for non-editors', () => {
})
})
+describe('wrapSinkForRole — project documents lose their exec fields for non-editors', () => {
+ const doc = JSON.stringify({
+ t: 'ev',
+ channel: IPC.workspaceServerChange,
+ args: [{ id: 'P', nodes: [{ id: 'n1', kind: 'terminal', position: { x: 0, y: 0 }, shell: '/usr/bin/evil' }] }]
+ })
+ const run = (role: AccessContext['role']) => {
+ const text: string[] = []
+ wrapSinkForRole({ sendText: (j) => text.push(j), sendBinary: () => {} }, () => ctx(role)).sendText(doc)
+ return text
+ }
+ it('a viewer gets the shared project without the exec field; an editor gets it verbatim', () => {
+ const v = run('viewer')
+ expect(v).toHaveLength(1)
+ expect(JSON.parse(v[0]).args[0].nodes[0].shell).toBeUndefined()
+ expect(run('editor')).toEqual([doc])
+ })
+ it('a viewer\'s workspace:load is stripped too', () => {
+ const out = narrowResponseForRole(IPC.workspaceLoad, { projects: [JSON.parse(doc).args[0]] }, ctx('viewer')) as {
+ projects: Array<{ nodes: Array<{ shell?: string }> }>
+ }
+ expect(out.projects[0].nodes[0].shell).toBeUndefined()
+ })
+})
+
describe('wrapSinkForRole', () => {
const sinkWith = (buffered: () => number) => {
const text: string[] = []
diff --git a/src/core/relay/access-policy.ts b/src/core/relay/access-policy.ts
index a1fda125e..e09f2527b 100644
--- a/src/core/relay/access-policy.ts
+++ b/src/core/relay/access-policy.ts
@@ -25,6 +25,8 @@ import { decodePtyDataSessionId } from '../../shared/rpc'
import type { TeamRole } from './team-store'
import type { AccessDecision } from './relay-host'
import type { UiSink } from '../ui-sink-registry'
+import { stripSharedNodeExec } from '../../shared/node-exec'
+import type { CanvasNodeState } from '../../shared/types'
export interface AccessContext {
role: TeamRole
@@ -558,10 +560,56 @@ export function filterOutboundEvent(json: string, ctx: AccessContext, subagentOw
* through unchanged. Wire it into the host's `narrowResponse` hook.
*/
export function narrowResponseForRole(method: string, result: unknown, ctx: AccessContext): unknown {
- if (isEditor(ctx.role) || method !== IPC.agentSubagentSnapshot) return result
+ if (isEditor(ctx.role)) return result
+ if (method === IPC.workspaceLoad) return redactWorkspaceExec(result)
+ if (method !== IPC.agentSubagentSnapshot) return result
return Array.isArray(result) ? result.filter((e) => sharedNode(field(e, 'nodeId'), ctx)) : []
}
+/**
+ * A Project document with its nodes' exec-enabling fields removed (`stripSharedNodeExec`,
+ * shared/node-exec.ts: the session program, ssh options — and, with the held-launch work, a held
+ * `pendingLaunch`'s command text). A peer that is not allowed to run commands has no business
+ * reading the command lines this machine holds for later. Anything that does not look like a
+ * project passes unchanged (it carries no nodes to strip).
+ */
+export function redactProjectExec(project: unknown): unknown {
+ if (project === null || typeof project !== 'object' || Array.isArray(project)) return project
+ const nodes = (project as { nodes?: unknown }).nodes
+ if (!Array.isArray(nodes)) return project
+ return { ...(project as object), nodes: stripSharedNodeExec(nodes as CanvasNodeState[]) }
+}
+
+/** `workspace:load`'s result with every project's exec fields removed (see `redactProjectExec`). */
+export function redactWorkspaceExec(ws: unknown): unknown {
+ if (ws === null || typeof ws !== 'object' || Array.isArray(ws)) return ws
+ const projects = (ws as { projects?: unknown }).projects
+ if (!Array.isArray(projects)) return ws
+ return { ...(ws as object), projects: projects.map(redactProjectExec) }
+}
+
+/** The two events that carry a WHOLE Project document (VIEW_EVENTS admits them for a shared project). */
+const PROJECT_DOC_EVENTS: ReadonlySet<string> = new Set([IPC.workspaceExternalChange, IPC.workspaceServerChange])
+
+/**
+ * The event JSON a peer may see, after `filterOutboundEvent` has admitted it: a whole-project event
+ * has its nodes' exec fields removed; every other event is returned as-is (same string). Never
+ * throws; an event it cannot parse is returned unchanged (the filter already judged it).
+ */
+export function redactOutboundEvent(json: string): string {
+ let m: unknown
+ try {
+ m = JSON.parse(json)
+ } catch {
+ return json
+ }
+ const channel = field(m, 't') === 'ev' ? field(m, 'channel') : undefined
+ if (typeof channel !== 'string' || !PROJECT_DOC_EVENTS.has(channel)) return json
+ const args = field(m, 'args')
+ if (!Array.isArray(args) || args.length === 0) return json
+ return JSON.stringify({ ...(m as object), args: [redactProjectExec(args[0]), ...args.slice(1)] })
+}
+
/**
* true = deliver this binary frame to the peer. Editors and owners get every frame. For anyone else
* only a pty data frame of a session whose node is in a shared project NOW: a subscription outlives
@@ -597,7 +645,16 @@ export function wrapSinkForRole(sink: UiSink, ctxFor: () => AccessContext): UiSi
}
return {
sendText: (json) => {
- if (decide(() => filterOutboundEvent(json, ctxFor(), owners))) sink.sendText(json)
+ // Editors are shell access and get the document as it is; anyone else gets it without the
+ // exec fields (redactOutboundEvent). Decided inside `decide`, so a throw drops the event.
+ let out: string | null = null
+ decide(() => {
+ const ctx = ctxFor()
+ if (!filterOutboundEvent(json, ctx, owners)) return false
+ out = isEditor(ctx.role) ? json : redactOutboundEvent(json)
+ return true
+ })
+ if (out !== null) sink.sendText(out)
},
sendBinary: (buf) => {
if (decide(() => filterOutboundBinary(buf, ctxFor()))) sink.sendBinary(buf)
diff --git a/src/core/relay/scoped-guest-policy.test.ts b/src/core/relay/scoped-guest-policy.test.ts
index 63d376e61..430f725dd 100644
--- a/src/core/relay/scoped-guest-policy.test.ts
+++ b/src/core/relay/scoped-guest-policy.test.ts
@@ -258,3 +258,37 @@ describe('relay host — host-only channels refused to every peer', () => {
await vi.waitFor(() => expect(t.dispatched.map((r) => r.method)).toEqual([IPC.relayHostedSelf]))
})
})
+
+describe('project documents reach peers without exec fields', () => {
+ // `shell` is an exec field stripSharedNodeExec removes today; a held `pendingLaunch` joins it with
+ // the held-launch work (#1038), through the same helper, with no change here.
+ const project = (id: string) => ({
+ id,
+ name: id,
+ nodes: [{ id: 'a1', kind: 'terminal', position: { x: 0, y: 0 }, shell: '/usr/bin/evil' }]
+ })
+ const ev = (channel: string, ...args: unknown[]) => JSON.stringify({ t: 'ev', channel, args })
+
+ it('scoped guest: workspace:server-change for the shared project arrives stripped; other projects never', () => {
+ const hooks = scopedGuestHooks('alpha', deps())
+ const got: string[] = []
+ const sink = hooks.wrapSink!(null as never, { sendText: (j) => got.push(j), sendBinary: () => {} })
+ sink.sendText(ev(IPC.workspaceServerChange, project('beta')))
+ sink.sendText(ev(IPC.workspaceServerChange, project('alpha')))
+ sink.sendText(ev(IPC.workspaceExternalChange, project('alpha')))
+ expect(got).toHaveLength(2)
+ for (const j of got) {
+ const node = JSON.parse(j).args[0].nodes[0]
+ expect(node.id).toBe('a1')
+ expect(node.shell).toBeUndefined()
+ }
+ })
+
+ it('scoped guest: workspace:load response is stripped', () => {
+ const hooks = scopedGuestHooks('alpha', deps())
+ const out = hooks.narrowResponse!(null as never, IPC.workspaceLoad, { projects: [project('alpha')] }) as {
+ projects: Array<{ nodes: Array<{ shell?: string }> }>
+ }
+ expect(out.projects[0].nodes[0].shell).toBeUndefined()
+ })
+})
diff --git a/src/core/relay/scoped-guest-policy.ts b/src/core/relay/scoped-guest-policy.ts
index b1da0a083..42a770261 100644
--- a/src/core/relay/scoped-guest-policy.ts
+++ b/src/core/relay/scoped-guest-policy.ts
@@ -35,6 +35,7 @@ import {
filterOutboundBinary,
filterOutboundEvent,
narrowResponseForRole,
+ redactOutboundEvent,
within,
type AccessContext,
type SubagentOwners
@@ -566,7 +567,10 @@ export function scopedGuestHooks(projectId: string, deps: ScopedGuestDeps): Rela
}
return {
sendText: (json) => {
- if (safe(() => filterScopedEvent(json, s, owners))) sink.sendText(json)
+ // Admitted events are sent with project documents' exec fields removed (held launch
+ // commands, session programs, ssh options): the guest reads the shared project's canvas,
+ // not the command lines this machine holds for it.
+ if (safe(() => filterScopedEvent(json, s, owners))) sink.sendText(redactOutboundEvent(json))
},
sendBinary: (buf) => {
if (safe(() => filterOutboundBinary(buf, viewCtx(s)))) sink.sendBinary(buf)