我有一个从客户端接收字符串的端点,如下所示:
@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 扫描?
最佳答案
HtmlUtils来自 spring-web
的工作完成了:
HtmlUtils.htmlEscape(x)
Maven 依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
关于java - 如何清理和验证用户输入以通过 Checkmarx 扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984962/