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(worktree): guard wtPath against argv flag injection (-- separator + leading-dash reject)

enes5ce560892c40
diff --git a/src/shared/worktree-ops.test.ts b/src/shared/worktree-ops.test.ts
index a7db8bb1e..94b113866 100644
--- a/src/shared/worktree-ops.test.ts
+++ b/src/shared/worktree-ops.test.ts
@@ -15,11 +15,17 @@ function fakeGit(handlers: Record<string, GitExec>) {
}
describe('worktreeAdd', () => {
- it('uses --no-track -b for a new branch', async () => {
+ it('uses --no-track -b and a -- separator for a new branch', async () => {
const { git, calls } = fakeGit({})
const r = await worktreeAdd(git, '/repo', '/wt/x', 'feature/x', 'main', true)
expect(r.ok).toBe(true)
- expect(calls[0]).toEqual(['worktree', 'add', '--no-track', '-b', 'feature/x', '/wt/x', 'main'])
+ expect(calls[0]).toEqual(['worktree', 'add', '--no-track', '-b', 'feature/x', '--', '/wt/x', 'main'])
+ })
+ it('uses a -- separator for an existing branch', async () => {
+ const { git, calls } = fakeGit({})
+ const r = await worktreeAdd(git, '/repo', '/wt/x', 'feature/x', 'main', false)
+ expect(r.ok).toBe(true)
+ expect(calls[0]).toEqual(['worktree', 'add', '--', '/wt/x', 'feature/x'])
})
it('rejects a flag-injecting branch name without calling git', async () => {
const { git, calls } = fakeGit({})
@@ -27,6 +33,12 @@ describe('worktreeAdd', () => {
expect(r.ok).toBe(false)
expect(calls.length).toBe(0)
})
+ it('rejects a flag-injecting worktree path without calling git', async () => {
+ const { git, calls } = fakeGit({})
+ const r = await worktreeAdd(git, '/repo', '--evil', 'feature/x', 'main', true)
+ expect(r.ok).toBe(false)
+ expect(calls.length).toBe(0)
+ })
})
describe('worktreeMerge', () => {
@@ -67,6 +79,12 @@ describe('worktreeRemove', () => {
expect(r.ok).toBe(false)
expect(calls.length).toBe(0)
})
+ it('rejects a flag-injecting worktree path without calling git', async () => {
+ const { git, calls } = fakeGit({})
+ const r = await worktreeRemove(git, '/repo', '--evil', '/home', false)
+ expect(r.ok).toBe(false)
+ expect(calls.length).toBe(0)
+ })
it('refuses an unregistered worktree path', async () => {
const { git } = fakeGit({ 'worktree list --porcelain': ok('worktree /repo\nbranch refs/heads/main\n') })
const r = await worktreeRemove(git, '/repo', '/wt/ghost', '/home', false)
@@ -77,7 +95,7 @@ describe('worktreeRemove', () => {
const { git, calls } = fakeGit({ 'worktree list --porcelain': ok(list) })
const r = await worktreeRemove(git, '/repo', '/wt/x', '/home', true)
expect(r.ok).toBe(true)
- expect(calls.some((c) => c.join(' ') === 'worktree remove --force /wt/x')).toBe(true)
+ expect(calls.some((c) => c.join(' ') === 'worktree remove --force -- /wt/x')).toBe(true)
expect(calls.some((c) => c.join(' ') === 'worktree prune')).toBe(true)
expect(calls.some((c) => c.join(' ') === 'branch -d feature/x')).toBe(true)
})
diff --git a/src/shared/worktree-ops.ts b/src/shared/worktree-ops.ts
index d8275c3e7..dbbae9498 100644
--- a/src/shared/worktree-ops.ts
+++ b/src/shared/worktree-ops.ts
@@ -29,12 +29,15 @@ export async function worktreeAdd(
git: GitExecutor, repoPath: string, wtPath: string, branch: string, baseRef: string, isNew: boolean
): Promise<WorktreeOpResult> {
if (!repoPath || !wtPath) return { ok: false, message: 'Missing repo or worktree path.' }
+ // Reject a path that could be parsed as an option flag (argv injection).
+ if (wtPath.startsWith('-')) return { ok: false, message: 'Invalid worktree path.' }
if (!isValidGitRef(branch)) return { ok: false, message: 'Invalid branch name.' }
if (isNew && !isValidGitRef(baseRef)) return { ok: false, message: 'Invalid base ref.' }
// `--no-track` so a new branch does not inherit upstream and report "behind".
+ // `--` ends option parsing so wtPath can never be read as a flag (verified git ≥2.39).
const args = isNew
- ? ['worktree', 'add', '--no-track', '-b', branch, wtPath, baseRef]
- : ['worktree', 'add', wtPath, branch]
+ ? ['worktree', 'add', '--no-track', '-b', branch, '--', wtPath, baseRef]
+ : ['worktree', 'add', '--', wtPath, branch]
const r = await git(repoPath, args)
return r.ok ? { ok: true, message: `Worktree ready at ${wtPath}.` } : { ok: false, message: r.err }
}
@@ -74,6 +77,8 @@ export async function worktreeMerge(
export async function worktreeRemove(
git: GitExecutor, repoPath: string, wtPath: string, homeDir: string, deleteBranch: boolean
): Promise<WorktreeOpResult> {
+ // Reject a path that could be parsed as an option flag (argv injection).
+ if (!wtPath || wtPath.startsWith('-')) return { ok: false, message: 'Invalid worktree path.' }
if (isDangerousWorktreeRemovalPath(wtPath, repoPath, homeDir)) {
return { ok: false, message: 'Refusing to remove that path.' }
}
@@ -81,7 +86,8 @@ export async function worktreeRemove(
const entry = list.find((e) => e.path.replace(/\/+$/, '') === wtPath.replace(/\/+$/, ''))
if (!entry) return { ok: false, message: 'Worktree is not registered.' }
const branch = entry.branch
- const rm = await git(repoPath, ['worktree', 'remove', '--force', wtPath])
+ // `--` ends option parsing so wtPath can never be read as a flag (verified git ≥2.39).
+ const rm = await git(repoPath, ['worktree', 'remove', '--force', '--', wtPath])
if (!rm.ok) return { ok: false, message: rm.err }
await git(repoPath, ['worktree', 'prune'])
if (deleteBranch && branch && isValidGitRef(branch)) {