{"success":true,"name":"build-mongodb-filters-from-whitelists-never-raw-client-input","description":"A MongoDB query filter is built from user or request input","category":"ai-brimind","path":"ai-brimind/build-mongodb-filters-from-whitelists-never-raw-client-input/SKILL.md","file":"SKILL.md","content":"---\nname: build-mongodb-filters-from-whitelists-never-raw-client-input\ndescription: \"A MongoDB query filter is built from user or request input\"\n---\n\n# Build MongoDB Filters From Whitelists, Never Raw Client Input\n\n## When to use\nProject uses MongoDB, and req.query/req.body values flow into find, update, or aggregation filters.\n\n## Example request\nfind({ user: req.body.user }) with body { user: { $ne: null } } → the client injected an operator and matched everyone; NoSQL injection. Login check { email, password: [REDACTED_KEY] } → { $gt: '' } bypasses it; validate types (must be string) before querying. Passing req.query straight as the filter → the client now writes your queries; build the filter from a whitelist of allowed fields instead. Using $where with user input → executes as code; never. Each maps to 'objects from the client can carry $ operators — treat filters as code you author, not data you forward'.\n\n## Instructions\nAct as a security-minded developer who assumes any object arriving from a client may contain $ operators aimed at your query engine. Wherever request data reaches a MongoDB filter or update: (1) never pass client objects directly as filters — construct the filter yourself from a whitelist of allowed fields, (2) enforce primitive types at the boundary: if email must be a string, reject objects (typeof checks or schema validation) so { $ne: null } can never stand in for a value, (3) use or verify the platform's existing protections — Mongoose sanitizeFilter, express-mongo-sanitize, JSON schema validation — before writing custom sanitizing, since the problem has industry-standard solutions, (4) ban $where and any operator that evaluates code with user input, no exceptions. When reviewing existing code, grep for spread request objects ({...req.query}) in query positions and treat each as a finding. Only accept the query path once every client value is type-checked and every filter key is author-controlled, and explain why an attacker-supplied operator now has no route into the query.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1057\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}