Skip to content

Commit 32c997b

Browse files
committed
Adding options.allowDots (#41)
Squashed commit of the following: commit f85e51644ed68a74dd5fbe7a79c17e62e02aed01 Author: Tom Spencer <[email protected]> Date: Fri Jan 14 10:47:41 2022 +0000 Removed unnecessary file commit 059be6ba7fb8a4d6e80147a94b03a064f1a43fcc Merge: 565c1ea 55a16c7 Author: Tom Spencer <[email protected]> Date: Fri Jan 14 10:46:46 2022 +0000 Merge branch 'master' of github.com:Blagoj5/express-mongo-sanitize into Blagoj5-master commit 55a16c7 Merge: 16534f2 9cc5240 Author: Blagoj <[email protected]> Date: Wed May 12 18:22:57 2021 +0200 Merge github.com:fiznool/express-mongo-sanitize commit 16534f2 Author: Blagoj <[email protected]> Date: Wed May 12 17:07:35 2021 +0200 Clean code and fix tests commit 565c1ea Author: Tom Spencer <[email protected]> Date: Tue May 11 16:47:57 2021 +0100 Bump package version to 2.1.0 commit 05e39bb Author: Blagoj <[email protected]> Date: Sat Jan 23 16:40:25 2021 +0100 feat: Adding new options (options.allowDots) Adding new option/feature, options.allowDots that is used for skipping the sanitization of data that has .(dot). This can be useful for nested document quering for mongoDb: https://docs.mongodb.com/manual/tutorial/query-embedded-documents/ Creating new tests that include the new option Updating the documentation (README.md) file for the new option Adressing issue: #36 commit 287075b Author: Blagoj <[email protected]> Date: Sat Jan 23 16:38:15 2021 +0100 feat: Adding new options (options.allowDots) Adding new option/feature, options.allowDots that is used for skipping the sanitization of data that has .(dot). This can be useful for nested document quering for mongoDb: https://docs.mongodb.com/manual/tutorial/query-embedded-documents/ Creating new tests that include the new option Updating the documentation (README.md) file for the new option Adressing issue: #36 commit aec9249 Author: Blagoj <[email protected]> Date: Sat Jan 23 16:17:53 2021 +0100 feat: Adding new options (options.allowDots) Adding new option/feature, options.allowDots that is used for skipping the sanitization of data that has .(dot). This can be useful for nested document quering for mongoDb: https://docs.mongodb.com/manual/tutorial/query-embedded-documents/ Creating new tests that include the new option Updating the documentation (README.md) file for the new option Adressing issue: #36
1 parent 81c2eb5 commit 32c997b

File tree

4 files changed

+858
-7
lines changed

4 files changed

+858
-7
lines changed

Diff for: README.md

+31
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ app.use(
3737
replaceWith: '_',
3838
}),
3939
);
40+
41+
// Or, to sanitize data that only contains $, without .(dot)
42+
// 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
43+
// READ MORE: https://github.com/fiznool/express-mongo-sanitize/issues/36
44+
app.use(
45+
mongoSanitize({
46+
allowDots: true,
47+
}),
48+
);
49+
50+
// Both allowDots and replaceWith
51+
app.use(
52+
mongoSanitize({
53+
allowDots: true,
54+
replaceWith: '_',
55+
}),
56+
);
4057
```
4158

4259
### `onSanitize`
@@ -85,8 +102,22 @@ mongoSanitize.sanitize(payload, {
85102
replaceWith: '_'
86103
});
87104

105+
// Exclude sanitization of . (dot), only sanitize data that contains $. This may cause some problems on older versions of mongo db
106+
mongoSanitize.sanitize(payload, {
107+
allowDots: true
108+
});
109+
110+
// Both allowDots and replaceWith
111+
mongoSanitize.sanitize(payload, {
112+
allowDots: true,
113+
replaceWith: '_'
114+
});
115+
88116
// Check if the payload has keys with prohibited characters
89117
const hasProhibited = mongoSanitize.has(payload);
118+
119+
// 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)
120+
const hasProhibited = mongoSanitize.has(payload, true);
90121
```
91122

92123
## What?

Diff for: index.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare namespace ExpressMongoSanitize {
55
replaceWith?: string;
66
onSanitize?: (params: { key: string; req: Request }) => void;
77
dryRun?: boolean;
8+
allowDots?: boolean;
89
}
910
}
1011

@@ -27,7 +28,10 @@ type Middleware = {
2728
* Check if the payload has keys with prohibited characters‘
2829
* @param target
2930
*/
30-
has(target: Record<string, unknown> | unknown[]): boolean;
31+
has(
32+
target: Record<string, unknown> | unknown[],
33+
allowDots?: boolean,
34+
): boolean;
3135
};
3236

3337
declare const ExpressMongoSanitize: Middleware & {

Diff for: index.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use strict';
22

33
const TEST_REGEX = /^\$|\./;
4+
const TEST_REGEX_WITHOUT_DOT = /^\$/;
45
const REPLACE_REGEX = /^\$|\./g;
56

67
function isPlainObject(obj) {
78
return typeof obj === 'object' && obj !== null;
89
}
910

11+
function getTestRegex(allowDots) {
12+
return allowDots ? TEST_REGEX_WITHOUT_DOT : TEST_REGEX;
13+
}
14+
1015
function withEach(target, cb) {
1116
(function act(obj) {
1217
if (Array.isArray(obj)) {
@@ -23,10 +28,12 @@ function withEach(target, cb) {
2328
})(target);
2429
}
2530

26-
function has(target) {
31+
function has(target, allowDots) {
32+
const regex = getTestRegex(allowDots);
33+
2734
let hasProhibited = false;
2835
withEach(target, function (obj, val, key) {
29-
if (TEST_REGEX.test(key)) {
36+
if (regex.test(key)) {
3037
hasProhibited = true;
3138
return { shouldRecurse: false };
3239
} else {
@@ -38,17 +45,19 @@ function has(target) {
3845
}
3946

4047
function _sanitize(target, options) {
48+
const regex = getTestRegex(options.allowDots);
49+
4150
let isSanitized = false;
4251
let replaceWith = null;
43-
let dryRun = Boolean(options.dryRun);
44-
if (!TEST_REGEX.test(options.replaceWith)) {
52+
const dryRun = Boolean(options.dryRun);
53+
if (!regex.test(options.replaceWith) && options.replaceWith !== '.') {
4554
replaceWith = options.replaceWith;
4655
}
4756

4857
withEach(target, function (obj, val, key) {
4958
let shouldRecurse = true;
5059

51-
if (TEST_REGEX.test(key)) {
60+
if (regex.test(key)) {
5261
isSanitized = true;
5362
// if dryRun is enabled, do not modify the target
5463
if (dryRun) {

0 commit comments

Comments
 (0)