有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用ESAPI解决资源注入(URL)问题

我是堆栈溢出论坛的新手。我对修复加固扫描问题有疑问

HP Fortify scan报告以下代码的资源注入问题

String testUrl = "http://google.com";
URL url = null;

try {
   url = new URL(testUrl);
} catch (MalformedURLException mue) {
   log.error("MalformedUrlException URL " + testUrl + " Exception : " + mue);
}

在上面的代码中,fortify在第行显示资源注入=>url=新url(testUrl)

我已经使用ESAPI对URL验证进行了以下代码更改,以解决这个问题

String testUrl = "http://google.com";
URL url = null;

try {
   String canonURL = ESAPI.encoder().canonicalize(strurl, false, false);
   if(ESAPI.validator().isValidInput("URLContext", canonURL, "URL", canonURL.length(), false)) {
       url = new URL(canonURL);
   } else {
       log.error("In Valid script URL passed"+ canonURL);
   }
} catch (MalformedURLException mue) {
   log.error("MalformedUrlException URL " + canonURL + " Exception : " + mue);
}

但是,仍将扫描报告强化为en错误。这不是解决这个问题的办法。我做错了什么

任何解决方案都会大有裨益

谢谢

马里穆图。M


共 (1) 个答案

  1. # 1 楼答案

    boolean isValidInput(java.lang.String context,
                     java.lang.String input,
                     java.lang.String type,
                     int maxLength,
                     boolean allowNull)
                     throws IntrusionException
    

    type filed in isValidInput function defines a Regular expression or pattern to match with your testUrl.

    比如:

    try {
        ESAPI.validator().getValidInput("URI_VALIDATION", requestUri, "URL", 80, false);        
    } catch (ValidationException e) {
        System.out.println("Validation exception");
        e.printStackTrace();
    } catch (IntrusionException e) {
        System.out.println("Inrusion exception");
        e.printStackTrace();
    }
    

    如果requestUri与验证中定义的模式匹配,它将通过。验证程序下的属性。URL且其长度小于80

    Validator.URL=^(ht|f)tp(s?)\:\/\/0-9a-zA-Z(:(0-9))(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\+=&%\$#_])?$