Skip to content

Commit

Permalink
escape express5 in testing express-mongo-sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasShabi committed Nov 25, 2024
1 parent 0959528 commit 0e039c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { prepareTestServerForIastInExpress } = require('../utils')
const agent = require('../../../plugins/agent')

describe('nosql injection detection in mongodb - whole feature', () => {
withVersions('express', 'express', '>4.18.0', expressVersion => {
withVersions('express', 'express', '>4.18.0 <5.0.0', expressVersion => {
withVersions('mongodb', 'mongodb', mongodbVersion => {
const mongodb = require(`../../../../../../versions/mongodb@${mongodbVersion}`)

Expand Down Expand Up @@ -155,27 +155,30 @@ describe('nosql injection detection in mongodb - whole feature', () => {
redactionEnabled: false
})

withVersions('express-mongo-sanitize', 'express-mongo-sanitize', expressMongoSanitizeVersion => {
prepareTestServerForIastInExpress('Test with sanitization middleware', expressVersion, (expressApp) => {
const mongoSanitize =
require(`../../../../../../versions/express-mongo-sanitize@${expressMongoSanitizeVersion}`).get()
expressApp.use(mongoSanitize())
}, (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => {
testThatRequestHasNoVulnerability({
fn: async (req, res) => {
await collection.find({
key: req.query.key
})

res.end()
},
vulnerability: 'NOSQL_MONGODB_INJECTION',
makeRequest: (done, config) => {
axios.get(`http://localhost:${config.port}/?key=value`).catch(done)
}
// https://github.com/fiznool/express-mongo-sanitize/issues/200
if (semver.intersects(expressVersion, '<5.0.0')) {
withVersions('express-mongo-sanitize', 'express-mongo-sanitize', expressMongoSanitizeVersion => {
prepareTestServerForIastInExpress('Test with sanitization middleware', expressVersion, (expressApp) => {
const mongoSanitize =
require(`../../../../../../versions/express-mongo-sanitize@${expressMongoSanitizeVersion}`).get()
expressApp.use(mongoSanitize())
}, (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => {
testThatRequestHasNoVulnerability({
fn: async (req, res) => {
await collection.find({
key: req.query.key
})

res.end()
},
vulnerability: 'NOSQL_MONGODB_INJECTION',
makeRequest: (done, config) => {
axios.get(`http://localhost:${config.port}/?key=value`).catch(done)
}
})
})
})
})
}
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -313,31 +313,34 @@ describe('nosql injection detection with mquery', () => {
}, 'NOSQL_MONGODB_INJECTION')
})

withVersions('express-mongo-sanitize', 'express-mongo-sanitize', expressMongoSanitizeVersion => {
prepareTestServerForIastInExpress('Test with sanitization middleware', expressVersion, (expressApp) => {
const mongoSanitize =
// https://github.com/fiznool/express-mongo-sanitize/issues/200
if (semver.intersects(expressVersion, '<5.0.0')) {
withVersions('express-mongo-sanitize', 'express-mongo-sanitize', expressMongoSanitizeVersion => {
prepareTestServerForIastInExpress('Test with sanitization middleware', expressVersion, (expressApp) => {
const mongoSanitize =
require(`../../../../../../versions/express-mongo-sanitize@${expressMongoSanitizeVersion}`).get()
expressApp.use(mongoSanitize())
}, (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => {
testThatRequestHasNoVulnerability({
fn: async (req, res) => {
const filter = {
name: req.query.key
}
try {
await require(tmpFilePath).vulnerableFindOne(collection, filter)
} catch (e) {
// do nothing
expressApp.use(mongoSanitize())
}, (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => {
testThatRequestHasNoVulnerability({
fn: async (req, res) => {
const filter = {
name: req.query.key
}
try {
await require(tmpFilePath).vulnerableFindOne(collection, filter)
} catch (e) {
// do nothing
}
res.end()
},
vulnerability: 'NOSQL_MONGODB_INJECTION',
makeRequest: (done, config) => {
axios.get(`http://localhost:${config.port}/?key=value`).catch(done)
}
res.end()
},
vulnerability: 'NOSQL_MONGODB_INJECTION',
makeRequest: (done, config) => {
axios.get(`http://localhost:${config.port}/?key=value`).catch(done)
}
})
})
})
})
}
})
})
})
Expand Down

0 comments on commit 0e039c8

Please sign in to comment.