Skip to content

Commit 2779340

Browse files
committed
fix(@angular/cli): validate registry option is a valid URL in ng add
Ensure that the --registry option passed to ng add is parsed as a valid absolute URL. (cherry picked from commit be25199)
1 parent 72c5d09 commit 2779340

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

packages/angular/cli/src/commands/add/cli.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import npa from 'npm-package-arg';
1616
import semver, { Range, compare, intersects, prerelease, satisfies, valid } from 'semver';
1717
import { Argv } from 'yargs';
1818
import {
19+
CommandModuleError,
1920
CommandModuleImplementation,
2021
Options,
2122
OtherOptions,
@@ -131,6 +132,17 @@ export default class AddCommandModule
131132
type: 'boolean',
132133
default: false,
133134
})
135+
.check(({ registry }) => {
136+
if (registry === undefined) {
137+
return true;
138+
}
139+
140+
if (typeof registry === 'string' && URL.canParse(registry)) {
141+
return true;
142+
}
143+
144+
throw new CommandModuleError('Option --registry must be a valid URL.');
145+
})
134146
// Prior to downloading we don't know the full schema and therefore we cannot be strict on the options.
135147
// Possibly in the future update the logic to use the following syntax:
136148
// `ng add @angular/localize -- --package-options`.

tests/e2e/tests/commands/add/registry-option.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
import { getGlobalVariable } from '../../../utils/env';
22
import { expectFileToExist } from '../../../utils/fs';
3-
import { ng } from '../../../utils/process';
3+
import { execAndCaptureError, ng } from '../../../utils/process';
44
import { expectToFail } from '../../../utils/utils';
55

66
export default async function () {
77
const testRegistry = getGlobalVariable('package-registry');
88

9+
// Validate that a non-URL registry string fails with a validation error
10+
const error = await execAndCaptureError('ng', [
11+
'add',
12+
'--registry=not-a-valid-url',
13+
'@angular/pwa',
14+
'--skip-confirmation',
15+
]);
16+
if (!error.message.includes('Option --registry must be a valid URL.')) {
17+
throw new Error(
18+
`Expected registry validation error, but got different error: ${error.message}`,
19+
);
20+
}
21+
22+
// Validate that an empty registry string fails with a validation error
23+
const errorEmpty = await execAndCaptureError('ng', [
24+
'add',
25+
'--registry=',
26+
'@angular/pwa',
27+
'--skip-confirmation',
28+
]);
29+
if (!errorEmpty.message.includes('Option --registry must be a valid URL.')) {
30+
throw new Error(
31+
`Expected registry validation error for empty string, but got: ${errorEmpty.message}`,
32+
);
33+
}
34+
935
// Set an invalid registry
1036
process.env['NPM_CONFIG_REGISTRY'] = 'http://127.0.0.1:9999';
1137

0 commit comments

Comments
 (0)