【问题标题】:How to sanitize and validate user input to pass a Checkmarx scan如何清理和验证用户输入以通过 Checkmarx 扫描
【发布时间】:2015-11-06 05:28:34
【问题描述】:

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

@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 扫描?

【问题讨论】:

  • 我也面临同样的问题,即使在尝试了 y =HtmlUtils.htmlEscape(x)

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


【解决方案1】:

来自spring-webHtmlUtils 完成了这项工作:

HtmlUtils.htmlEscape(x)

Maven 依赖:

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

【讨论】:

  • 它对我有用,我的问题是声纳在控制器类中的一种方法的 String 类型的输入上给出 XSS 问题,我做了领先的尾随空白,但仍然存在问题,所以我使用 HtmlUtils.htmlEscape 对我来说效果很好
  • 这仅适用于 Spring 5 吗?我尝试添加到路径参数字符串。但我在 check marx 中仍然遇到同样的问题
【解决方案2】:

在 .Net framework > 4.0 中使用 AntiXSS

AntiXssEncoder.HtmlEncode()

【讨论】:

  • 它必须逐个属性地应用到属性或一些常见的方式来实现这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
相关资源
最近更新 更多