Skip to content

Commit 61c495e

Browse files
author
Victor Xu
committed
feat!: support Express 5
BREAKING CHANGE: Must use expressApp.set('query parser', 'extended') to parse and sanitize nested query params
1 parent 5a94a5f commit 61c495e

File tree

6 files changed

+1624
-3618
lines changed

6 files changed

+1624
-3618
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Created by https://www.gitignore.io/api/node
22

3+
### Dev ###
4+
app.js
5+
36
### Node ###
47
# Logs
58
logs

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Express Mongo Sanitize
22

3-
Express 4.x middleware which sanitizes user-supplied data to prevent MongoDB Operator Injection.
3+
Express 5.x middleware which sanitizes user-supplied data to prevent MongoDB Operator Injection.
4+
5+
For Express 4.x please use v2.2 of this package.
46

57
[![Build Status](https://github.com/fiznool/express-mongo-sanitize/workflows/Node.js%20CI/badge.svg)](https://github.com/fiznool/express-mongo-sanitize/actions/workflows/nodejs.yml)
68
[![npm version](https://img.shields.io/npm/v/express-mongo-sanitize)](https://www.npmjs.com/package/express-mongo-sanitize)
@@ -42,7 +44,7 @@ const bodyParser = require('body-parser');
4244
const mongoSanitize = require('express-mongo-sanitize');
4345

4446
const app = express();
45-
47+
app.set('query parser', 'extended');
4648
app.use(bodyParser.urlencoded({ extended: true }));
4749
app.use(bodyParser.json());
4850

@@ -110,6 +112,24 @@ app.use(
110112
);
111113
```
112114

115+
### Sanitizing Nested Objects
116+
117+
To sanitize nested objects in query strings, such as `/query?username[$gt]=foo&username[dotted.data]=some_data`, ensure that Express' `query parser` option is set to `extended`. This helps protect against nested query injection attacks through query parameters.
118+
119+
If `replaceWith` is not set, the sanitized query parameter will appear as:
120+
121+
```json
122+
{ "username": {} }
123+
```
124+
125+
However, if using Express v5's default `simple` query parser, the query parameter will remain as:
126+
127+
```json
128+
{ "username[$gt]": "foo" }
129+
```
130+
131+
For sanitizing nested objects in the request body, configure `bodyParser.urlencoded({ extended: true })`.
132+
113133
### Node Modules API
114134

115135
You can also bypass the middleware and use the module directly:
@@ -148,6 +168,10 @@ const hasProhibited = mongoSanitize.has(payload);
148168
const hasProhibited = mongoSanitize.has(payload, true);
149169
```
150170

171+
### `req.query` Being Readonly in Express v5
172+
173+
`req.query` is designed to be read only in Express v5; however, this middleware modifies `req.query`, which might be unexpected for some users.
174+
151175
## Contributing
152176

153177
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.

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function sanitize(target, options = {}) {
107107
function middleware(options = {}) {
108108
const hasOnSanitize = typeof options.onSanitize === 'function';
109109
return function (req, res, next) {
110-
['body', 'params', 'headers', 'query'].forEach(function (key) {
110+
['body', 'params', 'headers'].forEach(function (key) {
111111
if (req[key]) {
112112
const { target, isSanitized } = _sanitize(req[key], options);
113113
req[key] = target;
@@ -119,6 +119,24 @@ function middleware(options = {}) {
119119
}
120120
}
121121
});
122+
123+
if (req.query) {
124+
const { target, isSanitized } = _sanitize(req.query, options);
125+
if (isSanitized) {
126+
Object.defineProperty(req, 'query', {
127+
value: target,
128+
writable: false,
129+
configurable: true,
130+
enumerable: true,
131+
});
132+
if (hasOnSanitize) {
133+
options.onSanitize({
134+
req,
135+
key: 'query',
136+
});
137+
}
138+
}
139+
}
122140
next();
123141
};
124142
}

0 commit comments

Comments
 (0)