如何清理和验证用户输入以通过Checkmarx扫描

时间:2015-08-13 09:54:27

标签: java security code-analysis static-code-analysis checkmarx

我有一个从客户端接收String的端点,如下所示:

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

Checkmarx抱怨这个元素的值然后"流经代码而没有经过适当的消毒或验证,并最终以方法doSomething"

显示给用户

然后我尝试了这个:

@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();
}

但它仍然认为这是一个高严重性漏洞。

如何正确清理或验证通过Checkmarx扫描?

2 个答案:

答案 0 :(得分:7)

来自spring-web

HtmlUtils完成了工作:

HtmlUtils.htmlEscape(x)

Maven依赖:

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

答案 1 :(得分:1)

在.Net framework> 4.0中使用AntiXSS

AntiXssEncoder.HtmlEncode()