fix: handle dotdot-prefixed worktree child paths (#3644)
This commit is contained in:
@@ -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))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user