You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+34-22
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,25 @@ Express 4.x middleware which sanitizes user-supplied data to prevent MongoDB Ope
7
7
[](https://img.shields.io/npm/dw/express-mongo-sanitize?color=blue)
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.
// Check if the payload has keys with prohibited characters
116
144
consthasProhibited=mongoSanitize.has(payload);
117
145
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)
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
-
139
151
## Contributing
140
152
141
153
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.
0 commit comments