Skip to content

Commit ad6e0f4

Browse files
kyliauKeen Yee Liau
authored andcommitted
fix: do not use require() to read JSON files
This commit fixes a potential security vulnerability caused by the use of require() to read a JSON file. If there is a JS file named `package.json.js` in the node_modules, we would unknowingly execute the remote code while trying to require() it. Even though we only load `typescript` and `@angular/language-service`, we cannot guarantee the content of these packages when they are installed on users' machines. Thanks @ddworken for reporting this issue and @koto for surfacing it to the Angular team.
1 parent 52ac23e commit ad6e0f4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

server/src/version_provider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import * as fs from 'fs';
10+
911
const MIN_TS_VERSION = '3.9';
1012
const MIN_NG_VERSION = '10.0';
1113

@@ -25,7 +27,9 @@ function resolve(packageName: string, location: string, rootPackage?: string): N
2527
const packageJsonPath = require.resolve(`${rootPackage}/package.json`, {
2628
paths: [location],
2729
});
28-
const packageJson = require(packageJsonPath);
30+
// Do not use require() to read JSON files since it's a potential security
31+
// vulnerability.
32+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
2933
const resolvedPath = require.resolve(packageName, {
3034
paths: [location],
3135
});

0 commit comments

Comments
 (0)