Skip to content
SQL injection Write-up Beginner

Stop Trusting Req Body How I Broke An Express Mongodb App With Nosql Injection

The application passed user-controlled objects from the request body directly into a MongoDB query, allowing an attacker to supply query operators instead of plain strings. By sending operator objects for the email and password fields, an attacker could bypass authentication and receive a valid session token without knowing any credentials.

byte Follow

Published on infosecwriteups.com

In Express applications using MongoDB, a common mistake is passing req.body directly into database queries.

byte&borrow, on infosecwriteups.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The application passed user-controlled objects directly into a MongoDB query without enforcing that the values were strings, so MongoDB interpreted the objects as query operators.

Impact

An attacker could bypass authentication and obtain a valid session token for the first matching user, potentially gaining unauthorised access to that account.

What the attacker needed

A running Express + MongoDB app; a login endpoint that accepts JSON body; no special permissions needed

The fix

The application should enforce that input values are of the expected type (strings) at the API boundary and reject objects before they reach the database query.

How it was found

  1. Set up a simple Express application with a MongoDB user model and a login endpoint that accepts email and password from the request body.
  2. Send a POST request to the login endpoint with JSON where the email and password fields are objects containing MongoDB query operators instead of strings.
  3. Observe that the server returns a successful authentication response with a token, even though no valid credentials were provided.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

In a normal login, the app checks that the email and password you type match a record in the database. Here, the app accepted not just text but also special database instructions (called query operators) from the user. An attacker could send instructions that mean 'match any user whose email is not empty', so the database returned a user and the app logged them in without a real password.

What is SQL injection?

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.