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

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

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

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

Checkmarx 抱怨此元素的值然后 "flows through the code without being properly sanitized or validated and is eventually displayed to the user in method 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 扫描?

来自 spring-web

HtmlUtils 完成了工作:

HtmlUtils.htmlEscape(x)

Maven 依赖项:

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

在 .Net 框架 > 4.0 中使用 AntiXSS

AntiXssEncoder.HtmlEncode()