Skip to content

feat!: support Express 5 #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Created by https://www.gitignore.io/api/node

### Dev ###
app.js

### Node ###
# Logs
logs
Expand Down
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Express Mongo Sanitize

Express 4.x middleware which sanitizes user-supplied data to prevent MongoDB Operator Injection.
Express 5.x middleware which sanitizes user-supplied data to prevent MongoDB Operator Injection.

For Express 4.x please use v2.2 of this package.

[![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)
[![npm version](https://img.shields.io/npm/v/express-mongo-sanitize)](https://www.npmjs.com/package/express-mongo-sanitize)
Expand Down Expand Up @@ -42,7 +44,7 @@ const bodyParser = require('body-parser');
const mongoSanitize = require('express-mongo-sanitize');

const app = express();

app.set('query parser', 'extended');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

Expand Down Expand Up @@ -110,6 +112,24 @@ app.use(
);
```

### Sanitizing Nested Objects

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.

If `replaceWith` is not set, the sanitized query parameter will appear as:

```json
{ "username": {} }
```

However, if using Express v5's default `simple` query parser, the query parameter will remain as:

```json
{ "username[$gt]": "foo" }
```

For sanitizing nested objects in the request body, configure `bodyParser.urlencoded({ extended: true })`.

### Node Modules API

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

### `req.query` Being Readonly in Express v5

`req.query` is designed to be read only in Express v5; however, this middleware modifies `req.query`, which might be unexpected for some users.

## Contributing

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.
Expand Down
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function sanitize(target, options = {}) {
function middleware(options = {}) {
const hasOnSanitize = typeof options.onSanitize === 'function';
return function (req, res, next) {
['body', 'params', 'headers', 'query'].forEach(function (key) {
['body', 'params', 'headers'].forEach(function (key) {
if (req[key]) {
const { target, isSanitized } = _sanitize(req[key], options);
req[key] = target;
Expand All @@ -119,6 +119,24 @@ function middleware(options = {}) {
}
}
});

if (req.query) {
const { target, isSanitized } = _sanitize(req.query, options);
if (isSanitized) {
Object.defineProperty(req, 'query', {
value: target,
writable: false,
configurable: true,
enumerable: true,
});
if (hasOnSanitize) {
options.onSanitize({
req,
key: 'query',
});
}
}
}
next();
};
}
Expand Down
Loading