Skip to content

Commit c492a9d

Browse files
committed
fixup! fix(@angular/cli): validate path options against MCP roots in host execution
1 parent 196b357 commit c492a9d

4 files changed

Lines changed: 236 additions & 59 deletions

File tree

‎packages/angular/cli/src/commands/mcp/host.ts‎

Lines changed: 128 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ const EXACT_PATH_OPTIONS = new Set([
351351
'configfile',
352352
'coveragedir',
353353
'coveragedirectory',
354+
'coveragereporters',
354355
'eslintconfig',
355356
'filereplacements',
356357
'index',
@@ -365,6 +366,7 @@ const EXACT_PATH_OPTIONS = new Set([
365366
'project',
366367
'providersfile',
367368
'proxyconfig',
369+
'reporters',
368370
'routesfile',
369371
'runnerconfig',
370372
'scripts',
@@ -434,7 +436,7 @@ function stripNegationPrefix(segment: string): string {
434436
return segment.toLowerCase().startsWith('no-') ? segment.slice(3) : segment;
435437
}
436438

437-
function classifySingleKey(rawSegment: string): OptionPathKind {
439+
function classifySingleKey(rawSegment: string, isRunnerContext = false): OptionPathKind {
438440
const segment = stripNegationPrefix(rawSegment);
439441
const compact = normalizeOptionKey(segment);
440442

@@ -453,7 +455,11 @@ function classifySingleKey(rawSegment: string): OptionPathKind {
453455
if (GLOB_PATH_OPTIONS.has(compact)) {
454456
return 'glob';
455457
}
456-
if (EXACT_PATH_OPTIONS.has(compact) || compact === 'o') {
458+
if (
459+
EXACT_PATH_OPTIONS.has(compact) ||
460+
compact === 'o' ||
461+
(isRunnerContext && (compact === 'c' || compact === 'r'))
462+
) {
457463
return 'path';
458464
}
459465

@@ -465,21 +471,21 @@ function classifySingleKey(rawSegment: string): OptionPathKind {
465471
return 'none';
466472
}
467473

468-
function classifyOptionKey(rawKey: string): OptionPathKind {
474+
function classifyOptionKey(rawKey: string, isRunnerContext = false): OptionPathKind {
469475
const dotSegments = rawKey.split('.');
470476
if (dotSegments.length === 1) {
471-
return classifySingleKey(dotSegments[0]);
477+
return classifySingleKey(dotSegments[0], isRunnerContext);
472478
}
473479

474-
const rootKind = classifySingleKey(dotSegments[0]);
480+
const rootKind = classifySingleKey(dotSegments[0], isRunnerContext);
475481
const leafSegment = dotSegments.at(-1) ?? '';
476482
const leafCompact = normalizeOptionKey(leafSegment);
477483

478484
if (rootKind === 'output-dir') {
479485
return leafCompact === 'base' ? 'output-dir' : 'output-relative';
480486
}
481487

482-
const leafKind = classifySingleKey(leafSegment);
488+
const leafKind = classifySingleKey(leafSegment, isRunnerContext);
483489
if (leafKind !== 'none') {
484490
return leafKind;
485491
}
@@ -680,36 +686,72 @@ function assertNotWorkspaceOrRootAncestor(
680686
}
681687
}
682688

683-
function validateGlobOptionValue(
689+
function validateOutputRelativeOptionValue(
684690
roots: readonly string[],
685691
flag: string,
692+
rawValue: string,
693+
cwd: string,
694+
realCwd: string,
695+
): void {
696+
if (rawValue.includes('\0')) {
697+
throw new Error(`Access denied: option '${flag}' contains a null byte.`);
698+
}
699+
700+
if (isAbsolute(rawValue)) {
701+
throw new Error(
702+
`Access denied: option '${flag}' path '${rawValue}' must be relative to the output directory.`,
703+
);
704+
}
705+
706+
if (!isPathWithinRoot(cwd, resolve(cwd, rawValue))) {
707+
throw new Error(
708+
`Access denied: option '${flag}' path '${rawValue}' must not traverse outside the output directory.`,
709+
);
710+
}
711+
712+
validateOptionPathValue(roots, flag, rawValue, cwd, realCwd);
713+
}
714+
715+
function validateGlobOptionValue(
716+
roots: readonly string[],
717+
flag: string | undefined,
686718
pattern: string,
687719
cwd: string,
688720
realCwd: string,
689721
): void {
690722
if (pattern.includes('\0')) {
691-
throw new Error(`Access denied: option '${flag}' contains a null byte.`);
723+
throw new Error(
724+
flag
725+
? `Access denied: option '${flag}' contains a null byte.`
726+
: 'Access denied: glob pattern contains a null byte.',
727+
);
692728
}
693729

694-
const normalized = pattern.startsWith('!') ? pattern.slice(1) : pattern;
730+
const normalized = pattern.replace(/^!+/, '');
695731
if (!normalized) {
696732
return;
697733
}
698734

699-
const label = `option '${flag}' pattern '${pattern}'`;
735+
const label = flag ? `option '${flag}' pattern '${pattern}'` : `glob pattern '${pattern}'`;
700736
const firstWildcardIndex = normalized.search(/[*?[{]/);
701737
const basePath = firstWildcardIndex >= 0 ? normalized.slice(0, firstWildcardIndex) : normalized;
702738

703739
if (firstWildcardIndex >= 0) {
704740
const wildcardTail = normalized.slice(firstWildcardIndex);
705-
if (/(?:^|[\\/{,])\.\.(?:[\\/},]|$)/.test(wildcardTail)) {
741+
if (/(?:^|[\\/{,!])\.\.(?:[\\/},]|$)/.test(wildcardTail)) {
706742
throw new Error(`Access denied: ${label} contains path traversal sequences.`);
707743
}
708744

709-
if (normalized.startsWith('{')) {
745+
let searchPos = 0;
746+
while (searchPos < normalized.length) {
747+
const braceOpenIndex = normalized.indexOf('{', searchPos);
748+
if (braceOpenIndex === -1) {
749+
break;
750+
}
751+
710752
let braceDepth = 0;
711753
let braceCloseIndex = normalized.length;
712-
for (let i = 0; i < normalized.length; i++) {
754+
for (let i = braceOpenIndex; i < normalized.length; i++) {
713755
if (normalized[i] === '{') {
714756
braceDepth++;
715757
} else if (normalized[i] === '}') {
@@ -721,17 +763,27 @@ function validateGlobOptionValue(
721763
}
722764
}
723765

724-
const braceContent = normalized.slice(1, braceCloseIndex);
725-
for (const token of braceContent.split(/[{},]/)) {
766+
const prefixBeforeBrace = normalized.slice(0, braceOpenIndex);
767+
const hasWildcardInPrefix = /[*?[{]/.test(prefixBeforeBrace);
768+
const braceBaseDir = hasWildcardInPrefix ? cwd : resolve(cwd, prefixBeforeBrace);
769+
const braceContent = normalized.slice(braceOpenIndex + 1, braceCloseIndex);
770+
771+
for (const rawToken of braceContent.split(/[{},]/)) {
772+
const token = rawToken.replace(/^!+/, '');
726773
if (!token) {
727774
continue;
728775
}
729776
const tokenWildcard = token.search(/[*?[]/);
730777
const tokenBase = tokenWildcard >= 0 ? token.slice(0, tokenWildcard) : token;
731778
if (tokenBase) {
732-
assertRealPathAllowed(roots, resolve(cwd, tokenBase), label);
779+
assertRealPathAllowed(roots, resolve(braceBaseDir, tokenBase), label);
780+
if (braceBaseDir !== cwd) {
781+
assertRealPathAllowed(roots, resolve(cwd, tokenBase), label);
782+
}
733783
}
734784
}
785+
786+
searchPos = braceCloseIndex + 1;
735787
}
736788
}
737789

@@ -758,35 +810,68 @@ function validateNgCommandArgs(
758810
const outputDirs: Array<{ raw: string; real: string }> = [];
759811
const outputRelativeChecks: Array<{ flag: string; value: string }> = [];
760812
const nestedRunnerArgs: string[] = [];
813+
let inPassthrough = false;
761814

762815
for (let i = 0; i < args.length; i++) {
763816
const arg = args[i];
764-
if (!arg.startsWith('-') || arg === '-' || arg === '--') {
817+
if (arg === '--') {
818+
inPassthrough = true;
819+
continue;
820+
}
821+
if (!arg.startsWith('-') || arg === '-') {
765822
continue;
766823
}
767824

768-
const withoutPrefix = arg.startsWith('--') ? arg.slice(2) : arg.slice(1);
825+
const isRunnerContext = depth > 0 || inPassthrough;
826+
const isLongFlag = arg.startsWith('--');
827+
const withoutPrefix = isLongFlag ? arg.slice(2) : arg.slice(1);
769828
const eqIndex = withoutPrefix.indexOf('=');
770829
const hasEquals = eqIndex !== -1;
771-
const rawKey = hasEquals ? withoutPrefix.slice(0, eqIndex) : withoutPrefix;
772-
const kind = classifyOptionKey(rawKey);
830+
let rawKey = hasEquals ? withoutPrefix.slice(0, eqIndex) : withoutPrefix;
831+
let attachedShortValue: string | undefined;
832+
833+
if (!isLongFlag && !hasEquals && rawKey.length > 1) {
834+
const shortChar = rawKey[0];
835+
if (shortChar === 'o' || (isRunnerContext && (shortChar === 'c' || shortChar === 'r'))) {
836+
attachedShortValue = rawKey.slice(1);
837+
rawKey = shortChar;
838+
}
839+
}
840+
841+
const kind = classifyOptionKey(rawKey, isRunnerContext);
773842
if (kind === 'none') {
774843
continue;
775844
}
776845

777-
const flag = arg.startsWith('--') ? `--${rawKey}` : `-${rawKey}`;
846+
const flag = isLongFlag ? `--${rawKey}` : `-${rawKey}`;
778847
const isNegated = rawKey.toLowerCase().startsWith('no-');
848+
const compactKey = normalizeOptionKey(stripNegationPrefix(rawKey));
849+
const canBeBareFlag = BOOLEAN_OR_PATH_OPTIONS.has(compactKey) || rawKey === 'o';
779850

780851
let value: string | undefined;
781852
if (hasEquals) {
782853
value = withoutPrefix.slice(eqIndex + 1);
783-
} else if (!isNegated && i + 1 < args.length && !args[i + 1].startsWith('-')) {
784-
value = args[++i];
854+
} else if (attachedShortValue !== undefined) {
855+
value = attachedShortValue;
856+
} else if (!isNegated && i + 1 < args.length) {
857+
const nextArg = args[i + 1];
858+
const isStandardFlag = /^--?[a-zA-Z0-9][a-zA-Z0-9-_.]*(?:=.*)?$/.test(nextArg);
859+
if (
860+
kind === 'runner-args' ||
861+
!nextArg.startsWith('-') ||
862+
!isStandardFlag ||
863+
(!canBeBareFlag && !nextArg.startsWith('--'))
864+
) {
865+
value = args[++i];
866+
}
785867
}
786868

787869
if (kind === 'runner-args') {
788870
if (value !== undefined) {
789871
nestedRunnerArgs.push(value);
872+
if (/\s--?[a-zA-Z]/.test(value)) {
873+
nestedRunnerArgs.push(...value.split(/\s+/).filter(Boolean));
874+
}
790875
}
791876
continue;
792877
}
@@ -809,7 +894,6 @@ function validateNgCommandArgs(
809894
continue;
810895
}
811896

812-
const compactKey = normalizeOptionKey(stripNegationPrefix(rawKey));
813897
if (
814898
BOOLEAN_OR_PATH_OPTIONS.has(compactKey) &&
815899
(value === 'true' || value === 'false' || value === '')
@@ -820,7 +904,7 @@ function validateNgCommandArgs(
820904
if (kind === 'glob') {
821905
validateGlobOptionValue(roots, flag, value, cwd, realCwd);
822906
} else if (kind === 'output-relative') {
823-
validateOptionPathValue(roots, flag, value, cwd, realCwd);
907+
validateOutputRelativeOptionValue(roots, flag, value, cwd, realCwd);
824908
outputRelativeChecks.push({ flag, value });
825909
} else {
826910
validateOptionPathValue(roots, flag, value, cwd, realCwd);
@@ -829,7 +913,23 @@ function validateNgCommandArgs(
829913

830914
for (const { flag, value } of outputRelativeChecks) {
831915
for (const outputDir of outputDirs) {
832-
validateOptionPathValue(roots, flag, value, outputDir.raw, outputDir.real);
916+
const { realPath, walkedPath } = validateOptionPathValue(
917+
roots,
918+
flag,
919+
value,
920+
outputDir.raw,
921+
outputDir.real,
922+
);
923+
if (!isPathWithinRoot(outputDir.real, realPath)) {
924+
throw new Error(
925+
`Access denied: option '${flag}' path '${value}' must not traverse outside the output directory.`,
926+
);
927+
}
928+
if (walkedPath && !isPathWithinRoot(outputDir.real, walkedPath)) {
929+
throw new Error(
930+
`Access denied: option '${flag}' path '${value}' must not traverse outside the output directory.`,
931+
);
932+
}
833933
}
834934
}
835935

@@ -870,19 +970,8 @@ export function createRootRestrictedHost(
870970
return baseHost.readFile(path, encoding);
871971
},
872972
glob(pattern: string, options: { cwd: string }) {
873-
if (pattern.includes('..')) {
874-
throw new Error(
875-
`Access denied: glob pattern '${pattern}' contains path traversal sequences.`,
876-
);
877-
}
878-
879-
checkPath(options.cwd);
880-
881-
const firstWildcardIndex = pattern.search(/[*?[{]/);
882-
const basePath = firstWildcardIndex >= 0 ? pattern.substring(0, firstWildcardIndex) : pattern;
883-
884-
const targetDir = resolve(options.cwd, basePath);
885-
checkPath(targetDir);
973+
const realCwd = checkPath(options.cwd);
974+
validateGlobOptionValue(roots, undefined, pattern, options.cwd, realCwd);
886975

887976
return baseHost.glob(pattern, options);
888977
},

0 commit comments

Comments
 (0)