【问题标题】:Code injection issue for class.forname in Java during Checkmarx scanCheckmarx 扫描期间 Java 中 class.forname 的代码注入问题
【发布时间】:2020-02-01 06:27:03
【问题描述】:

如何解决 Java 中 class.forname 的代码注入问题。在 checkmarx 扫描期间,我遇到了这个代码注入问题,下面是示例代码 sn-p


StringBuffer xml = new StringBuffer

xml.append("<?xml version=\ "1.0\"encoding=\"utf-8\" ?>");

xml.append("<Response>");

try{
    String strpath = request.getParameter("myclass");

    If(strpath!= null){
        xml.append("<Message>"+strpath+"</Message>");

        Class cls = Class.forName(strpath);

        strpath = getLocation(cls);

        xml.append("<Path>"+strpath+"</Path>");
    }

}

catch(Exception err){

    strpath = err.getMessage();

}

【问题讨论】:

  • 更新getLocation 实施也在您的问题中。
  • getLocation 实现。 String getLocation(Class cls){String name = cls.getName().replace('.','/') URL loc = cls.getResource("/"+name+".class");文件 f = 新文件 (loc.getFile()); if(f.getPath().starts with("file:")){String s = f.getPath();在} try{ f = f.getCanonicalFile();} catch(IOExecption ioe){ ioe.printStackTrace();}}
  • @Priyo,而不是在评论中发布代码(不编译缺少返回语句),编辑您的问题
  • @Priyo 你想做什么以及为什么使用Class.forName()?请edit您的问题包括您从 checkmarx 获得的完整问题。此外,不要手动编写 XML 代码,为此使用库。你可以犯的错误太多了。

标签: java code-injection checkmarx


【解决方案1】:

对于Checkmarx,您直接为字符串“strpath”分配参数而不对其进行圣化。您需要在使用之前验证字符串“strpath”,并且Checkmarx不会看到任何代码注入

【讨论】:

  • 谢谢。我试过验证没有解决这个问题得到了同样的错误。你能建议吗
猜你喜欢
  • 1970-01-01
  • 2022-11-27
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2023-01-03
  • 2022-12-13
相关资源
最近更新 更多