风险写法与修复
风险写法与修复
// 风险:直接使用客户端 JSON 作为查询条件
// db.collection('users').findOne(req.body)
// 修复:只取所需字段并强制类型
const username = String(req.body.username);
const password = String(req.body.password);
db.collection('users').findOne({ username, password });输入层限制(示意)
输入层限制(示意)
拒绝非字符串/数字类型的参数
过滤 $ 开头的键名
为集合定义 schema 校验,限制字段类型ORM/ODM 提供 schema 校验时,为集合定义严格 schema 可显著降低此类风险。