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): count a .git only as a gitfile or a directory holding HEAD (R46 C1)

reviewb0ebbddba2bb
diff --git a/CLAUDE.md b/CLAUDE.md
index 56db48d9c..7a11ead8b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -6186,10 +6186,11 @@ The invariants, each with its reason:
argument check. `fs:*` is realpath-jailed to a shared project's realpathed cwd; `git:diff` jails
the FILE too (`--no-index` diffs any file on the host); a `git:show-file` ref starting with `-`
is an option (`--output=` writes a file); **every git read also needs the shared root holding
- the cwd to be the top of its OWN repository** (a `.git` dir or worktree file): `git show
- <ref>:<p>` resolves `<p>` against the repository's top level and `git status`/`log` report the
- whole repository, so from a shared `repo/shared/` a Viewer read `repo/secret/key.txt` (measured,
- C1) — a monorepo subfolder gets no git panel; `pty:create` is cut down to a whitelist, because
+ the cwd to be the top of its OWN repository** (a `.git` dir holding `HEAD`, or a worktree's
+ `.git` file; git skips an empty `.git` dir): `git show <ref>:<p>` resolves `<p>` against the
+ repository's top level and `git status`/`log` report the whole repository, so from a shared
+ `repo/shared/` a Viewer read `repo/secret/key.txt` (measured, C1) — a monorepo subfolder gets no
+ git panel; `pty:create` is cut down to a whitelist, because
`sshRemote`'s args run `ssh` on the host during the existence probe. A non-editor's terminal
frames (output, resync, size, exit) are judged per frame by the session's node
(`PtyManager.nodeOfSession`), because a subscription outlives `team unshare`. Editors pass untouched
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.md
index 48da0034f..4a1e26c48 100644
--- a/docs/hosted-team-relay.md
+++ b/docs/hosted-team-relay.md
@@ -204,7 +204,7 @@ session key** in the team store.
| `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. 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: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 that holds `HEAD`, or a worktree's `.git` file. An empty `.git` directory, or a `.git` symlink to a folder that is not a repository, does not count: git skips it and uses the enclosing repository. 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. |
| `agent:subagent-snapshot` | The response is trimmed to shared nodes. |
diff --git a/src/core/relay/access-policy.test.ts b/src/core/relay/access-policy.test.ts
index 62784b885..22f147e69 100644
--- a/src/core/relay/access-policy.test.ts
+++ b/src/core/relay/access-policy.test.ts
@@ -21,7 +21,9 @@ const ctx = (role: AccessContext['role']): AccessContext => ({
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)
+ realpath: (p) => (p.startsWith('/srv/app/link') ? '/etc/passwd' : p),
+ // Every mock root is the top of its own repository unless a helper says otherwise.
+ isFile: (p) => p.endsWith('/.git/HEAD')
})
describe('access policy', () => {
@@ -124,6 +126,13 @@ describe('access policy: the fs jail', () => {
} catch {
return null
}
+ },
+ isFile: (p) => {
+ try {
+ return fs.statSync(p).isFile()
+ } catch {
+ return false
+ }
}
}
expect(decideAccess('req', IPC.fsRead, [path.join(root, 'link', 'README.md')], c).allow).toBe(true)
@@ -464,7 +473,7 @@ describe('access policy: git needs a shared root that is its own repository (C1)
const subfolder = (role: AccessContext['role']): AccessContext => ({
...ctx(role),
projectCwds: () => ['/repo/shared'],
- realpath: (p) => (p.endsWith('/.git') && p !== '/repo/.git' ? null : p)
+ isFile: (p) => p === '/repo/.git/HEAD'
})
it('every git VIEW method is refused, with the reason, when the shared root has no .git of its own', () => {
@@ -492,7 +501,7 @@ describe('access policy: git needs a shared root that is its own repository (C1)
const c: AccessContext = {
...ctx('viewer'),
projectCwds: () => ['/mono/a', '/mono/a/b'],
- realpath: (p) => (p.endsWith('/.git') && p !== '/mono/a/b/.git' ? null : p)
+ isFile: (p) => p === '/mono/a/b/.git/HEAD'
}
expect(decideAccess('req', IPC.gitStatus, ['/mono/a/b/src'], c).allow).toBe(true)
expect(decideAccess('req', IPC.gitStatus, ['/mono/a/x'], c)).toEqual({ allow: false, message: NOT_OWN_REPO })
@@ -502,6 +511,7 @@ describe('access policy: git needs a shared root that is its own repository (C1)
const base = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'access-git-')))
try {
fs.mkdirSync(path.join(base, 'repo', '.git'), { recursive: true })
+ fs.writeFileSync(path.join(base, 'repo', '.git', 'HEAD'), 'ref: refs/heads/main\n')
fs.mkdirSync(path.join(base, 'repo', 'shared'), { recursive: true })
fs.mkdirSync(path.join(base, 'wt'), { recursive: true })
fs.writeFileSync(path.join(base, 'wt', '.git'), `gitdir: ${path.join(base, 'repo', '.git', 'worktrees', 'wt')}\n`)
@@ -518,6 +528,13 @@ describe('access policy: git needs a shared root that is its own repository (C1)
} catch {
return null
}
+ },
+ isFile: (p) => {
+ try {
+ return fs.statSync(p).isFile()
+ } catch {
+ return false
+ }
}
})
const show = (root: string, cwd: string) => decideAccess('req', IPC.gitShowFile, [cwd, 'HEAD', 'secret/key.txt'], onDisk(root))
@@ -533,6 +550,60 @@ describe('access policy: git needs a shared root that is its own repository (C1)
fs.rmSync(base, { recursive: true, force: true })
}
})
+
+ // R46. Git skips a `.git` that is not a repository and keeps searching upward (measured with real
+ // git 2.43: an empty `.git` dir and a `.git` symlink to a plain directory both resolve to the
+ // ENCLOSING repository). So "the root holds a `.git`" is not enough: it must be a gitfile, which
+ // git either follows or stops on, or a directory that holds HEAD.
+ it.skipIf(process.platform === 'win32')('on disk: a .git counts only as a gitfile or a directory holding HEAD (symlinks need privileges on Windows)', () => {
+ const base = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'access-gitmark-')))
+ try {
+ const repo = path.join(base, 'repo')
+ fs.mkdirSync(path.join(repo, '.git'), { recursive: true })
+ fs.writeFileSync(path.join(repo, '.git', 'HEAD'), 'ref: refs/heads/main\n')
+ // An empty `.git` dir at a subfolder root.
+ fs.mkdirSync(path.join(repo, 'empty', '.git'), { recursive: true })
+ // A `.git` dir holding HEAD: its own repository.
+ fs.mkdirSync(path.join(repo, 'headed', '.git'), { recursive: true })
+ fs.writeFileSync(path.join(repo, 'headed', '.git', 'HEAD'), 'ref: refs/heads/main\n')
+ // A `.git` file (a worktree or submodule gitfile).
+ fs.mkdirSync(path.join(repo, 'gitfile'), { recursive: true })
+ fs.writeFileSync(path.join(repo, 'gitfile', '.git'), `gitdir: ${path.join(repo, '.git', 'worktrees', 'gitfile')}\n`)
+ // A `.git` symlink to a directory that is not a repository.
+ fs.mkdirSync(path.join(base, 'plain'), { recursive: true })
+ fs.mkdirSync(path.join(repo, 'linked'), { recursive: true })
+ fs.symlinkSync(path.join(base, 'plain'), path.join(repo, 'linked', '.git'))
+ const onDisk = (root: string): AccessContext => ({
+ role: 'viewer',
+ sharedProjects: new Set(['P']),
+ projectsOfNode: () => [],
+ nodeOfSession: () => undefined,
+ hostDataDir: '/nonexistent-nodeterm-data',
+ projectCwds: () => [root],
+ realpath: (p) => {
+ try {
+ return fs.realpathSync(p)
+ } catch {
+ return null
+ }
+ },
+ isFile: (p) => {
+ try {
+ return fs.statSync(p).isFile()
+ } catch {
+ return false
+ }
+ }
+ })
+ const status = (sub: string) => decideAccess('req', IPC.gitStatus, [path.join(repo, sub)], onDisk(path.join(repo, sub)))
+ expect(status('empty')).toEqual({ allow: false, message: NOT_OWN_REPO })
+ expect(status('linked')).toEqual({ allow: false, message: NOT_OWN_REPO })
+ expect(status('headed')).toEqual({ allow: true })
+ expect(status('gitfile')).toEqual({ allow: true })
+ } finally {
+ fs.rmSync(base, { recursive: true, force: true })
+ }
+ })
})
const READ_JAIL_MSG = 'Viewers can only read files inside a shared project.'
diff --git a/src/core/relay/access-policy.ts b/src/core/relay/access-policy.ts
index e97fff42b..f74a10372 100644
--- a/src/core/relay/access-policy.ts
+++ b/src/core/relay/access-policy.ts
@@ -43,6 +43,9 @@ export interface AccessContext {
hostDataDir: string
/** `fs.realpath`; null = the path does not resolve. */
realpath(p: string): string | null
+ /** Does `p` resolve (symlinks followed) to a regular file? false when it is missing, not a file,
+ * or unreadable. Only the git rule asks it (`ownRepoMarker`). */
+ isFile(p: string): boolean
}
type Check = (args: unknown[], ctx: AccessContext) => AccessDecision
@@ -123,18 +126,29 @@ 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.'
/**
- * Is `real` (a realpath inside a shared root) under a shared root that holds its OWN `.git` — a
- * directory, or a worktree's `.git` file? The cwd jail alone does not bound git: `git show <ref>:<p>`
- * resolves a bare `<p>` against the repository's TOP LEVEL, and `git status` / `git log` report the
- * whole repository, so from `repo/shared/` a viewer could read `repo/secret/key.txt` (measured with
- * real git). A root with its own `.git` stops git's upward discovery at or below it, so every
- * repository reachable from `real` is inside something that is shared anyway. Any containing root
- * counts: a nested shared root that is its own repository is fine even when the outer one is not.
- * Planting a `.git` (or a `gitdir:` file pointing elsewhere) takes write access to the shared root,
- * which only an editor has.
+ * Does `root` hold a `.git` that stops git's upward search there? A regular FILE (a worktree's or
+ * submodule's gitfile), which git either follows or stops on with an error, or a DIRECTORY holding
+ * HEAD. Git skips an empty `.git` directory, or a `.git` symlink to a directory that is not a
+ * repository, and finds the ENCLOSING repository (measured with real git), so neither counts.
+ */
+function ownRepoMarker(root: string, ctx: AccessContext): boolean {
+ const dotGit = path.join(root, '.git')
+ return ctx.isFile(dotGit) || ctx.isFile(path.join(dotGit, 'HEAD'))
+}
+
+/**
+ * Is `real` (a realpath inside a shared root) under a shared root that is the top of its OWN
+ * repository (`ownRepoMarker`)? The cwd jail alone does not bound git: `git show <ref>:<p>` resolves
+ * a bare `<p>` against the repository's TOP LEVEL, and `git status` / `git log` report the whole
+ * repository, so from `repo/shared/` a viewer could read `repo/secret/key.txt` (measured with real
+ * git). A root with its own repository stops git's upward discovery at or below it. Any containing
+ * root counts: a nested shared root that is its own repository is fine even when the outer one is
+ * not. This check cannot tell a real repository from a planted one: a `.git` directory with a HEAD,
+ * or a gitfile pointing at another repository's gitdir, would pass. Planting either needs write
+ * access to the shared root, which only an Editor has, and an Editor already has a shell.
*/
function underOwnRepoRoot(real: string, ctx: AccessContext): boolean {
- return sharedRoots(ctx).some((root) => within(root, real) && ctx.realpath(path.join(root, '.git')) !== null)
+ return sharedRoots(ctx).some((root) => within(root, real) && ownRepoMarker(root, ctx))
}
/** Every git VIEW check: the cwd jailed like a file read, then the repository rule above. */
diff --git a/src/core/relay/hosted-service.ts b/src/core/relay/hosted-service.ts
index 13236039e..ae719e487 100644
--- a/src/core/relay/hosted-service.ts
+++ b/src/core/relay/hosted-service.ts
@@ -25,7 +25,7 @@
// - The scheduler hears about EVERY session end. The core fires `onClose` only for ends the shell
// did not ask for; every end this service causes (deny, expiry, removal, a listener the
// scheduler closes) runs the same `ended` bookkeeping, at most once per session.
-import { realpathSync } from 'node:fs'
+import { realpathSync, statSync } from 'node:fs'
import path from 'node:path'
import { randomUUID } from 'node:crypto'
import { IPC } from '../../shared/ipc'
@@ -164,6 +164,14 @@ const realpath = (p: string): string | null => {
}
}
+const isFile = (p: string): boolean => {
+ try {
+ return statSync(p).isFile()
+ } catch {
+ return false
+ }
+}
+
const idOf = (p: unknown): unknown =>
p !== null && typeof p === 'object' ? (p as Record<string, unknown>).id : undefined
@@ -227,7 +235,8 @@ export function createHostedService(deps: HostedServiceDeps): HostedService {
[...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
+ realpath,
+ isFile
}
}