diff --git a/backend/src/services/sandbox/command.ts b/backend/src/services/sandbox/command.ts index 0d91330a..c07b5089 100644 --- a/backend/src/services/sandbox/command.ts +++ b/backend/src/services/sandbox/command.ts @@ -1,7 +1,7 @@ import path from 'path' import { createHash } from 'crypto' import { accessSync, constants, realpathSync, statSync } from 'fs' -import { readFile, readdir, realpath } from 'fs/promises' +import { lstat, readFile, realpath } from 'fs/promises' import { ENV, getForgeWorktreesPath, @@ -558,44 +558,12 @@ export async function resolveSandboxWorkDirectory(directory: string): Promise { - const worktreesDirectory = path.join(common, 'worktrees') - - let entries: string[] +async function safeRealpath(target: string): Promise { try { - entries = await readdir(worktreesDirectory) + return await realpath(target) } catch { - return false + return null } - - let expectedTarget: string - try { - expectedTarget = path.join(await realpath(top), '.git') - } catch { - return false - } - - for (const entry of entries) { - const entryDirectory = path.join(worktreesDirectory, entry) - let contents: string - try { - contents = (await readFile(path.join(entryDirectory, 'gitdir'), 'utf8')).trim() - } catch { - continue - } - if (contents === '') continue - - const candidate = path.isAbsolute(contents) ? contents : path.resolve(entryDirectory, contents) - let resolvedCandidate: string - try { - resolvedCandidate = await realpath(candidate) - } catch { - continue - } - if (resolvedCandidate === expectedTarget) return true - } - - return false } export async function resolveSessionSandboxMounts(locationDirectory: string): Promise { @@ -605,7 +573,7 @@ export async function resolveSessionSandboxMounts(locationDirectory: string): Pr let result: string | { exitCode: number; stdout: string; stderr: string } try { result = (await executeCommand( - ['git', '-C', work, 'rev-parse', '--path-format=absolute', '--show-toplevel', '--git-common-dir'], + ['git', '-C', work, 'rev-parse', '--path-format=absolute', '--show-toplevel', '--git-dir', '--git-common-dir'], { ignoreExitCode: true, silent: true, timeout: SANDBOX_GIT_RESOLVE_TIMEOUT_MS }, )) as string | { exitCode: number; stdout: string; stderr: string } } catch { @@ -615,12 +583,39 @@ export async function resolveSessionSandboxMounts(locationDirectory: string): Pr if (listing.exitCode !== 0) return [work] const lines = listing.stdout.split('\n').map((line) => line.trim()).filter((line) => line !== '') - if (lines.length < 2) return [work] + if (lines.length < 3) return [work] const top = await resolveSandboxWorkDirectory(lines[0]!) - const common = await resolveSandboxWorkDirectory(lines[1]!) + const gitDir = lines[1]! + const common = await resolveSandboxWorkDirectory(lines[2]!) if (top === null || common === null) return null - if (isPathWithinRoot(top, common)) return [top] - return (await worktreeLinksBackToCommonDir(top, common)) ? [top, common] : null + + const resolvedTop = await safeRealpath(top) + const resolvedWork = await safeRealpath(work) + const resolvedGitDir = await safeRealpath(gitDir) + const resolvedCommon = await safeRealpath(common) + if (resolvedTop === null || resolvedWork === null || resolvedGitDir === null || resolvedCommon === null) return null + if (!isPathWithinRoot(resolvedTop, resolvedWork)) return null + + const topGitPath = path.join(resolvedTop, '.git') + const topGitStat = await lstat(topGitPath).catch(() => null) + if (topGitStat === null || topGitStat.isSymbolicLink()) return null + + if (resolvedGitDir === resolvedCommon) { + if (!topGitStat.isDirectory() || resolvedCommon !== topGitPath) return null + } else { + if (!topGitStat.isFile()) return null + if (path.dirname(resolvedGitDir) !== path.join(resolvedCommon, 'worktrees')) return null + let pointer: string + try { + pointer = (await readFile(path.join(resolvedGitDir, 'gitdir'), 'utf8')).trim() + } catch { + return null + } + const resolvedPointer = await safeRealpath(path.isAbsolute(pointer) ? pointer : path.join(resolvedGitDir, pointer)) + if (resolvedPointer === null || resolvedPointer !== topGitPath) return null + } + + return isPathWithinRoot(top, common) ? [top] : [top, common] } export function resolveSandboxExecUser(): string { diff --git a/backend/test/services/sandbox/command.test.ts b/backend/test/services/sandbox/command.test.ts index d0837750..22df74cd 100644 --- a/backend/test/services/sandbox/command.test.ts +++ b/backend/test/services/sandbox/command.test.ts @@ -918,4 +918,150 @@ describe('resolveSessionSandboxMounts', () => { rmSync(tmp, { recursive: true, force: true }) } }) + + it('rejects a main repository whose core.worktree points at the repos root', async () => { + const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-session-core-worktree-')) + const originalWorkspacePath = process.env.WORKSPACE_PATH + try { + const repos = path.join(tmp, 'repos') + const repo = path.join(repos, 'repo') + mkdirSync(repo, { recursive: true }) + runGit(['init', '-q'], repo) + runGit(['config', 'core.worktree', repos], repo) + + process.env.WORKSPACE_PATH = tmp + + await expect(resolveSessionSandboxMounts(repo)).resolves.toBeNull() + } finally { + restoreWorkspacePath(originalWorkspacePath) + rmSync(tmp, { recursive: true, force: true }) + } + }) + + it('rejects a core.worktree escape that plants a matching worktree back-link', async () => { + const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-session-core-worktree-link-')) + const originalWorkspacePath = process.env.WORKSPACE_PATH + try { + const repos = path.join(tmp, 'repos') + const mine = path.join(repos, 'mine') + const other = path.join(repos, 'other') + mkdirSync(mine, { recursive: true }) + mkdirSync(other, { recursive: true }) + runGit(['init', '-q'], mine) + runGit(['init', '-q'], other) + runGit(['config', 'core.worktree', other], mine) + const planted = path.join(mine, '.git', 'worktrees', 'x') + mkdirSync(planted, { recursive: true }) + writeFileSync(path.join(planted, 'gitdir'), `${path.join(other, '.git')}\n`) + + process.env.WORKSPACE_PATH = tmp + + await expect(resolveSessionSandboxMounts(mine)).resolves.toBeNull() + } finally { + restoreWorkspacePath(originalWorkspacePath) + rmSync(tmp, { recursive: true, force: true }) + } + }) + + it('rejects a main repository whose git dir is a symlink to another repository git dir', async () => { + const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-session-symlink-main-')) + const originalWorkspacePath = process.env.WORKSPACE_PATH + try { + const repos = path.join(tmp, 'repos') + const repo = path.join(repos, 'repo') + const other = path.join(repos, 'other') + mkdirSync(repo, { recursive: true }) + mkdirSync(other, { recursive: true }) + runGit(['init', '-q'], repo) + runGit(['init', '-q'], other) + runGit( + ['-c', 'user.email=ocm@example.com', '-c', 'user.name=OCM', 'commit', '--allow-empty', '-q', '-m', 'init'], + repo, + ) + + rmSync(path.join(repo, '.git'), { recursive: true, force: true }) + symlinkSync(path.join(other, '.git'), path.join(repo, '.git'), 'dir') + + process.env.WORKSPACE_PATH = tmp + + await expect(resolveSessionSandboxMounts(repo)).resolves.toBeNull() + } finally { + restoreWorkspacePath(originalWorkspacePath) + rmSync(tmp, { recursive: true, force: true }) + } + }) + + it('rejects a worktree whose git file is a symlink to another repository linked worktree git file', async () => { + const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-session-symlink-worktree-')) + const originalWorkspacePath = process.env.WORKSPACE_PATH + try { + const repos = path.join(tmp, 'repos') + const main = path.join(repos, 'main') + const other = path.join(repos, 'other') + const otherWorktree = path.join(repos, 'other-linked') + const worktree = path.join(tmp, 'schedule-worktrees', 'job-1-run-2') + mkdirSync(main, { recursive: true }) + mkdirSync(other, { recursive: true }) + mkdirSync(path.dirname(worktree), { recursive: true }) + runGit(['init', '-q'], main) + runGit(['init', '-q'], other) + runGit( + ['-c', 'user.email=ocm@example.com', '-c', 'user.name=OCM', 'commit', '--allow-empty', '-q', '-m', 'init'], + main, + ) + runGit( + ['-c', 'user.email=ocm@example.com', '-c', 'user.name=OCM', 'commit', '--allow-empty', '-q', '-m', 'init'], + other, + ) + runGit(['worktree', 'add', '-q', '-b', 'feature', worktree], main) + runGit(['worktree', 'add', '-q', '-b', 'linked', otherWorktree], other) + + rmSync(path.join(worktree, '.git'), { force: true }) + symlinkSync(path.join(otherWorktree, '.git'), path.join(worktree, '.git')) + + process.env.WORKSPACE_PATH = tmp + + await expect(resolveSessionSandboxMounts(worktree)).resolves.toBeNull() + } finally { + restoreWorkspacePath(originalWorkspacePath) + rmSync(tmp, { recursive: true, force: true }) + } + }) + + it('rejects a worktree whose git file points at another repository linked worktree', async () => { + const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-session-cross-linked-')) + const originalWorkspacePath = process.env.WORKSPACE_PATH + try { + const repos = path.join(tmp, 'repos') + const main = path.join(repos, 'main') + const other = path.join(repos, 'other') + const otherWorktree = path.join(repos, 'other-linked') + const worktree = path.join(tmp, 'schedule-worktrees', 'job-1-run-2') + mkdirSync(main, { recursive: true }) + mkdirSync(other, { recursive: true }) + mkdirSync(path.dirname(worktree), { recursive: true }) + runGit(['init', '-q'], main) + runGit(['init', '-q'], other) + runGit( + ['-c', 'user.email=ocm@example.com', '-c', 'user.name=OCM', 'commit', '--allow-empty', '-q', '-m', 'init'], + main, + ) + runGit( + ['-c', 'user.email=ocm@example.com', '-c', 'user.name=OCM', 'commit', '--allow-empty', '-q', '-m', 'init'], + other, + ) + runGit(['worktree', 'add', '-q', '-b', 'feature', worktree], main) + runGit(['worktree', 'add', '-q', '-b', 'linked', otherWorktree], other) + + const otherGitDir = path.join(other, '.git', 'worktrees', path.basename(otherWorktree)) + writeFileSync(path.join(worktree, '.git'), `gitdir: ${otherGitDir}\n`) + + process.env.WORKSPACE_PATH = tmp + + await expect(resolveSessionSandboxMounts(worktree)).resolves.toBeNull() + } finally { + restoreWorkspacePath(originalWorkspacePath) + rmSync(tmp, { recursive: true, force: true }) + } + }) })