Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate plugin name before install #597

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oclif/plugin-plugins",
"description": "plugins plugin for oclif",
"version": "2.4.8",
"version": "3.0.0",
"author": "Salesforce",
"bugs": "https://github.com/oclif/plugin-plugins/issues",
"dependencies": {
Expand All @@ -17,6 +17,7 @@
"semver": "^7.5.0",
"shelljs": "^0.8.5",
"tslib": "^2.4.1",
"validate-npm-package-name": "^5.0.0",
"yarn": "^1.22.18"
},
"devDependencies": {
Expand All @@ -30,6 +31,7 @@
"@types/semver": "^7.3.13",
"@types/shelljs": "^0.8.12",
"@types/supports-color": "^7.2.0",
"@types/validate-npm-package-name": "^4.0.0",
"chai": "^4.3.7",
"commitlint": "^12.1.4",
"eslint": "^7.31.0",
Expand All @@ -46,7 +48,7 @@
"typescript": "4.9.5"
},
"engines": {
"node": ">=12.0.0"
"node": ">=16"
},
"files": [
"oclif.manifest.json",
Expand Down Expand Up @@ -87,4 +89,4 @@
"version": "oclif readme && git add README.md"
},
"types": "lib/index.d.ts"
}
}
11 changes: 10 additions & 1 deletion src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Command, Flags, ux, Args} from '@oclif/core'
import {Command, Flags, ux, Args, Errors} from '@oclif/core'
import * as validate from 'validate-npm-package-name'
import * as chalk from 'chalk'

import Plugins from '../../plugins'
Expand Down Expand Up @@ -85,6 +86,7 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
if (input.includes('@') && input.includes('/')) {
input = input.slice(1)
const [name, tag = 'latest'] = input.split('@')
validateNpmPkgName('@' + name)
return {name: '@' + name, tag, type: 'npm'}
}

Expand All @@ -95,6 +97,13 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins

const [splitName, tag = 'latest'] = input.split('@')
const name = await this.plugins.maybeUnfriendlyName(splitName)
validateNpmPkgName(name)
return {name, tag, type: 'npm'}
}
}

function validateNpmPkgName(name:string): void {
if (!validate(name).validForNewPackages) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at the npm docs https://github.com/npm/validate-npm-package-name#legacy-names

I can't tell how far in time you'd have to go to hit an "old name"

What do you think the chances are that someone using oclif has an npm package that violates the new rules (ex: has a Capital letter in it)?

For safety, I'd consider doing this

  1. make this a major version bump
  2. since we're doing a major anyway, this is a good time to bump pjson.engines to 16+

then we can bump this in our CLIs and most oclif CLIs could, too, unless they happen to need some old-npm -named-plugin?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this issue from 2013, which is before oclif was released:
npm/npm#3914

Not sure about any other legacy rule, let's do a new major 👍🏼

throw new Errors.CLIError('Invalid npm package name.')
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,11 @@
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-7.2.0.tgz#edd98ae52ee786b733a5dea0a23da4eb18ef7310"
integrity sha512-gtUcOP6qIpjbSDdWjMBRNSks42ccx1709mwKTgelW63BESIADw8Ju7klpydDDb9Kr0iRXfpwrXH8+zoU8TCqiA==

"@types/validate-npm-package-name@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/validate-npm-package-name/-/validate-npm-package-name-4.0.0.tgz#3b53194dd3888fbb01f794fa368fb3472381d1fe"
integrity sha512-RpO62vB2lkjEkyLbwTheA2+uwYmtVMWTr/kWRI++UAgVdZqNqdAuIQl/SxBCGeMKfdjWaXPbyhZbiCc4PAj+KA==

"@types/vinyl@^2.0.4":
version "2.0.6"
resolved "https://registry.npmjs.org/@types/vinyl/-/vinyl-2.0.6.tgz#b2d134603557a7c3d2b5d3dc23863ea2b5eb29b0"
Expand Down