I am developing a web application using java servlets and jsps. I wanted to make sure my application is secure, that's why I ran some tools and got the reports regarding cross-site scripting. Please find the below code:
SampleServlet.java:
String key = ExternalAuthentication.startExternalAuthentication(request);
request.setAttribute("authParam", authParam);
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n");
out.write("<html><body onload=\"document.forms[0].submit()\">\n");
out.write("<form method=\"POST\" action=\"" + request.getContextPath() + targetPage + "\">\n");
out.write("<input type=\"hidden\" name=\"actionUrl\" value=\"" + actionUrlBuilder.toString() + "\"/>\n");
out.write("<input type=\"hidden\" name=\"authParam\" value=\"" + request.getAttribute("authParam") + "\"/>\n");
out.write("</form>\n</body>\n</html>\n");
The above `setAttribute` will be used in JSP by saying
in jsp:
// I am referring to the request attributes that have been contaminated. - comment from tool
//for context HTML double quoted is not properly sanitized for attribute, request.getAttribute ( "authParam" ) linked to an
//HTML page of There is a risk that lead to cross-site scripting - comment from tool
request.getAttribute("authParam");
Can anyone suggest how to fix it? Is it required to encode the authParam
value before setting into the request?