Skip to content

Commit e7b3397

Browse files
aeschlichrmarti
authored andcommitted
fix npm view exploits
1 parent 8b617bd commit e7b3397

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

extensions/npm/src/features/packageJSONContribution.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ export class PackageJSONContribution implements IJSONContribution {
252252
}
253253

254254
private isValidNPMName(name: string): boolean {
255-
// following rules from https://github.com/npm/validate-npm-package-name
256-
if (!name || name.length > 214 || name.match(/^[_.]/)) {
255+
// following rules from https://github.com/npm/validate-npm-package-name,
256+
// leading slash added as additional security measure
257+
if (!name || name.length > 214 || name.match(/^[-_.\s]/)) {
257258
return false;
258259
}
259-
const match = name.match(/^(?:@([^/]+?)[/])?([^/]+?)$/);
260+
const match = name.match(/^(?:@([^/~\s)('!*]+?)[/])?([^/~)('!*\s]+?)$/);
260261
if (match) {
261262
const scope = match[1];
262263
if (scope && encodeURIComponent(scope) !== scope) {
@@ -284,7 +285,7 @@ export class PackageJSONContribution implements IJSONContribution {
284285

285286
private npmView(npmCommandPath: string, pack: string, resource: Uri | undefined): Promise<ViewPackageInfo | undefined> {
286287
return new Promise((resolve, _reject) => {
287-
const args = ['view', '--json', pack, 'description', 'dist-tags.latest', 'homepage', 'version', 'time'];
288+
const args = ['view', '--json', '--', pack, 'description', 'dist-tags.latest', 'homepage', 'version', 'time'];
288289
const cwd = resource && resource.scheme === 'file' ? dirname(resource.fsPath) : undefined;
289290
cp.execFile(npmCommandPath, args, { cwd }, (error, stdout) => {
290291
if (!error) {

extensions/npm/src/npmMain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
9797
}
9898

9999
async function getNPMCommandPath(): Promise<string | undefined> {
100-
if (canRunNpmInCurrentWorkspace()) {
100+
if (vscode.workspace.isTrusted && canRunNpmInCurrentWorkspace()) {
101101
try {
102102
return await which(process.platform === 'win32' ? 'npm.cmd' : 'npm');
103103
} catch (e) {

0 commit comments

Comments
 (0)