https://github.com/panga/node-shield
Protects against common Node.js vulnerabilities in MEAN stack (MongoDB, Node.js).
https://github.com/panga/node-shield
express-middleware nodejs nosql-injection owasp security
Last synced: 12 months ago
JSON representation
Protects against common Node.js vulnerabilities in MEAN stack (MongoDB, Node.js).
- Host: GitHub
- URL: https://github.com/panga/node-shield
- Owner: panga
- License: apache-2.0
- Created: 2019-11-12T23:47:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T00:45:27.000Z (over 3 years ago)
- Last Synced: 2025-05-11T22:03:15.282Z (about 1 year ago)
- Topics: express-middleware, nodejs, nosql-injection, owasp, security
- Language: TypeScript
- Homepage:
- Size: 1.13 MB
- Stars: 7
- Watchers: 1
- Forks: 2
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-shield
[](https://badge.fury.io/js/node-shield)
[](https://travis-ci.com/panga/node-shield)
Protects against common Node.js vulnerabilities in MEAN stack (MongoDB, Node.js).
Provides an extremelly fast and low overhead API and Express 4.x middleware.
- Executes in ~200ns (nanoseconds) for a payload with 10 keys and 500 bytes.
- 100% code coverage.
- Zero dependencies.
- Supports Node 6+
## Install
`npm install node-shield`
## Description
This module aims in protecting Node.js applications againt OWASP Injection (A1) attacks.
One of the most common attacks of MEAN stack is the MongoDB NoSQL injection using arbitraty input in request parameters.
A second and more recent attack comes with JavaScript prototype pollution and it was seen in multiple libraries in last years ([Lodash](https://snyk.io/vuln/SNYK-JS-LODASH-450202), [Hapi.js](https://github.com/hapijs/hapi/issues/3916)), but it is also present if you use `Object.assign` API.
**WARNING** This is not a replacement for good coding practices like:
* Use parameterized queries to prevent injection flaws.
* Always validate input parameters types (JSON Schema recommended)
### MongoDB NoSQL protection
Block object keys which start with `$` operator for MongoDB. e.g: `username: { $gt: ''}`.
References:
* https://www.owasp.org/index.php/Testing_for_NoSQL_injection
* https://blog.websecurify.com/2014/08/hacking-nodejs-and-mongodb.html
* https://blog.websecurify.com/2014/08/attacks-nodejs-and-mongodb-part-to.html
### Prototype Pollution protection
Block object keys with names `__proto__` or `constructor` which are also an object.
References:
* https://github.com/HoLyVieR/prototype-pollution-nsec18/blob/master/paper/JavaScript_prototype_pollution_attack_in_NodeJS.pdf
## API usage
### Callback style
```javascript
const { shield } = require('node-shield');
shield.evaluate({ user: { $gt: '' } }, { mongo: true, proto: true },
(err) => {
if (err) {
throw err;
}
});
```
### Promise style
```javascript
const { shield } = require('node-shield');
shield.evaluateAsync({ user: { $gt: '' } }, { mongo: true, proto: true })
.catch((err) => {
throw err;
});
```
## Express 4.x middleware usage
By default, both `mongo` and `proto` protections are evaluated and the error handler return a `403` error.
You can do anything you would normally do in a express middleware.
Example, but not limited to:
- Log the injection attempt and continue to process the request
- Log the injection attempt and response with an error
```javascript
const express = require('express');
const { expressShield } = require('node-shield');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(expressShield({
errorHandler: (shieldError, req, res, next) => {
console.error(shieldError);
res.sendStatus(400);
},
}));
app.listen(3000);
```
## License
[Apache2.0](https://www.apache.org/licenses/LICENSE-2.0)
## Author
Leonardo Zanivan
[www.panga.dev](https://panga.dev)