Skip to content

Commit 863bb04

Browse files
authored
Update NPM Security Cheat Sheet (#1856)
* Add Trusted publishing section * Typosquatting + slopsquatting * Add allowlist documentation * Update case * Update paragraph to the third-person
1 parent 28eecf1 commit 863bb04

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

cheatsheets/NPM_Security_Cheat_Sheet.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# NPM Security best practices
22

3-
In the following npm cheatsheet, we’re going to focus on [10 npm security best practices](https://snyk.io/blog/ten-npm-security-best-practices) and productivity tips, useful for JavaScript and Node.js developers.
3+
The following cheatsheet covers several npm security best practices and productivity tips, useful for JavaScript and Node.js developers. This list was originally based on the [10 npm security best practices](https://snyk.io/blog/ten-npm-security-best-practices) from the Snyk blog.
44

55
## 1) Avoid publishing secrets to the npm registry
66

@@ -46,6 +46,22 @@ Apply these npm security best practices in order to minimize the malicious modul
4646
- When installing packages make sure to add the `--ignore-scripts` suffix to disable the execution of any scripts by third-party packages.
4747
- Consider adding `ignore-scripts` to your `.npmrc` project file, or to your global npm configuration.
4848

49+
### Using an allowlist for lifecycle scripts
50+
51+
Disabling lifecycle scripts by default by adding `ignore-script` to your `.npmrc` file is the safest option. If you use packages that rely on lifecycle scripts for legitimate reasons, you can use a plugin like [`@lavamoat/allow-scripts`](https://github.com/LavaMoat/LavaMoat/tree/main/packages/allow-scripts) to create an _allowlist_ of packages authorized to run lifecylce scripts.
52+
53+
Here's how the allowlist would look like in the `package.json` file on a project using the popular image processing package [sharp](https://www.npmjs.com/package/sharp):
54+
55+
```json
56+
{
57+
"lavamoat": {
58+
"allowScripts": {
59+
"sharp": true
60+
}
61+
}
62+
}
63+
```
64+
4965
## 4) Assess npm project health
5066

5167
### npm outdated command
@@ -70,7 +86,7 @@ Call the doctor! The npm CLI incorporates a health assessment tool to diagnose y
7086

7187
The npm ecosystem is the single largest repository of application libraries amongst all the other language ecosystems. The registry and the libraries in it are at the core for JavaScript developers as they are able to leverage work that others have already built and incorporate it into their codebase. With that said, the increasing adoption of open source libraries in applications brings with it an increased risk of introducing security vulnerabilities.
7288

73-
Many popular npm packages have been found to be vulnerable and may carry a significant risk without proper security auditing of your project’s dependencies. Some examples are npm [request](https://snyk.io/vuln/npm:request:20160119), [superagent](https://snyk.io/vuln/search?q=superagent&type=npm), [mongoose](https://snyk.io/vuln/search?q=mongoose&type=npm), and even security-related packages like [jsonwebtoken](https://snyk.io/vuln/npm:jsonwebtoken:20150331), and [validator](https://snyk.io/vuln/search?q=validator&type=npm).
89+
Many popular npm packages have been found to be vulnerable and may carry a significant risk without proper security auditing of your project’s dependencies. Some examples are npm [request](https://snyk.io/vuln/npm:request:20160119), [superagent](https://snyk.io/vuln/search?q=superagent&type=npm), [mongoose](https://snyk.io/vuln/search?q=mongoose&type=npm), and even security-related packages like [jsonwebtoken](https://snyk.io/vuln/npm:jsonwebtoken:20150331), and [validator](https://snyk.io/vuln/search?q=validator&type=npm).
7490

7591
Security doesn’t end by just scanning for security vulnerabilities when installing a package but should also be streamlined with developer workflows to be effectively adopted throughout the entire lifecycle of software development, and monitored continuously when code is deployed:
7692

@@ -177,7 +193,7 @@ Follow the command-line instructions to enable 2FA, and to save emergency authen
177193

178194
Every time you log in with the npm CLI, a token is generated for your user and authenticates you to the npm registry. Tokens make it easy to perform npm registry-related actions during CI and automated procedures, such as accessing private modules on the registry or publishing new versions from a build step.
179195

180-
Tokens can be managed through the npm registry website, as well as using the npm command-line client. An example of using the CLI to create a read-only token that is restricted to a specific IPv4 address range is as follows:
196+
Tokens can be managed through the npm registry website, as well as using the npm command-line client. An example of using the CLI to create a read-only token that is restricted to a specific IPv4 address range is as follows:
181197

182198
```sh
183199
npm token create --read-only --cidr=192.0.2.0/24
@@ -187,25 +203,47 @@ To verify which tokens are created for your user or to revoke tokens in cases of
187203

188204
Ensure you are following this npm security best practice by protecting and minimizing the exposure of your npm tokens.
189205

190-
## 10) Understand module naming conventions and typosquatting attacks
206+
## 10) Understanding typosquatting and slopsquatting attacks
207+
208+
### Typosquatting attacks
209+
210+
Typosquatting is an attack that relies on mistakes made by users, such as typos. With typosquatting, bad actors publish malicious modules to the npm registry with names that look much like existing popular modules. These malicious packages exploit common typing errors or visual similarities to trick developers into installing them instead of the legitimate packages they intended to use.
211+
212+
The Snyk security team has tracked tens of malicious packages in the npm ecosystem that used typosquatting to trick users into installing them; similar attacks have been observed on the PyPi Python registry as well. Some of the most notable incidents include [cross-env](https://snyk.io/vuln/npm:crossenv:20170802), [event-stream](https://snyk.io/vuln/SNYK-JS-EVENTSTREAM-72638), and [eslint-scope](https://snyk.io/vuln/npm:eslint-scope:20180712).
213+
214+
One of the main targets for typosquatting attacks are user credentials, since any package has access to environment variables via the global variable `process.env`. Other examples include the event-stream case, where attackers targeted developers in the hopes of [injecting malicious code](https://snyk.io/blog/a-post-mortem-of-the-malicious-event-stream-backdoor) into an application's source code.
215+
216+
### Slopsquatting attacks
217+
218+
Slopsquatting is a newer attack vector that exploits AI-powered coding assistants and Large Language Models (LLMs). When developers use AI tools to generate code or package recommendations, these models may occasionally "hallucinate" package names that don't actually exist. Attackers monitor these hallucinations and create malicious packages with those exact names, knowing that developers may blindly trust and install packages suggested by their AI assistants.
219+
220+
To protect against slopsquatting:
221+
222+
- Always verify that packages suggested by AI tools actually exist and are legitimate
223+
- Check the package's repository, download statistics, and maintainer information
224+
- Cross-reference AI suggestions with official documentation
225+
- Be skeptical of packages with very low download counts or recent creation dates
226+
- Review the package source code before installing, especially for AI-suggested packages
227+
228+
## 11) Use trusted publishers for secure package publishing
229+
230+
Traditional npm publishing relies on long-lived tokens that can be compromised or accidentally exposed. Trusted publishing with OpenID Connect (OIDC) provides a more secure alternative by using short-lived, workflow-specific credentials that are automatically generated during CI/CD processes. Trusted publishing currently supports GitHub Actions and GitLab CI/CD Pipelines.
231+
232+
### How trusted publishing works
233+
234+
Trusted publishing creates a trust relationship between npm and your CI/CD provider using OIDC. When you configure a trusted publisher for your package, npm will accept publishes from the specific workflow you've authorized, in addition to traditional authentication methods like npm tokens and manual publishes. The npm CLI automatically detects OIDC environments and uses them for authentication before falling back to traditional tokens.
191235

192-
Naming a module is the first thing you might do when creating a package, but before defining a final name, npm defines some rules that a package name must follow:
236+
This approach eliminates the security risks associated with long-lived write tokens, which can be compromised, accidentally exposed in logs, or require manual rotation. Instead, each publish uses short-lived, cryptographically-signed tokens that are specific to your workflow and cannot be extracted or reused.
193237

194-
- It is limited to 214 characters
195-
- No uppercase letters in the name
196-
- No trailing spaces
197-
- Some special characters are not allowed: “~\’!()*
198-
- Can’t start with . or _
199-
- Can’t use node_modules or favicon.ico in the name
200-
- Even if you follow these rules, be aware that npm uses a spam detection mechanism when publishing new packages, based on score and whether a package name violates the terms of the service. If conditions are violated, the registry might deny the request.
238+
### Automatic provenance generation
201239

202-
Typosquatting is an attack that relies on mistakes made by users, such as typos. With typosquatting, bad actors could publish malicious modules to the npm registry with names that look much like existing popular modules.
240+
When publishing via trusted publishing, npm automatically generates provenance attestations that provide cryptographic proof of package authenticity. This helps users verify that packages come from legitimate sources and haven't been tampered with.
203241

204-
We have been tracking tens of malicious packages in the npm ecosystem; they have been seen on the PyPi Python registry as well. Perhaps some of the most popular incidents have been for [cross-env](https://snyk.io/vuln/npm:crossenv:20170802), [event-stream](https://snyk.io/vuln/SNYK-JS-EVENTSTREAM-72638), and [eslint-scope](https://snyk.io/vuln/npm:eslint-scope:20180712).
242+
For more information, see the [npm trusted publishing documentation](https://docs.npmjs.com/trusted-publishers).
205243

206-
One of the main targets for typosquatting attacks are the user credentials, since any package has access to environment variables via the global variable process.env. Other examples we’ve seen in the past include the case with event-stream, where the attack targeted developers in the hopes of [injecting malicious code](https://snyk.io/blog/a-post-mortem-of-the-malicious-event-stream-backdoor) into an application’s source code.
244+
## Final Recommendations
207245

208-
Closing our list of ten npm security best practices are the following tips to reduce the risk of such attacks:
246+
Closing our list of npm security best practices are the following tips to reduce the risk of such attacks:
209247

210248
- Be extra-careful when copy-pasting package installation instructions into the terminal. Make sure to verify in the source code repository as well as on the npm registry that this is indeed the package you are intending to install. You might verify the metadata of the package with `npm info` to fetch more information about contributors and latest versions.
211249
- Default to having an npm logged-out user in your daily work routines so your credentials won’t be the weak spot that would lead to easily compromising your account.

0 commit comments

Comments
 (0)