我刚刚学习 mongodb,有一个重要的问题。 mongodb 中是否有像 php 中那样的安全性?在 php 中我可以使用类似的东西:
$stmt = $this->conn->prepare("UPDATE news SET shown = shown+1 WHERE newsID = :newsID");
$stmt->bindValue(":newsID", $id, PDO::PARAM_INT);
$stmt->execute();
但是 mongodb 呢?看到将变量直接插入到 mongodb insert 语句中很奇怪,例如:
db.products.insertOne( { _id: 10, "item": itemName, "qty": itemQuantity } );
谢谢您的解释!
最佳答案
除非你构建类似的东西
db.products.insertOne( JSON.parse('{ _id: 10, "item": itemName, "qty": itemQuantity }') )
SQL注入(inject)会相当困难。我想没有人会自愿编写这样的代码。
当你运行像 db.find({"someProperty": userInput})
这样的语句时
黑客尝试传递 {"$exists": true}
那么结果会是 db.find({"someProperty": "{\"$exists\": true}"})
不应返回任何结果。
但如上所述,使用 JSON.parse()
或类似内容可能会存在安全缺陷。
关于javascript - mongodb 中是否有类似于 php 中的准备语句以确保安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69785444/