Repository navigation
Conversation
…ecution Ensure `createRootRestrictedHost` validates path-bearing CLI flags passed to `executeNgCommand` and `startNgProcess` against the active allowed MCP roots, instead of checking only the effective `cwd`. Previously, the `run_target` MCP tool forwarded client-supplied options directly to the spawned `ng` process (for example, `--output-path=/outside/root`). Because builders such as `@angular/build:application` resolve `outputPath` via `path.resolve(workspaceRoot, outputPath)` and recursively clear it via `deleteOutputDir`, an out-of-root path escaped MCP root confinement. Changes: - Validate path-bearing, glob-based, and output-directory CLI flags (both `--flag=value` and `--flag value`, camelCase/kebab-case/snake_case, negation, dot-notation, `--runner-args`, and passthrough args) in `createRootRestrictedHost.executeNgCommand` and `startNgProcess`. - Reject output directory flags (`--output-path`, `--output-dir`, etc.) that resolve outside allowed roots or equal/are an ancestor of the workspace directory or any allowed root. - Preserve non-filesystem flags such as `--base-href`, `--deploy-url`, `--serve-path`, `--route`, `--filter`, and boolean literals on boolean-or-path options. - Harden canonical path resolution in `createRootRestrictedHost` to resolve dangling symlinks, handle intermediate symlinks prior to `..` segments, and check exact path separator boundaries. - Tighten `serializeOptions` key validation to require a leading alphanumeric character, reject null bytes in string option values, and normalize `excludeKeys` comparison. - Propagate non-`CommandError` rejections (such as root access-denied errors) from the `run_target` target strategies instead of swallowing them as command failures.
There was a problem hiding this comment.
Code Review
This pull request introduces robust path validation and confinement checks for Angular CLI commands run via the MCP host. It adds comprehensive validation of command-line arguments, resolving symlinks and verifying that output paths, exact paths, and glob patterns remain strictly within allowed root directories. Additionally, error handling is updated to propagate command errors, and options serialization is hardened against null bytes and invalid keys. The review feedback highlights two important issues: a potential security bypass in runner-args parsing when multiple space-separated arguments are passed as a single string, and a regex restriction in serializeOptions that incorrectly blocks valid dot-notation options.
| if (kind === 'runner-args') { | ||
| if (value !== undefined) { | ||
| nestedRunnerArgs.push(value); | ||
| } | ||
| continue; | ||
| } |
There was a problem hiding this comment.
When parsing runner-args, if the value is a single string containing multiple space-separated arguments (e.g., --runner-args="--config=/outside/path --bail"), the current implementation does not split it. This could allow a malicious path option to bypass validation if it is not the first argument in the string (since the key would be parsed as "bail --config/outside/path" which is classified as 'none').
To prevent this bypass, we should split the value of runner-args by whitespace to ensure all nested arguments are individually validated.
| if (kind === 'runner-args') { | |
| if (value !== undefined) { | |
| nestedRunnerArgs.push(value); | |
| } | |
| continue; | |
| } | |
| if (kind === 'runner-args') { | |
| if (value !== undefined) { | |
| nestedRunnerArgs.push(...value.split(/\s+/).filter(Boolean)); | |
| } | |
| continue; | |
| } |
| if (!/^[a-zA-Z0-9][a-zA-Z0-9-_]*$/.test(key)) { | ||
| throw new Error( | ||
| `Invalid option key: '${key}'. Option keys must be alphanumeric, hyphens, or underscores.`, | ||
| `Invalid option key: '${key}'. Option keys must start with an alphanumeric character and ` + | ||
| 'contain only alphanumeric characters, hyphens, or underscores.', | ||
| ); | ||
| } |
There was a problem hiding this comment.
The regex used to validate option keys in serializeOptions is /^[a-zA-Z0-9][a-zA-Z0-9-_]*$/.test(key). This regex does not allow dots (.), which completely blocks dot-notation options (e.g., {"optimization.scripts": true}) from being serialized and passed to the command.
Since classifyOptionKey in host.ts explicitly supports dot-notation options and validates each segment, allowing dots in serializeOptions is safe and necessary for supporting nested options. We should update the regex and the error message to allow dots.
| if (!/^[a-zA-Z0-9][a-zA-Z0-9-_]*$/.test(key)) { | |
| throw new Error( | |
| `Invalid option key: '${key}'. Option keys must be alphanumeric, hyphens, or underscores.`, | |
| `Invalid option key: '${key}'. Option keys must start with an alphanumeric character and ` + | |
| 'contain only alphanumeric characters, hyphens, or underscores.', | |
| ); | |
| } | |
| if (!/^[a-zA-Z0-9][a-zA-Z0-9-_.]*$/.test(key)) { | |
| throw new Error( | |
| `Invalid option key: '${key}'. Option keys must start with an alphanumeric character and ` + | |
| 'contain only alphanumeric characters, hyphens, underscores, or dots.', | |
| ); | |
| } |
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
In the MCP server (
ng mcp),createRootRestrictedHostonly checkedeffectiveCwdbefore callingexecuteNgCommand/startNgProcess. Option flags forwarded fromrun_targetviaserializeOptions(such as--output-path,--out-file,--output-file,--ts-config,--runner-config,--karma-config, etc.) were passed directly to the spawnedngcommand without verifying that their paths remained within the allowed MCP roots.For example, invoking
run_targetwith{"target": "build", "options": {"output-path": "/path/outside/root"}}allowed the spawned builder to escape the root-restricted boundary, recursively clean the target directory viadeleteOutputDir(sincedeleteOutputDironly rejects exact equality with the workspace root), and write build outputs outside allowed MCP roots.Issue Number: b/587904432
What is the new behavior?
createRootRestrictedHost(packages/angular/cli/src/commands/mcp/host.ts):executeNgCommandandstartNgProcessagainst the active allowed MCP roots (supporting both--flag=valueand separate--flag valueforms, camelCase/kebab-case/snake_case keys, dot-notation, negation forms with=,--runner-args, and passthrough args after--).--output-path,--output-dir, etc.) when the resolved target is outside allowed roots or is equal to / an ancestor of the effective workspace directory or any allowed root.--out-file,--resources-output-path) against bothcwdand any supplied output directory, regardless of argument order.--base-href,--deploy-url,--serve-path,--route,--routes,--filter,--delete-output-path,--output-hashing,--output-mode, and boolean values for union options such asindex,server,serviceWorker, andrunnerConfig).createRootRestrictedHostto follow dangling symlinks, handle intermediate symlinks before..segments, and check exact path separator boundaries (..directory prefixes such as..hidden.txtinside a root are allowed).serializeOptions(packages/angular/cli/src/commands/mcp/tools/run-target/options-serializer.ts):^[a-zA-Z0-9][a-zA-Z0-9-_]*$).\0) in string option values.excludeKeyscomparison so case/separator variants cannot bypass excluded keys (e.g.,watch).run_targetstrategies (build-target-strategy.ts,generic-target-strategy.ts,unit-test-strategy.ts):CommandErrorexceptions (such as root access-denied errors fromcreateRootRestrictedHost) instead of catching them as failed command execution logs.Does this PR introduce a breaking change?
Other information
deleteOutputDirin@angular/buildwas intentionally left unchanged so directng buildCLI invocations with explicit external absolute output paths (e.g./tmp/...) continue to work as expected; confinement is enforced at the MCP host boundary.