diff --git a/packages/cli/src/create/__tests__/prompts.spec.ts b/packages/cli/src/create/__tests__/prompts.spec.ts index 4c75a2d74e..d179bfa372 100644 --- a/packages/cli/src/create/__tests__/prompts.spec.ts +++ b/packages/cli/src/create/__tests__/prompts.spec.ts @@ -4,13 +4,15 @@ import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -const { mockSelect } = vi.hoisted(() => ({ +const { mockSelect, mockText } = vi.hoisted(() => ({ mockSelect: vi.fn(), + mockText: vi.fn(), })); vi.mock('@voidzero-dev/vite-plus-prompts', () => ({ isCancel: () => false, select: mockSelect, + text: mockText, })); vi.mock('../../utils/prompts.ts', () => ({ @@ -23,7 +25,7 @@ vi.mock('../../utils/terminal.ts', () => ({ accent: (value: string) => value, })); -const { checkProjectDirExists, isTargetDirAvailable, suggestAvailableTargetDir } = +const { checkProjectDirExists, isTargetDirAvailable, promptTargetDir, suggestAvailableTargetDir } = await import('../prompts.js'); const tempDirs: string[] = []; @@ -76,6 +78,24 @@ describe('target directory helpers', () => { expect(isTargetDirAvailable(targetDir)).toBe(false); }); + it('rejects ".." path segments but accepts directory names that contain two dots', async () => { + const cwd = makeTempDir(); + let validate: ((value: string | undefined) => string | undefined) | undefined; + mockText.mockImplementation((options: { validate: typeof validate }) => { + validate = options.validate; + return Promise.resolve('my..lib'); + }); + + await expect(promptTargetDir('my-project', true, { cwd })).resolves.toBe('my..lib'); + + const parentError = 'Relative path contains ".." which is not allowed'; + expect(validate?.('my..lib')).toBeUndefined(); + expect(validate?.('foo/my..lib')).toBeUndefined(); + expect(validate?.('..')).toBe(parentError); + expect(validate?.('../my-project')).toBe(parentError); + expect(validate?.('foo/../../my-project')).toBe(parentError); + }); + it('suggests a different target directory when the default already exists', () => { const cwd = makeTempDir(); fs.mkdirSync(path.join(cwd, 'fate-template'), { recursive: true }); diff --git a/packages/cli/src/create/__tests__/utils.spec.ts b/packages/cli/src/create/__tests__/utils.spec.ts index 4529ae0b2c..e52df9cf0a 100644 --- a/packages/cli/src/create/__tests__/utils.spec.ts +++ b/packages/cli/src/create/__tests__/utils.spec.ts @@ -80,6 +80,23 @@ describe('formatTargetDir', () => { expect(formatTargetDir('../../foo/bar')).matchSnapshot(); }); + it('rejects ".." path segments but accepts names that contain two dots', () => { + const parentError = 'Relative path contains ".." which is not allowed'; + expect(formatTargetDir('..').error).toBe(parentError); + expect(formatTargetDir('../my-package').error).toBe(parentError); + expect(formatTargetDir('foo/../../my-package').error).toBe(parentError); + + expect(formatTargetDir('my..lib')).toEqual({ directory: 'my..lib', packageName: 'my..lib' }); + expect(formatTargetDir('./foo/my..lib')).toEqual({ + directory: 'foo/my..lib', + packageName: 'my..lib', + }); + expect(formatTargetDir('foo/@my-scope/my..lib')).toEqual({ + directory: 'foo/my..lib', + packageName: '@my-scope/my..lib', + }); + }); + // Regression test for https://github.com/voidzero-dev/vite-plus/issues/938 // On Windows, path.join/normalize produce backslashes which break when passed as CLI args. // The snapshots pin forward slashes in every nested directory. diff --git a/packages/cli/src/create/prompts.ts b/packages/cli/src/create/prompts.ts index d2ace5bcac..fb8d42c661 100644 --- a/packages/cli/src/create/prompts.ts +++ b/packages/cli/src/create/prompts.ts @@ -236,7 +236,7 @@ function validateTargetDir(input?: string, cwd?: string): { directory: string; e if (path.isAbsolute(targetDir)) { return { directory: '', error: 'Absolute path is not allowed' }; } - if (targetDir.includes('..')) { + if (targetDir.split(/[\\/]/).includes('..')) { return { directory: '', error: 'Relative path contains ".." which is not allowed' }; } if (cwd && !isTargetDirAvailable(path.join(cwd, targetDir))) { diff --git a/packages/cli/src/create/utils.ts b/packages/cli/src/create/utils.ts index a3cc8dea88..be28eacc1a 100644 --- a/packages/cli/src/create/utils.ts +++ b/packages/cli/src/create/utils.ts @@ -210,7 +210,7 @@ export function formatTargetDir(input: string): { error: 'Absolute path is not allowed', }; } - if (targetDir.includes('..')) { + if (targetDir.split(/[\\/]/).includes('..')) { return { directory: '', packageName: '',