Veracode中创建猫鼬的数据查询逻辑中特殊元素的不适当中和

问题描述

我正在使用猫鼬创建功能将其添加到我的收藏中。我正在从请求正文中获取数据,并将其传递给创建这样的功能-

const someVariable = req.body;

await usermodelName.create(someVariable);

veracode上,此语句显示了数据查询逻辑中特殊元素的不正确中和-await usermodelName.create(someVariable);

有什么方法可以修改代码以从veracode中删除此警报?

解决方法

您需要使用req.body进行清理,验证或编码。盲目接受可能受到污染或来自不受信任来源的输入是一种风险。

我假设您期望以json形式输入,因此请尝试使用js-string-escape库转义req.body:

var jsescape = require('js-string-escape');
const someVariable = jsescape(req.body);
await userModelName.create(someVariable);

现在,Veracode可能无法识别此第三方编码库,并且仍然会使您的扫描失败。您必须向安全团队提出mitigation步骤,以提出此​​建议。

如果您希望获得特定类型的数据,另一种方法是使用Mongoose的Validation功能:

const schema = new Schema({
  name: {
    type: String,required: true
  }
});

const someVariable = escape(req.body);
await userModelName.create(someVariable,schema);

再次,Veracode可能也无法识别出这种情况,但这是安全的编码最佳实践。