Search code examples
javasecuritycheckmarx

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


I am getting Unchecked Input for Loop Condition checkmarx issue.
I tried recommended code handling but its not working for me.

Checkmarx report's description :

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.

I tried below code:

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;
}

Solution

  • I have fixed these issues by using ESAPI.validator().getValidInteger() or ESAPI.validator().getValidDouble() based on the return type.

    Hope this solution will help you too.