{"success":true,"name":"never-build-a-query-from-raw-user-input-parameterize-and-san","description":"User input flows into a DB query, search, or filter you are writing","category":"ai-brimind","path":"ai-brimind/never-build-a-query-from-raw-user-input-parameterize-and-san/SKILL.md","file":"SKILL.md","content":"---\nname: never-build-a-query-from-raw-user-input-parameterize-and-san\ndescription: \"User input flows into a DB query, search, or filter you are writing\"\n---\n\n# Never Build a Query From Raw User Input: Parameterize, and Sanitize Mongo Operators\n\n## When to use\nYou are writing a database query (SQL or MongoDB) whose filter, search text, or values come from a request — body, query params, or URL — and the input is placed into the query without a deliberate safety step.\n\n## Example request\nSQL built by string concatenation: \"WHERE name = '\" + input + \"'\" → the input ' OR 1=1 -- turns it into 'return everything'; this is SQL injection, and parameterized queries (placeholders) are the built-in cure. Mongo login like find({ email, password: [REDACTED_KEY] }) → sending { \"$ne\": null } as the password matches ANY password; objects from the client must never be dropped into queries as-is — this is NoSQL operator injection. Search box input pasted into a regex → the input '(' crashes it, and 'a+)+' patterns can freeze the server; escaping regex special characters is a one-line, solved step. Using an ORM/ODM normally (Mongoose with typed schema fields, parameterized SQL through the driver) → the tool already does the safe thing; the danger appears exactly where you bypass it with raw strings or raw objects.\n\n## Instructions\nAct as a developer who assumes every request field will one day contain a crafted attack string — because automated scanners try them against every public endpoint on the internet, including yours, within days of deploy. For every query that touches user input, do this: (1) Point at each place input enters the query and classify how it gets there: through a parameter placeholder / typed schema field (safe lane), or through string concatenation / a raw client object spread into the filter (danger lane). Anything in the danger lane gets fixed now. (2) In SQL, never concatenate values into query strings — use the parameterized form your driver or ORM provides ($1/? placeholders, query builders); the database then treats input as DATA, never as query language. This mechanism exists in every driver; hand-escaping is the wheel you must not reinvent. (3) In MongoDB, enforce that values from the client are primitives, not objects: cast explicitly (String(req.body.email)), validate types at the border, or strip $-prefixed keys (mongo-sanitize style) — because any raw object can smuggle operators like $ne, $gt, $where into your filter and rewrite its logic. Login and permission queries deserve the hardest look. (4) User text going into a regex (search features) gets its special characters escaped first — one tiny utility function — so '(' cannot crash the query and pathological patterns cannot hang the process. (5) Keep the DB user's permissions minimal (the app account does not need to drop tables), so that even a mistake has a small blast radius. Only ship the query once you can say, for every input it uses: 'this enters as pure data through this safe mechanism, and here is the line that guarantees it' — 'it worked with normal input' proves nothing, because attackers do not send normal input.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #206\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}