[RRFC] Improve experience and security around npx and scoped packages
Open
@isaacs is already working on this.
Since Feb 17, 2021.
- Dominant language
- JavaScript
- Stars
- 777
- Forks
- 267
- PR merge metrics
- No merged PRs in 30d
Description
Motivation ("The Why")
- Reduce the amount of typing (and therefore typos) when executing commands in scoped packages
- Same, but for commonly used commands which do not match the package name
- Improve resilience against spoofing (esp. in the light of the recent article about hijacked public names)
Some of those were requests/issues even with the old npx that didn't get much attention.
Despite reporting multiple issues in here, I figure they are all somewhat related and probably need to be considered in context of each other.
Examples
- New feature: directly call a scoped command:
npx @scope/cli- Current behavior: error:
could not determine executable to run - Desired behavior: executes the
cli(or the only existing) bin from the@scope/clipackage. This actually works with thenpxinnpm@6. - I'm not sure how
npm execworks internally... if it's just looking for something that's linked into.bin, then maybe there's quite an amount of work involved, but nevertheless - having this feature, and having it be strict, is a primary fix for any spoofing attacks.
- Current behavior: error:
- New feature: create a config with well-known command mappings, e.g. map
another-commandto always be used from@scope/cli- Current behavior: the user has to always call with a
--packageoption:npx another-command -p @scope/cli - Desired behavior: after configuring a map/alias in
.npmrcthe user can callnpx another-commandwithout providing the-p @scope/cli - There's a large amount of muscle memory to retrain without this...
- Current behavior: the user has to always call with a
- New feature: default
--packageto current folder- Current behavior:
cd [path to @scope/cli] && npx @scope/cli(ornpx another-commandeven) would fail, unless@scope/cliis a dependency of itself - Desired behavior: if I'm in a folder which has a
package.json(or a parent folder has apackage.json, and thatpackage.jsonexposes somebins - I'd like tonpxthose bins, rather than something that still needs to be installed. - Without this, you need to essentially expose the
scriptsas well asbinand remember to usenpm run, instead ofnpm exec. - A workaround I've been using for ages is to
npm i -g ., but it would still be nice to get this.
- Current behavior:
- Potential security issue: disable behavior to use unscoped command from scoped packages
- Current behavior:
npx cliwill execute aclifrom@scope/cliif it is installed - Desired behavior:
npx clishould only execute@scope/cliif@scopeis explicitly allowed via some configuration to use shorthand commands, i.e. the user should explicitly tell where to look for the bin either via config, or via--package, or via the first thing suggested here (i.e. scoping the command call itself). - As this is a breaking change, it can maybe be introduced as a configuration option, and possibly this can be made the default in npm@8
- Current behavior:
- Potential security issue: Disallow running
cliwhen it is installed from multiple scopes- Current behavior: when
cliand@scope/cliare installed, callingnpx cliwill executecli - Desired behavior: not 100% certain about what is the right thing to do here, but if
@scope/clicomes from my private registry, andclicomes from a deep public dependency, I definitely do not wantclito execute -npxshould probably force me to provide a--packagewhen there's any ambiguity (or usenpx @scope/clionce that is supported) - This is likely a breaking change. Not sure how to go about it at the moment - probably needs discussion.
- Current behavior: when
References
- n/a
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.