Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/cli/src/create/__tests__/prompts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/create/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/create/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/create/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down
Loading