Group
  • Security

    Prevent NoSQL injection in my queries

    Stops query operator injection by validating input types and stripping $-prefixed keys before they reach the database.

    • Improve quality
    • Debug & fix
    • Intermediate
    • General

Free Prompt

Audit my app for NoSQL injection vulnerabilities. Look at every place request data (body, query string, params) is passed into a database query, especially MongoDB-style queries where user input lands inside a filter object. The specific risk: if user input is an object instead of a string, operators like $gt, $ne, or $where can change the query's meaning (the classic example is a login check where { "email": { "$ne": null }, "password": { "$ne": null } } matches any user). For each query, enforce that expected scalar fields are actually strings or numbers before they enter the query, reject or strip keys beginning with $ or containing dots from user-supplied objects, and use explicit whitelists of allowed fields when building filters from request data. Do not change the shape of my API responses or the database schema. Do not add a heavy ORM if the project queries the database directly; fix the queries in place. Keep validation errors consistent with the app's existing error format. Show me each query you hardened, the attack it was open to, and the fix. End with a reproduction I can run: a crafted request body that would have bypassed a query before your fix, and confirmation it now gets rejected.

What This Does / How This Helps

Finds the queries where user input can smuggle database operators into your filters, and locks each one down with type checks and key stripping. NoSQL injection is less famous than SQL injection but just as effective. If a login endpoint passes the request body straight into a query, an attacker posts an object with a $ne operator instead of a password and signs in as the first user in the database. AI-generated MongoDB code does this constantly because passing the body directly is the shortest path. You get each vulnerable query fixed in place, plus the crafted request that used to bypass it so you can confirm it now fails.

Want to skip doing this by hand?

Fortivibe audits your app for all of the areas these prompts cover (and more).

See What We Check

Related Prompts