diff --git a/src/main/pty/overlay-mirror.test.ts b/src/main/pty/overlay-mirror.test.ts index 49af0c51a..5499d2264 100644 --- a/src/main/pty/overlay-mirror.test.ts +++ b/src/main/pty/overlay-mirror.test.ts @@ -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('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() + }) })