Files
repomix-mirror/tests/shared/pathUtils.test.ts
Kazuki YamadaandClaude Opus 5.5 a066b5e2d1 refactor(shared): Add isPathWithin and toPosixPath path helpers
Several modules hand-rolled the same path.relative containment check and
the same backslash-to-slash conversion. Move both into src/shared/pathUtils
and use them at the call sites whose semantics match exactly.

The remote config trust check used `relative.startsWith('..')`, which
wrongly refused a config under a child directory named like `..foo`;
isPathWithin only treats a real parent traversal as outside. This is the
one intended behaviour change.

intent(path-helpers): one containment check and one posix conversion instead of copies that drift
decision(is-path-within): path.relative based, equal paths count as within; watchIgnore keeps its own `relative === ''` skip for the root itself
decision(to-posix-path): always converts backslashes (the form used by the replace/replaceAll sites and the old private toPosixPath in fileSearch)
rejected(path-scope): kept the MCP pathScope string-prefix isInside; switching to path.relative would make the sandbox boundary case-insensitive on Windows and accept filesystem roots, loosening a security check
rejected(to-posix-path): `split(path.sep).join('/')` sites (watchIgnore, pathScope toVirtualPath, fileSearch) keep their form; they only convert the host separator, so a literal backslash in a POSIX file name would change meaning
constraint(remote-config-trust): `..foo` children were refused before; now accepted, covered by tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:06:42 +09:00

54 lines
2.3 KiB
TypeScript

import path from 'node:path';
import { describe, expect, test } from 'vitest';
import { isPathWithin, toPosixPath } from '../../src/shared/pathUtils.js';
describe('pathUtils', () => {
describe('isPathWithin', () => {
test('treats equal paths as within', () => {
expect(isPathWithin('/repo', '/repo', path.posix)).toBe(true);
expect(isPathWithin('/repo/', '/repo', path.posix)).toBe(true);
});
test('accepts descendants', () => {
expect(isPathWithin('/repo', '/repo/src/index.ts', path.posix)).toBe(true);
});
test('rejects the parent, siblings, and paths sharing a name prefix', () => {
expect(isPathWithin('/repo', '/', path.posix)).toBe(false);
expect(isPathWithin('/repo', '/other/file.ts', path.posix)).toBe(false);
expect(isPathWithin('/repo', '/repo-evil/file.ts', path.posix)).toBe(false);
});
test('accepts children whose name starts with ".."', () => {
expect(isPathWithin('/repo', '/repo/..foo', path.posix)).toBe(true);
expect(isPathWithin('/repo', '/repo/..cache/file.ts', path.posix)).toBe(true);
});
test('handles Windows paths, including other drives and case-insensitive matches', () => {
expect(isPathWithin('C:\\repo', 'C:\\repo\\src\\index.ts', path.win32)).toBe(true);
expect(isPathWithin('C:\\repo', 'C:\\repo\\..foo\\x.ts', path.win32)).toBe(true);
expect(isPathWithin('C:\\repo', 'c:\\REPO\\src', path.win32)).toBe(true);
expect(isPathWithin('C:\\repo', 'C:\\other', path.win32)).toBe(false);
expect(isPathWithin('C:\\repo', 'D:\\repo\\src', path.win32)).toBe(false);
});
test('uses the host path module by default', () => {
const parent = path.resolve('some', 'dir');
expect(isPathWithin(parent, path.join(parent, 'child'))).toBe(true);
expect(isPathWithin(parent, path.dirname(parent))).toBe(false);
});
});
describe('toPosixPath', () => {
test('converts every backslash to a forward slash', () => {
expect(toPosixPath('src\\core\\index.ts')).toBe('src/core/index.ts');
expect(toPosixPath('C:\\repo\\a/b')).toBe('C:/repo/a/b');
});
test('leaves POSIX paths unchanged', () => {
expect(toPosixPath('src/core/index.ts')).toBe('src/core/index.ts');
expect(toPosixPath('')).toBe('');
});
});
});