Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Can you tell me if express-mongo-sanitize is working?

I'm trying to set up some security middleware for my humble little MERN web app, and I'm currently using helmet and express-mongo-sanitize, specifically for protection against NoSQL injection attacks.

I've set it up, however, as below in my server.js file:

const express = require('express')
const helmet = require('helmet')
const mongoSanitize = require('express-mongo-sanitize')

...

app.use(mongoSanitize())
app.use(helmet())

// Routes below

...

I've tried to test it by making a mock sign up like:

username: {"$gt": ""} password: 'TestPassword'

so that req.body would be:

{

username: '{"$gt": ""}',

password: 'TestPassword'

}

but express-mongo-sanitize doesn't seem to be catching it and it goes through to my database. Am I misunderstanding something? The value of the username key is a string, so maybe it's already OK? Please forgive my ignorance, I'm learning.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What express-mongo-sanitize does is sanitize keys that start with a dollar sign.

username: '{"$gt": ""}' --> this isn't a key starting with a dollar sign. Rather, the value of username is just a string.

Try sending it this object instead:

{

"username": { "$gt": "" }

}
about 4 years ago · Juan Pablo Isaza Report

0

From what I understood, from debugging and going through the code, the keys it sanitizes is any potential key in a key=value pair of query & post params that have the $ or a dot. It also tries to sanitize any keys in the body and header of the request.

For example, even the json provided by the previous user above won't do it.

but https://your-domain/?$user=json would be sanitized to user=json.

It doesn't remove $ from the value of the params as you and also I was expecting. I also opened up a question on the github to the creator and will see what he says. I would think the security risk is for both the key and value. This doesn't do any good if you're not saving the key to mongodb but saving the value instead.

For reference it checks the following HTTP sections to remove any potential harmful $ or .

['body', 'params', 'headers', 'query'].forEach(function (key) ...

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!