fix: handle dotdot-prefixed worktree child paths (#3644)

This commit is contained in:
Neil
2026-05-30 12:53:34 -07:00
committed by GitHub
parent 522c984c48
commit e372bca2d0
3 changed files with 36 additions and 2 deletions
+5 -1
View File
@@ -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))
)
}
+26
View File
@@ -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(
+5 -1
View File
@@ -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))
)
}