diff --git a/v3/@claude-flow/cli/__tests__/validate-input-path-traversal-5218.test.ts b/v3/@claude-flow/cli/__tests__/validate-input-path-traversal-5218.test.ts new file mode 100644 index 0000000000..1351251831 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/validate-input-path-traversal-5218.test.ts @@ -0,0 +1,26 @@ +/** + * #5218: the CLI boundary must reject a standalone `..` segment at any + * position. cli-core's current `/\.\.[/\\]/` misses a leading or trailing + * `..`, while the CLI shim covers every MCP tool until cli-core's next leaf + * release carries the same segment rule. + */ +import { describe, it, expect } from 'vitest'; +import { validatePath } from '../src/mcp-tools/validate-input.js'; + +describe('validatePath standalone .. segments (#5218)', () => { + it.each(['..', 'src/..', 'src\\..', 'a/../b', 'a\\..\\b', '../etc/passwd', '..\\windows\\system32'])( + 'rejects %s', + (value) => { + const result = validatePath(value, 'filePath'); + expect(result.valid).toBe(false); + expect(result.error).toMatch(/path traversal/); + }, + ); + + it.each(['a..b/c', '.../x', '/home/u/repo/file.ts', 'E:\\Repos\\my-app\\file.ts', 'app/page.tsx'])( + 'accepts %s', + (value) => { + expect(validatePath(value, 'filePath').valid).toBe(true); + }, + ); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/validate-input.ts b/v3/@claude-flow/cli/src/mcp-tools/validate-input.ts index 8370728158..898cc11837 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/validate-input.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/validate-input.ts @@ -27,3 +27,29 @@ export function validateIdentifier(value: unknown, label: string): ValidationRes } return result; } + +// Temporary CLI-boundary path validation (#5218). cli-core's current +// `/\.\.[/\\]/` accepts a bare or trailing `..` and false-rejects `.../x`; +// cli-core is a non-bundled leaf, so its source change needs a version bump +// and a publish first. This keeps the exact core checks (4096 chars, shell +// metacharacters, backslash-to-slash sanitization) with the segment rule +// already used by managed-agent-tools.ts and sona-optimizer.ts. Remove this +// override when cli-core ships the same check. +const PATH_TRAVERSAL_SEGMENT = /(?:^|[/\\])\.\.(?:$|[/\\])/; +const PATH_SHELL_META = /[;&|`$(){}[\]<>!#]/; + +export function validatePath(value: unknown, label: string): ValidationResult { + if (typeof value !== 'string' || value.length === 0) { + return { valid: false, sanitized: '', error: `${label} must be a non-empty string` }; + } + if (value.length > 4096) { + return { valid: false, sanitized: '', error: `${label} exceeds 4096 characters` }; + } + if (PATH_TRAVERSAL_SEGMENT.test(value)) { + return { valid: false, sanitized: '', error: `${label} contains path traversal (..)` }; + } + if (PATH_SHELL_META.test(value)) { + return { valid: false, sanitized: '', error: `${label} contains shell metacharacters` }; + } + return { valid: true, sanitized: value.replace(/\\/g, '/') }; +}