2015-08-13 69 views
4

我有一個端點從用戶處獲取字符串,如下所示。如何清理和驗證用戶輸入以通過Checkmarx掃描

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

回答