diff --git a/src/main/worktree-orphan-gitdir-proof.ts b/src/main/worktree-orphan-gitdir-proof.ts index bdde6d9ea..b1952fd36 100644 --- a/src/main/worktree-orphan-gitdir-proof.ts +++ b/src/main/worktree-orphan-gitdir-proof.ts @@ -192,9 +192,13 @@ async function adminGitdirPointsAtCandidate( function containsPath(parentPath: string, childPath: string, pathOps: PathOps): boolean { const relativePath = pathOps.relative(parentPath, childPath) + // Why: `..name` is a valid child name; only `..` and `../...` escape. return ( relativePath === '' || - (!!relativePath && !relativePath.startsWith('..') && !pathOps.isAbsolute(relativePath)) + (!!relativePath && + relativePath !== '..' && + !relativePath.startsWith(`..${pathOps.sep}`) && + !pathOps.isAbsolute(relativePath)) ) } diff --git a/src/main/worktree-removal-safety.test.ts b/src/main/worktree-removal-safety.test.ts index cbecf00f0..5aeee3287 100644 --- a/src/main/worktree-removal-safety.test.ts +++ b/src/main/worktree-removal-safety.test.ts @@ -80,6 +80,18 @@ describe('getRegisteredDeletableWorktree', () => { ]) ).toMatchObject({ path: '/workspaces/parent' }) }) + + it('rejects deleting a worktree that contains another registered worktree in a dotdot-prefixed child', () => { + expect(() => + getRegisteredDeletableWorktree('/repo', '/workspaces/parent', [ + makeGitWorktree('/repo', true), + makeGitWorktree('/workspaces/parent'), + makeGitWorktree('/workspaces/parent/..child') + ]) + ).toThrow( + 'Refusing to delete worktree because it contains another registered worktree: /workspaces/parent/..child' + ) + }) }) describe('canSafelyRemoveOrphanedWorktreeDirectory', () => { @@ -97,6 +109,20 @@ describe('canSafelyRemoveOrphanedWorktreeDirectory', () => { ).resolves.toBe(true) }) + it('accepts repo worktree admin entries with dotdot-prefixed directory names', async () => { + await expect( + canSafelyRemoveOrphanedWorktreeDirectory( + '/workspaces/orphan', + '/repo', + makeStatPath(['/workspaces/orphan/.git'], ['/repo/.git']), + makeReadPath([ + ['/workspaces/orphan/.git', 'gitdir: /repo/.git/worktrees/..orphan\n'], + ['/repo/.git/worktrees/..orphan/gitdir', '/workspaces/orphan/.git\n'] + ]) + ) + ).resolves.toBe(true) + }) + it('accepts remote filesystem provider readFile results for linked worktree .git files', async () => { await expect( canSafelyRemoveOrphanedWorktreeDirectory( diff --git a/src/main/worktree-removal-safety.ts b/src/main/worktree-removal-safety.ts index 5fcd962fc..32ae52435 100644 --- a/src/main/worktree-removal-safety.ts +++ b/src/main/worktree-removal-safety.ts @@ -46,9 +46,13 @@ function getPathOps(...paths: string[]): PathOps { function containsPath(parentPath: string, childPath: string, pathOps: PathOps): boolean { const relativePath = pathOps.relative(parentPath, childPath) + // Why: `..name` is a valid child name; only `..` and `../...` escape. return ( relativePath === '' || - (!!relativePath && !relativePath.startsWith('..') && !pathOps.isAbsolute(relativePath)) + (!!relativePath && + relativePath !== '..' && + !relativePath.startsWith(`..${pathOps.sep}`) && + !pathOps.isAbsolute(relativePath)) ) }