If I append a script tag to a url in the browser the script executes and then the page loads. I want to avoid the execution of the script tag. I tried redirecting to the encoded url using servlet filter and owasp java encoder. The script is getting triggered first and then encoding happens. How do I prevent the script tag from executing?
Server: Tomcat v8.5
Browser: Chrome, Edge
Below is the code for the servlet filter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
String queryString = httpServletRequest.getQueryString();
if (queryString.contains("script")) {
HttpServletRequest modifiedRequest = new HttpServletRequestWrapper((httpServletRequest)) {
@Override
public String getRequestURI() {
String encodedQueryString = Encode.forUri(httpServletRequest.getQueryString());
return httpServletRequest.getRequestURI() + "?" + encodedQueryString;
}
};
chain.doFilter(modifiedRequest, response);
} else {
chain.doFilter(request, response);
}
}