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

cah*_*hen 6 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();
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

但它仍然抱怨并认为这是一个高度严重的漏洞.

如何正确消毒或验证以满足Checkmarx的要求?

cah*_*hen 9

spring-web的HtmlUtils完成了工作:

spring-web