繁体   English   中英

如何解决 java 中未检查的循环条件输入 checkmarx 问题

[英]how to resolve Unchecked Input for Loop Condition checkmarx issue in java

我收到循环条件 checkmarx 问题的未经检查的输入。
我尝试了推荐的代码处理,但它对我不起作用。

Checkmarx 报告的描述:

 Method transformPojoCommon at line 334 of to_web/src/com/toweb/bd/TrainCategoriesBD.java gets user input from element TC_TRAIN_CAT_NAME. This element's value flows through the code without being validated, and is eventually used in a loop condition in getParentTrainTypes at line 162 of to_web/src/com/toweb/dao/TrainCategoriesDAO.java. This constitutes an Unchecked Input for Loop Condition.

我尝试了以下代码:

valdiateRequestInput(ESAPI.encoder().
   canonicalize(request.getParameter(TOWebRequestConstants.TC_OBJID).trim()));

private String valdiateRequestInput(String currentPage) {
    try {
        currentPage = ESAPI.validator().getValidInput("HTTP parameter value: ", currentPage, "HTTPParameterValue", 2000, true);
    } catch (Exception e1) {
        log.error("failed to validate HTTP parameter value ", e1);
        throw new IllegalArgumentException("failed to validate HTTP parameter value "+currentPage, e1);
    }
    return currentPage;
}

我已经根据返回类型使用 ESAPI.validator().getValidInteger() 或 ESAPI.validator().getValidDouble() 解决了这些问题。

希望这个解决方案也能帮助你。

下面的方法只是将字符串的最大大小与输入(您的字符串)进行比较以跳过输入(您的字符串),如果它有无限的长度,因为如果用户输入(您的字符串)有无限的长度,这个输入(您的字符串)如果yourString.length()在 while 或 for 循环中的某处使用,则有可能无限次运行条件,这是一个漏洞并导致通过异常。 因此,使用这种方法,我们可以通过抛出异常消息来说明输入有效字符串或按您的要求处理相同的字符串来避免这种情况。

String yourString = unchecked_input_loop(yourString);

public String unchecked_input_loop(String yourString) {
    if (yourString.length() >= Integer.MAX_VALUE) {
        //if you need to throw the exeption saying OutOfMemoryError you can this, or else you can just return NULL
        throw new RuntimeException("Enter a valid yourString");
    }
    
    return yourString;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM