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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(hosted-team): viewers never read the server's data directory (M7)
reviewa4fe2aca2796
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.mdindex 55fdb4569..09dbaac25 100644--- a/docs/hosted-team-relay.md+++ b/docs/hosted-team-relay.md@@ -203,7 +203,7 @@ session key** in the team store.| `pty:kill` | Detaches the caller's own view; the session keeps running. || `pty:capture`, `pty:read-scrollback`, `pty:pane-command` | Nodes of shared projects only. || `pty:tmux-status` | Allowed. |-| `fs:list`, `fs:read`, `fs:read-binary`, `fs:exists` | The path must be absolute and its **realpath** inside a shared project's cwd (also realpathed). A symlink planted inside the project that points out of it is outside. |+| `fs:list`, `fs:read`, `fs:read-binary`, `fs:exists` | The path must be absolute and its **realpath** inside a shared project's cwd (also realpathed). A symlink planted inside the project that points out of it is outside. This server's data directory is refused even when a shared project's folder contains it (a project opened on `$HOME`, say): "Viewers can't read this server's own data folder." It holds the host key, `team.json`, every terminal's scrollback snapshot and the unshared canvases. The same holds for a git cwd, and for the file of a `git:diff`. || `git:status`, `git:repo-root`, `git:history` | The cwd is jailed like a file read, **and** the shared project that contains it must be the top folder of its own repository: a `.git` directory, or a worktree's `.git` file. Otherwise: "Git is available to viewers only in a project that is the top folder of its own repository, never in a subfolder of a larger one." A cwd jail alone does not bound git: `git status` and `git log` report the whole repository, and `git show <ref>:<path>` reads `<path>` from the repository's top level, so from a shared `repo/shared/` a Viewer could read `repo/secret/key.txt`. || `git:diff` | The cwd (with the repository rule above) **and** the file are jailed; a pathspec starting with `:` is refused; an untracked diff (`git diff --no-index`, which diffs any file) needs a real path inside. The file is relative to the cwd, not the repository's top level. || `git:show-file` | The cwd jailed, with the repository rule above; a ref starting with `-` is refused (it would become an option, and `--output=` writes a file). Any other revision is allowed. |@@ -420,7 +420,7 @@ The human `team status` reads `state`, `idle` and `lastError` together:terminal runs as that user and can run `team add-owner`. The approval dialog says Editor is "thesame as SSH access".- **A Viewer sees terminal output** and can read **every file** under a shared project's folder,- including `.env` files and `.git`. The approval dialog says so for the Viewer role: "Can read every+ including `.env` files and `.git`, except this server's own data directory. The approval dialog says so for the Viewer role: "Can read everyfile in the shared project's folder, .env files included, and watch its terminals and anythingprinted in them. Its git history too, when the folder is a repository of its own."- **Sharing a repository shares its whole history.** When a shared project is the top folder of itsdiff --git a/src/core/relay/access-policy.test.ts b/src/core/relay/access-policy.test.tsindex 9fde761e5..62784b885 100644--- a/src/core/relay/access-policy.test.ts+++ b/src/core/relay/access-policy.test.ts@@ -20,6 +20,7 @@ const ctx = (role: AccessContext['role']): AccessContext => ({// Terminal sessions: 's' / 's1' run node n1 (project P, shared), 's2' runs n2 (project Q, not).nodeOfSession: (sid) => (sid === 's' || sid === 's1' ? 'n1' : sid === 's2' ? 'n2' : undefined),projectCwds: () => ['/srv/app'],+ hostDataDir: '/var/lib/nodeterm-data',realpath: (p) => (p.startsWith('/srv/app/link') ? '/etc/passwd' : p)})@@ -115,6 +116,7 @@ describe('access policy: the fs jail', () => {sharedProjects: new Set(['P']),projectsOfNode: () => [],nodeOfSession: () => undefined,+ hostDataDir: '/nonexistent-nodeterm-data',projectCwds: () => [path.join(root, 'link')],realpath: (p) => {try {@@ -508,6 +510,7 @@ describe('access policy: git needs a shared root that is its own repository (C1)sharedProjects: new Set(['P']),projectsOfNode: () => [],nodeOfSession: () => undefined,+ hostDataDir: '/nonexistent-nodeterm-data',projectCwds: () => [root],realpath: (p) => {try {@@ -647,3 +650,56 @@ describe('access policy: a node id held by more than one project (M4)', () => {expect(decideAccess('req', IPC.ptyCapture, ['both'], dup('editor', ['P'])).allow).toBe(true)})})++// M7. A shared project whose folder CONTAINS this server's data directory (a project opened on $HOME,+// say) would let a Viewer read the host key, team.json, the password hash, every project's+// scrollback snapshots and the unshared inline canvases. The data directory is never readable by a+// non-editor, whatever shared root holds it.+describe('access policy: the server data directory is never readable by a non-editor (M7)', () => {+ const HOST_DATA = "Viewers can't read this server's own data folder."+ const home = (role: AccessContext['role']): AccessContext => ({+ ...ctx(role),+ projectCwds: () => ['/home/u'],+ hostDataDir: '/home/u/.nodeterm-server',+ realpath: (p) => (p === '/home/u/data-link' ? '/home/u/.nodeterm-server' : p.startsWith('/home/u/data-link/') ? '/home/u/.nodeterm-server' + p.slice('/home/u/data-link'.length) : p)+ })++ it('fs reads, lists and existence checks inside it are refused with the reason; the rest of the root is not', () => {+ const v = home('viewer')+ for (const m of [IPC.fsRead, IPC.fsReadBinary, IPC.fsExists]) {+ expect(decideAccess('req', m, ['/home/u/.nodeterm-server/relay/host-key.json'], v), m).toEqual({ allow: false, message: HOST_DATA })+ }+ expect(decideAccess('req', IPC.fsList, ['/home/u/.nodeterm-server'], v)).toEqual({ allow: false, message: HOST_DATA })+ expect(decideAccess('req', IPC.fsRead, ['/home/u/notes.txt'], v)).toEqual({ allow: true })+ expect(decideAccess('req', IPC.fsList, ['/home/u'], v)).toEqual({ allow: true })+ // A sibling whose name starts the same is not inside it.+ expect(decideAccess('req', IPC.fsRead, ['/home/u/.nodeterm-server2/x'], v)).toEqual({ allow: true })+ })++ it('a symlink inside the shared root that points into it is refused too', () => {+ expect(decideAccess('req', IPC.fsRead, ['/home/u/data-link/team.json'], home('commenter'))).toEqual({ allow: false, message: HOST_DATA })+ })++ it('git reads cannot reach it either: a cwd inside it, or a diffed file inside it', () => {+ const v = home('viewer')+ expect(decideAccess('req', IPC.gitStatus, ['/home/u/.nodeterm-server'], v)).toEqual({ allow: false, message: HOST_DATA })+ expect(decideAccess('req', IPC.gitDiff, ['/home/u', '.nodeterm-server/relay/team.json', false, true], v)).toEqual({ allow: false, message: HOST_DATA })+ expect(decideAccess('req', IPC.gitDiff, ['/home/u', '.nodeterm-server/relay/team.json', false, false], v)).toEqual({ allow: false, message: HOST_DATA })+ expect(decideAccess('req', IPC.gitDiff, ['/home/u', 'src/a.ts', false, false], v)).toEqual({ allow: true })+ })++ it('a data directory configured through a symlink is compared by its real path', () => {+ const c: AccessContext = {+ ...ctx('viewer'),+ projectCwds: () => ['/data'],+ hostDataDir: '/srv/nt-link',+ realpath: (p) => (p === '/srv/nt-link' ? '/data/nt' : p)+ }+ expect(decideAccess('req', IPC.fsRead, ['/data/nt/relay/host-key.json'], c)).toEqual({ allow: false, message: HOST_DATA })+ expect(decideAccess('req', IPC.fsRead, ['/data/other.txt'], c)).toEqual({ allow: true })+ })++ it('editors are unaffected', () => {+ expect(decideAccess('req', IPC.fsRead, ['/home/u/.nodeterm-server/relay/host-key.json'], home('editor'))).toEqual({ allow: true })+ })+})diff --git a/src/core/relay/access-policy.ts b/src/core/relay/access-policy.tsindex a01fdef41..59db3747b 100644--- a/src/core/relay/access-policy.ts+++ b/src/core/relay/access-policy.ts@@ -34,6 +34,10 @@ export interface AccessContext {nodeOfSession(sessionId: string): string | undefined/** The LOCAL cwds of the shared projects. */projectCwds(): string[]+ /** This server's own data directory (host key, team.json, password hash, every project's+ * scrollback, the unshared inline canvases). Never readable by a non-editor, even under a shared+ * root. Compared by its realpath. */+ hostDataDir: string/** `fs.realpath`; null = the path does not resolve. */realpath(p: string): string | null}@@ -82,16 +86,35 @@ function within(root: string, p: string): boolean {return rel === '' || (rel !== '..' && !rel.startsWith('..' + path.sep) && !path.isAbsolute(rel))}-/** The REAL path of `p` when it is an absolute path inside a shared project root, else null. The- * file is realpathed, so a symlink planted inside the project that points out of it is outside. */-function realInSharedCwd(p: unknown, ctx: AccessContext): string | null {- if (typeof p !== 'string' || !path.isAbsolute(p)) return null+const READ_JAIL = 'Viewers can only read files inside a shared project.'+const HOST_DATA = "Viewers can't read this server's own data folder."++/** `p` (a real or lexically resolved path) is inside this server's data directory. A share of `$HOME`+ * or `/` contains it, and it holds the host's secret key: the shared root is not the only fence. */+function inHostData(p: string, ctx: AccessContext): boolean {+ const d = ctx.hostDataDir+ if (typeof d !== 'string' || !d) return false+ const resolved = path.resolve(d)+ return within(ctx.realpath(resolved) ?? resolved, p)+}++/** The REAL path of `p` when a non-editor may read it, else why not. An absolute path inside a+ * shared project root and outside the data directory. The file is realpathed, so a symlink planted+ * inside the project that points out of it (or into the data directory) is refused. */+function jailRead(p: unknown, ctx: AccessContext): { real: string } | { refuse: string } {+ if (typeof p !== 'string' || !path.isAbsolute(p)) return { refuse: READ_JAIL }const resolved = ctx.realpath(path.resolve(p))- if (!resolved) return null- return sharedRoots(ctx).some((root) => within(root, resolved)) ? resolved : null+ if (!resolved) return { refuse: READ_JAIL }+ if (!sharedRoots(ctx).some((root) => within(root, resolved))) return { refuse: READ_JAIL }+ if (inHostData(resolved, ctx)) return { refuse: HOST_DATA }+ return { real: resolved }}-const READ_JAIL = 'Viewers can only read files inside a shared project.'+/** `jailRead`'s real path, or null when it refuses. */+function realInSharedCwd(p: unknown, ctx: AccessContext): string | null {+ const j = jailRead(p, ctx)+ return 'real' in j ? j.real : null+}const NOT_OWN_REPO ='Git is available to viewers only in a project that is the top folder of its own repository, never in a subfolder of a larger one.'@@ -115,14 +138,17 @@ function underOwnRepoRoot(real: string, ctx: AccessContext): boolean {const gitView =(rest: Check = pass): Check =>(a, ctx) => {- const cwd = realInSharedCwd(a[0], ctx)- if (!cwd) return no(READ_JAIL)- if (!underOwnRepoRoot(cwd, ctx)) return no(NOT_OWN_REPO)+ const j = jailRead(a[0], ctx)+ if (!('real' in j)) return no(j.refuse)+ if (!underOwnRepoRoot(j.real, ctx)) return no(NOT_OWN_REPO)return rest(a, ctx)}const nodeArg0: Check = (a, ctx) => (sharedNode(a[0], ctx) ? OK : no('That terminal is not in a shared project.'))-const pathArg0: Check = (a, ctx) => (realInSharedCwd(a[0], ctx) ? OK : no(READ_JAIL))+const pathArg0: Check = (a, ctx) => {+ const j = jailRead(a[0], ctx)+ return 'real' in j ? OK : no(j.refuse)+}const projectArg0: Check = (a, ctx) => (sharedProject(a[0], ctx) ? OK : no('That project is not shared.'))/** The ONLY create fields a watch-only join keeps. Everything else on `PtyCreateOptions` either@@ -150,10 +176,14 @@ const gitDiff: Check = (a, ctx) => {const target = path.resolve(cwd, file)// The handler reads `untracked` truthily, and then runs `git diff --no-index -- /dev/null <file>`,// which compares FILES, not a repository: any path on the host. Only a real path inside passes.- if (a[3]) return realInSharedCwd(target, ctx) ? OK : no(READ_JAIL)+ if (a[3]) {+ const j = jailRead(target, ctx)+ return 'real' in j ? OK : no(j.refuse)+ }// A tracked diff: git refuses a pathspec outside the repository and does not follow a symlink in// one, so a lexical check is enough — and a deleted file, which has no realpath, stays viewable.- return sharedRoots(ctx).some((root) => within(root, target)) ? OK : no(READ_JAIL)+ if (!sharedRoots(ctx).some((root) => within(root, target))) return no(READ_JAIL)+ return inHostData(target, ctx) ? no(HOST_DATA) : OK}/** `git show <ref>:<file>` (handler args: cwd, ref, file). A ref starting with `-` becomes andiff --git a/src/core/relay/hosted-service.ts b/src/core/relay/hosted-service.tsindex 182bb9037..13236039e 100644--- a/src/core/relay/hosted-service.ts+++ b/src/core/relay/hosted-service.ts@@ -225,6 +225,8 @@ export function createHostedService(deps: HostedServiceDeps): HostedService {nodeOfSession: (sessionId) => deps.nodeOfSession(sessionId),projectCwds: () =>[...shared].map((p) => deps.projectCwd(p)).filter((cwd): cwd is string => typeof cwd === 'string' && cwd.length > 0),+ // Never readable by a non-editor, even when a shared project's folder contains it (M7).+ hostDataDir: deps.dataDir,realpath}}