Nodejs:检查请求体是否有坏字符

时间:2018-01-30 11:57:30

标签: node.js express

我正在为我的项目使用Nodejs + Express。

我正在尝试检查req.body中是否有任何无效字符。

为此,我创建了以下代码:

在express.js

var badStrings = ["'", '"', '<', '>'];

if (hasBadInput(req.body)) {
    // Bad input detected
    return res.status(400).send("BAD REQUEST");
}

function hasBadInput(requestParams) {
    for (var key in requestParams) {
        var value = requestParams[key];

        if (value && isBadInput(value)) {
            return true;
        }
    }

    return false;
}

function isBadInput(value) {
    for (var i = 0; i < badStrings.length; i++) {
        value += ""; // convert to string

        if (value.indexOf(badStrings[i]) > -1) {
            return true;
        }
    }

    return false;
}

上面代码的问题在于它无法检查嵌套对象的错误字符。

与req.body一起使用,如:

{
   "username": "abc",
   "password": "*****"
}

不适用于req.body:

{
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "500"
   }
}

以上代码不会检查销售对象中的数据。此外,嵌套可以是任何级别。

我应该使用某种递归代码来检查嵌套数据吗?

我不想清理输入,因为这是通过应用程序完成的。

1 个答案:

答案 0 :(得分:1)

如果当前值为object,则可以使用recursion使hasBadInput函数自行调用它:

function hasBadInput(requestParams) {
    for (var key in requestParams) {

        var value = requestParams[key];

        // if the current value is an object check it too
        if ( value !== null && typeof value === 'object' ) {
            if ( hasBadInput(value) ) return true; 
            // else next iteration
            continue;
        }

        if (value && isBadInput(value)) {
            return true;
        }
    }

    return false;
}

工作演示

&#13;
&#13;
var badStrings = ["'", '"', '<', '>'];

function hasBadInput(requestParams) {
    for (var key in requestParams) {

        var value = requestParams[key];

        // if the current value is an object check it too
        if ( value !== null && typeof value === 'object' ) {
            if ( hasBadInput(value) ) return true;
            continue;
        }

        if (value && isBadInput(value)) {
            return true;
        }
    }

    return false;
}

function isBadInput(value) {
    for (var i = 0; i < badStrings.length; i++) {
        value += ""; // convert to string

        if (value.indexOf(badStrings[i]) > -1) {
            return true;
        }
    }

    return false;
}

// test 

var res = hasBadInput({
   "username": "abc",
   "password": "*****"
}); 

console.log(res); // should be false

res = hasBadInput({
   "username": "abc>",
   "password": "*****>"
}); // true

console.log(res); // should be true

res = hasBadInput({
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "500"
   }
}); 

console.log(res); // should be false

res = hasBadInput({
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "<500>"
   }
});

console.log(res); // should be true

res = hasBadInput({
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "500"
   },
   "sale1": {
      "product_id": "24",
      "amount": "500"
   }
}); // false

console.log(res); // should be false

res = hasBadInput({
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "500"
   },
   "sale1": {
      "product_id": "24",
      "amount": "<500>"
   }
}); // true

console.log(res); // should be true

res = hasBadInput({
   "user_id": "1",
   "sale": {
      "product_id": "24",
      "amount": "500"
   },
   "sale1": {
      "product_id": "24",
      "amount": "500",
      "sale2": {
          "product_id": "24",
          "amount": "<500>"
      }
   }
}); // true

console.log(res); // should be true
&#13;
&#13;
&#13;