Skip to content

Commit c59c279

Browse files
committed
v2.2.0
1 parent 597b882 commit c59c279

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [2.2.0] - 2022-01-14
7+
8+
### Added
9+
10+
- New `config` option:
11+
- `allowDots` boolean: if set, allows dots in the user-supplied data #41
12+
13+
### Fixed
14+
15+
- Prevent null pointer exception when using `dryRun` option #88
16+
617
## [2.1.0] - 2021-05-11
718

819
### Added
@@ -81,6 +92,7 @@ Note that if you weren't previously expecting headers to be sanitized, this is c
8192

8293
Initial Release.
8394

95+
[2.2.0]: https://github.com/fiznool/express-mongo-sanitize/compare/v2.1.0...v2.2.0
8496
[2.1.0]: https://github.com/fiznool/express-mongo-sanitize/compare/v2.0.2...v2.1.0
8597
[2.0.2]: https://github.com/fiznool/express-mongo-sanitize/compare/v2.0.1...v2.0.2
8698
[2.0.1]: https://github.com/fiznool/express-mongo-sanitize/compare/v2.0.0...v2.0.1

README.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ Express 4.x middleware which sanitizes user-supplied data to prevent MongoDB Ope
77
[![npm downloads per week](https://img.shields.io/npm/dw/express-mongo-sanitize?color=blue)](https://img.shields.io/npm/dw/express-mongo-sanitize?color=blue)
88
[![Dependency Status](https://img.shields.io/librariesio/release/npm/express-mongo-sanitize)](https://img.shields.io/librariesio/release/npm/express-mongo-sanitize)
99

10+
## What is this module for?
11+
12+
This module searches for any keys in objects that begin with a `$` sign or contain a `.`, from `req.body`, `req.query` or `req.params`. It can then either:
13+
14+
- completely remove these keys and associated data from the object, or
15+
- replace the prohibited characters with another allowed character.
16+
17+
The behaviour is governed by the passed option, `replaceWith`. Set this option to have the sanitizer replace the prohibited characters with the character passed in.
18+
19+
The config option `allowDots` can be used to allow dots in the user-supplied data. In this case, only instances of `$` will be sanitized.
20+
21+
See the spec file for more examples.
22+
23+
## Why is it needed?
24+
25+
Object keys starting with a `$` or containing a `.` are _reserved_ for use by MongoDB as operators. Without this sanitization, malicious users could send an object containing a `$` operator, or including a `.`, which could change the context of a database operation. Most notorious is the `$where` operator, which can execute arbitrary JavaScript on the database.
26+
27+
The best way to prevent this is to sanitize the received data, and remove any offending keys, or replace the characters with a 'safe' one.
28+
1029
## Installation
1130

1231
```bash
@@ -27,18 +46,25 @@ const app = express();
2746
app.use(bodyParser.urlencoded({ extended: true }));
2847
app.use(bodyParser.json());
2948

30-
// To remove data, use:
49+
// By default, $ and . characters are removed completely from user-supplied input in the following places:
50+
// - req.body
51+
// - req.params
52+
// - req.headers
53+
// - req.query
54+
55+
// To remove data using these defaults:
3156
app.use(mongoSanitize());
3257

33-
// Or, to replace prohibited characters with _, use:
58+
// Or, to replace these prohibited characters with _, use:
3459
app.use(
3560
mongoSanitize({
3661
replaceWith: '_',
3762
}),
3863
);
3964

4065
// Or, to sanitize data that only contains $, without .(dot)
41-
// Can be useful for letting data pass that is meant for querying nested documents. NOTE: This may cause some problems on older versions of MongoDb
66+
// Can be useful for letting data pass that is meant for querying nested documents.
67+
// NOTE: This may cause some problems on older versions of MongoDb
4268
// READ MORE: https://github.com/fiznool/express-mongo-sanitize/issues/36
4369
app.use(
4470
mongoSanitize({
@@ -101,7 +127,9 @@ mongoSanitize.sanitize(payload, {
101127
replaceWith: '_'
102128
});
103129

104-
// Exclude sanitization of . (dot), only sanitize data that contains $. This may cause some problems on older versions of mongo db
130+
// Exclude sanitization of . (dot), only sanitize data that contains $.
131+
// NOTE: This may cause some problems on older versions of MongoDb
132+
// READ MORE: https://github.com/fiznool/express-mongo-sanitize/issues/36
105133
mongoSanitize.sanitize(payload, {
106134
allowDots: true
107135
});
@@ -115,27 +143,11 @@ mongoSanitize.sanitize(payload, {
115143
// Check if the payload has keys with prohibited characters
116144
const hasProhibited = mongoSanitize.has(payload);
117145

118-
// Check if the payload has keys with prohibited characters (`.` is excluded). So if the payload only has `.` it will return false (since it doesn't see the data with `.` as a malicious data)
146+
// Check if the payload has keys with prohibited characters (`.` is excluded).
147+
// If the payload only has `.` it will return false (since it doesn't see the data with `.` as malicious)
119148
const hasProhibited = mongoSanitize.has(payload, true);
120149
```
121150

122-
## What?
123-
124-
This module searches for any keys in objects that begin with a `$` sign or contain a `.`, from `req.body`, `req.query` or `req.params`. It can then either:
125-
126-
- completely remove these keys and associated data from the object, or
127-
- replace the prohibited characters with another allowed character.
128-
129-
The behaviour is governed by the passed option, `replaceWith`. Set this option to have the sanitizer replace the prohibited characters with the character passed in.
130-
131-
See the spec file for more examples.
132-
133-
## Why?
134-
135-
Object keys starting with a `$` or containing a `.` are _reserved_ for use by MongoDB as operators. Without this sanitization, malicious users could send an object containing a `$` operator, or including a `.`, which could change the context of a database operation. Most notorious is the `$where` operator, which can execute arbitrary JavaScript on the database.
136-
137-
The best way to prevent this is to sanitize the received data, and remove any offending keys, or replace the characters with a 'safe' one.
138-
139151
## Contributing
140152

141153
PRs are welcome! Please add test coverage for any new features or bugfixes, and make sure to run `npm run prettier` before submitting a PR to ensure code consistency.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-mongo-sanitize",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Sanitize your express payload to prevent MongoDB operator injection.",
55
"main": "index.js",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)