SonarQube-不应使用函数构造函数

时间:2020-08-17 07:18:37

标签: javascript sonarqube ecmascript-5

我将SonarQube扫描用于ASP.NET MVC项目以进行代码质量检查。 SonarQube显示了项目中使用的一个JavaScript库的漏洞错误。 即-datepicker.js

错误- 查看此“函数”调用,并确保其参数已正确验证。

下面是datepicker.js中的代码-

(function(){
  var cache = {};
 
  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :
     
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
       
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
       
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
   
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})

此处显示了所用函数构造函数的漏洞-

new Function("obj",
            "var p=[],print=function(){p.push.apply(p,arguments);};" +
           
            // Introduce the data as local variables using with(){}

解决方案-它也提供了解决方案。它说,您应该使用ECMAScript 5的内置JSON函数或专用库。

链接-https://rules.sonarsource.com/javascript/RSPEC-3523

哪些替代方法可以代替新功能(“ param1”,“ param2”)通过声纳扫描?

0 个答案:

没有答案