fix(cli): reject standalone .. path segments at the MCP boundary (#5220)

The CLI path validator delegated to cli-core's /\.\.[/\\\\]/ check, which accepts a bare '..' or a trailing 'src/..' and false-rejects harmless '.../x'. terminal_create could record the parent as workingDir and hooks_transfer could resolve its source against the parent project (#5218).\n\n- Validate the CLI boundary with a standalone '..' segment at any position, the rule already used by managed-agent-tools.ts and sona-optimizer.ts.\n- Keep the exact core checks: 4096-char cap, shell-metacharacter denylist, backslash-to-slash sanitization, and #2352 Windows-path acceptance.\n- cli-core is a non-bundled leaf: changing its source requires a version bump and a publish first, so the same check moves there on its next leaf release. This shim covers every MCP tool that imports the CLI boundary now.\n\nRegression matrix covers leading, trailing, middle, mixed-separator and near-miss paths. On the pre-change CLI boundary the bare/trailing cases are accepted and .../x is rejected; all 12 pass with the change. Existing #2352 tests pass (22).

Co-authored-by: rUv <ruv@ruv.net>
This commit is contained in:
ordinary beingandrUv authored and GitHub committed 2026-12-01 12:27:50 -05:00
1 parent 7bc4373260
commit 43211177fd
2 files changed
+52

No files matched your search

@@ -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);
},
);
});
@@ -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, '/') };
}