logo

Database

Javascript Marsdb Nosql Injection

Description

This detector identifies NoSQL injection vulnerabilities in JavaScript applications using the MarsDB library. NoSQL injection occurs when user-controlled data is directly passed to database queries without proper validation or sanitization, allowing attackers to manipulate query logic and potentially access unauthorized data or perform malicious operations.

Weakness:

106 - NoSQL injection

Category: Unexpected Injection

Detection Strategy

    • The code must import or reference the MarsDB library (either directly as 'marsdb' or through an alias)

    • User-controlled data (from request parameters, form inputs, URL parameters, etc.) must be passed directly to MarsDB query methods without proper validation or sanitization

    • The vulnerable pattern occurs when external input flows into MarsDB collection operations like find(), findOne(), update(), remove(), or similar query methods

    • The detector specifically looks for cases where the query object or query parameters contain unsanitized user input that could modify the intended database operation

Vulnerable code example

const MarsDB = require('marsdb')

const users = new MarsDB.Collection('users')

module.exports = function login(req, res) {
  // Vulnerable: user input passed directly to query
  users.findOne(req.body.credentials)
}

✅ Secure code example

const MarsDB = require('marsdb')

const users = new MarsDB.Collection('users')

module.exports = function login(req, res) {
  // Safe: validate and sanitize input before query
  const { username, password } = req.body.credentials || {}
  ...