I am trying to improve the security of our web application by preventing XSS injection
I am using OWASP encoder library to escape characters : https://owasp.org/www-project-java-encoder/
The complete url has the following format :
https://nom.domain.fr/auth/login?service=http://nom.domaine.fr/app/jsp/ServletControleur%3Ftraitement=accueil&ticket=XXX-XXXXX-XXXXXXXX
where service get paramater contains the desired url to redirect
There is my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page import="java.util.Vector" %>
<%@ page import="org.owasp.encoder.Encode" %>
<%
String serviceId = (String) request.getAttribute("serviceId");
String token = (String) request.getAttribute("token");
String service = null;
if (serviceId.indexOf('?') == -1)
service = serviceId + "?ticket=" + token;
else
service = serviceId + "&ticket=" + token;
service =
com.dev.util.StringUtil.substituteAll(service, "\n", "");
service =
com.dev.util.StringUtil.substituteAll(service, "\r", "");
service =
com.dev.util.StringUtil.substituteAll(service, "\"", "");
//tried replace & by url encoding %26 but it's not working
service = service.replace("&", "%26");
service = Encode.forHtml(service);
%>
<html>
<head>
<title>Authentication Service</title>
<script type="text/javascript">
var service = "<%= service %>"
window.location.href= service;
</script>
</head>
</html>
The problem is that OWASP transform & character into & endlessly so window.location.href
cause an infinite loop
I tried to replace & by %26 but it's not working unfortunately and has the same effect
Any idea how to solve the problem ?