Guard overlay cleanup across Windows drives

This commit is contained in:
Neil
2026-05-31 02:18:20 -07:00
parent 2936b4936d
commit 91e19e5c97
+38
View File
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { mkdtemp } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import type * as NodePath from 'node:path'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { safeRemoveOverlay } from './overlay-mirror'
@@ -8,7 +9,10 @@ import { safeRemoveOverlay } from './overlay-mirror'
const tempRoots: string[] = []
afterEach(() => {
vi.doUnmock('fs')
vi.doUnmock('path')
vi.restoreAllMocks()
vi.resetModules()
for (const root of tempRoots.splice(0)) {
rmSync(root, { recursive: true, force: true })
}
@@ -58,4 +62,38 @@ describe('safeRemoveOverlay', () => {
expect(existsSync(marker)).toBe(true)
expect(warnSpy).toHaveBeenCalledOnce()
})
it('refuses Windows overlay targets on a different drive than the root', async () => {
const lstatSyncMock = vi.fn(() => ({
isSymbolicLink: () => false,
isDirectory: () => false
}))
const unlinkSyncMock = vi.fn()
vi.doMock('fs', () => ({
cpSync: vi.fn(),
linkSync: vi.fn(),
lstatSync: lstatSyncMock,
readdirSync: vi.fn(),
rmdirSync: vi.fn(),
symlinkSync: vi.fn(),
unlinkSync: unlinkSyncMock
}))
vi.doMock('path', async () => {
const path = await vi.importActual<typeof NodePath>('node:path')
return {
...path.win32,
default: path.win32
}
})
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const { safeRemoveOverlay: safeRemoveOverlayWithWinPath } = await import('./overlay-mirror')
safeRemoveOverlayWithWinPath(String.raw`D:\users\me\config`, String.raw`C:\orca\overlays`)
expect(warnSpy).toHaveBeenCalledWith(
'[overlay-mirror] refusing to remove overlay outside root: target=D:\\users\\me\\config root=C:\\orca\\overlays'
)
expect(lstatSyncMock).not.toHaveBeenCalled()
expect(unlinkSyncMock).not.toHaveBeenCalled()
})
})