How to sanitize and validate user input to pass a Checkmarx scan

自古美人都是妖i 提交于 2020-06-11 20:12:12

问题


I have an endpoint that receives a String from the client as seen below:

@GET
@Path("/{x}")
public Response doSomething(@PathParam("x") String x) {
    String y = myService.process(x);
    return Response.status(OK).entity(y).build();
}

Checkmarx complains that this element’s value then "flows through the code without being properly sanitized or validated and is eventually displayed to the user in method doSomething"

Then I tried this:

@GET
@Path("/{x}")
public Response doSomething(@PathParam("x") String x) {
    if (StringUtils.trimToNull(x) == null || x.length() > 100) { 
        throw new RuntimeException(); 
    }
    x = x.replace("'", "").replace("`", "").replace("\\", "").replace("\"", "")
    String y = myService.process(x);
    y = y.replace("'", "").replace("`", "").replace("\\", "").replace("\"", "")
    return Response.status(OK).entity(y).build();
}

But it still considers this a high severity vulnerability.

How do I properly sanitize or validate to pass a Checkmarx scan?


回答1:


HtmlUtils from spring-web got the job done with:

HtmlUtils.htmlEscape(x)

Maven dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>



回答2:


in .Net framework > 4.0 use AntiXSS

AntiXssEncoder.HtmlEncode()



来源:https://stackoverflow.com/questions/31984962/how-to-sanitize-and-validate-user-input-to-pass-a-checkmarx-scan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!