mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-29 16:46:01 +08:00
Merge pull request #370 from i0natan/npmignore
6.25 avoid publishing secrets to npm
This commit is contained in:
17
README.md
17
README.md
@ -9,7 +9,7 @@
|
||||
<br/>
|
||||
|
||||
<div align="center">
|
||||
<img src="https://img.shields.io/badge/⚙%20Item%20count%20-%2081%20Best%20Practices-blue.svg" alt="81 items"> <img src="https://img.shields.io/badge/%F0%9F%93%85%20Last%20update%20-%20Mar%2010%202019-green.svg" alt="Last update: March 10, 2019"> <img src="https://img.shields.io/badge/%E2%9C%94%20Updated%20For%20Version%20-%20Node%2010.15.3%20LTS-brightgreen.svg" alt="Updated for Node 10.15.3 LTS">
|
||||
<img src="https://img.shields.io/badge/⚙%20Item%20count%20-%2082%20Best%20Practices-blue.svg" alt="82 items"> <img src="https://img.shields.io/badge/%F0%9F%93%85%20Last%20update%20-%20Mar%2010%202019-green.svg" alt="Last update: March 10, 2019"> <img src="https://img.shields.io/badge/%E2%9C%94%20Updated%20For%20Version%20-%20Node%2010.15.3%20LTS-brightgreen.svg" alt="Updated for Node 10.15.3 LTS">
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
@ -26,6 +26,8 @@ Read in a different language: [**CN**](/README.chines
|
||||
|
||||
# Latest Best Practices and News
|
||||
|
||||
- **New best practice:** 6.25: [Avoid publishing secrets to the npm registry](/sections/security/avoid_publishing_secrets.md)
|
||||
|
||||
- **New translation:**  [Brazilian Portuguese](/README.brazilian-portuguese.md) available now, courtesy of [Marcelo Melo](https://github.com/marcelosdm)! ❤️
|
||||
|
||||
- **New best practice:** 4.2: Include 3 parts in each test name - [_From the section "Testing and overall quality"_](https://github.com/i0natan/nodebestpractices#4-testing-and-overall-quality-practices)
|
||||
@ -689,7 +691,7 @@ All statements above will return false if used with `===`
|
||||
# `6. Security Best Practices`
|
||||
|
||||
<div align="center">
|
||||
<img src="https://img.shields.io/badge/OWASP%20Threats-Top%2010-green.svg" alt="53 items"/>
|
||||
<img src="https://img.shields.io/badge/OWASP%20Threats-Top%2010-green.svg" alt="54 items"/>
|
||||
</div>
|
||||
|
||||
## ![✔] 6.1. Embrace linter security rules
|
||||
@ -973,6 +975,17 @@ All statements above will return false if used with `===`
|
||||
|
||||
🔗 [**Read More: Prevent unsafe redirects**](/sections/security/saferedirects.md)
|
||||
|
||||
<br/><br/>
|
||||
|
||||
## ![✔] 6.25. Avoid publishing secrets to the npm registry
|
||||
|
||||
<a href="https://www.owasp.org/index.php/Top_10-2017_A6-Security_Misconfiguration" target="_blank"><img src="https://img.shields.io/badge/%E2%9C%94%20OWASP%20Threats%20-%20A6:Security%20Misconfiguration%20-green.svg" alt=""/></a>
|
||||
|
||||
**TL;DR:** Precautions should be taken to avoid the risk of accidentally publishing secrets to public npm registries. An `.npmignore` file can be used to blacklist specific files or folders, or the `files` array in `package.json` can act as a whitelist.
|
||||
|
||||
**Otherwise:** Your project's API keys, passwords or other secrets are open to be abused by anyone who comes across them, which may result in financial loss, impersonation, and other risks.
|
||||
|
||||
🔗 [**Read More: Avoid publishing secrets**](/sections/security/avoid_publishing_secrets.md)
|
||||
<br/><br/><br/>
|
||||
|
||||
<p align="right"><a href="#table-of-contents">⬆ Return to top</a></p>
|
||||
|
||||
44
sections/security/avoid_publishing_secrets.md
Normal file
44
sections/security/avoid_publishing_secrets.md
Normal file
@ -0,0 +1,44 @@
|
||||
# Avoid publishing secrets to the npm registry
|
||||
|
||||
### One Paragraph Explainer
|
||||
Precautions should be taken to avoid the risk of accidentally publishing secrets to public npm registries. An `.npmignore` file can be used to blacklist specific files or folders, or the `files` array in `package.json` can act as a whitelist.
|
||||
|
||||
To gain a view of what npm publish will really publish to the registry, the `--dry-run` flag can be added the npm publish command to provide a verbose view of the tarbell package created.
|
||||
|
||||
It is important to note that if a project is utilising both `.npmignore` and `.gitignore` files, everything which isn't in `.npmignore` is published to the registry(i.e. the `.npmignore` file overrides the `.gitignore`). This condition is a common source of confusion and is a problem that can lead to leaking secrets. Developers may end up updating the `.gitignore` file, but forget to update `.npmignore` as well, which can lead to a potentially sensitive file not being pushed to source control, but still being included in the npm package.
|
||||
|
||||
### Code example
|
||||
Example .npmignore file
|
||||
```
|
||||
#tests
|
||||
test
|
||||
coverage
|
||||
|
||||
#build tools
|
||||
.travis.yml
|
||||
.jenkins.yml
|
||||
|
||||
#environment
|
||||
.env
|
||||
.config
|
||||
|
||||
```
|
||||
|
||||
Example use of files array in package.json
|
||||
|
||||
```
|
||||
{
|
||||
"files" : [
|
||||
"dist/moment.js",
|
||||
"dist/moment.min.js"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### What other bloggers say
|
||||
|
||||
From the blog by [Liran Tal & Juan Picado at Snyk](https://snyk.io/blog/ten-npm-security-best-practices/):
|
||||
> ... Another good practice to adopt is making use of the files property in package.json, which works as a whitelist and specifies the array of files to be included in the package that is to be created and installed (while the ignore file functions as a blacklist). The files property and an ignore file can both be used together to determine which files should explicitly be included, as well as excluded, from the package. When using both, the former the files property in package.json takes precedence over the ignore file.
|
||||
|
||||
From the [npm blog](https://blog.npmjs.org/post/165769683050/publishing-what-you-mean-to-publish)
|
||||
> ... When you run npm publish, npm bundles up all the files in the current directory. It makes a few decisions for you about what to include and what to ignore. To make these decisions, it uses the contents of several files in your project directory. These files include .gitignore, .npmignore, and the files array in the package.json. It also always includes certain files and ignores others.
|
||||
Reference in New Issue
Block a user