We have an very old web server with some JSP pages like below. I think I have checked the input parameter "version" with a whitelist "[a-zA-Z0-9]*". But the CheckMarx still got XSS attach warning: "This untrusted data is embedded straight into the output without proper sanitization or encoding, enabling an attacker to inject malicious code into the output.". Do you know how to do this correctly in JSP pages ? It just used to display something from the parameter input.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.mytest.util.SecurityService"%>
<html>
<%
String version = SecurityService.getSafeContent(request.getParameter("version"));
%>
<head>
<title>My Project</title>
</head>
<body>
<div style="text-align: center">
<table>
<tr>
<td>
<div style="text-align: center"><%=version%></div>
</td>
</tr>
</table>
</div>
</body>
</html>
public class SecurityService{
public static final String PARAM_INVALID_DATA_POINT = "";
public static String getSafeContent(String content) {
if(!StringUtils.isEmpty(content) && content.matches("[a-zA-Z0-9]*")) {
return content;
}
return PARAM_INVALID_DATA_POINT;
}
}
Thanks,