The JavaScript of a MongoDB $where is built from strings
What it finds
db.users.find({ $where: "this.name == '" + name + "'" }): MongoDB runs the text of $where as JavaScript on the server. A value put into that text can end the string and add code, which reads or changes other documents (CWE-943). Query operators such as { name: value } or $expr take values without running them.
Reported is a property $where whose value is a template literal with a substitution or a string concatenation with a part that is not a literal. A literal and a function are left alone.
From the rule's source, nosql_where_built_from_strings.rs.
export function find(db: any, name: string, age: number) { db.collection("users").find({ $where: `this.name == '${name}'` });tx0 reports: the JavaScript of this $where is built from strings db.collection("users").find({ "$where": "this.age > " + age });tx0 reports: built from strings db.collection("users").find({ $where: "this.age > 18" }); db.collection("users").find({ $where: `this.age > 18` }); db.collection("users").find({ $where: function (this: any) { return this.age > 18; } }); db.collection("users").find({ name, age: { $gt: age } }); return db.collection("users").find({ $where: "this.a" + " > 1" });} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:nosql-where-built-from-strings" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:nosql-where-built-from-strings] reason="..."tx0 explain tx0/ts:nosql-where-built-from-strings shows the same in the terminal. Found a false positive? Report it.