On our website we have a Search field and a security researcher has advised us that this is open to XSS exploits as visitors could attempt to execute full JavaScript. They sent us a simple example URL which causes a JavaScript alert box to appear on the Search page - the URL parameters are search?submitted=true&action=search&siteId=2.9945&freeText=1";confirm(/XSSPOSED/);a="&sort=publishedDate_descending&slotSearch=true
The security researcher advised that all data should be passed through a function similar to PHP's htmlentities with both ent quotes and UTF8 flags set to prevent exploitation using obfuscation. So I have searched to see if there is a Java equivalent to htmlentities, and I found the escapeHtml() and escapeJavaScript() methods from the StringEscapeUtils class from Apache Commons Lang: https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html. The escapeHtml() method escapes HTML characters such as < and > but I'm not so sure about JavaScript. The escapeJavaScript method actually seems to escape the characters in a String using JavaScript String rules, rather than stripping out the JavaScript itself.
So, does anyone know of a solution that will 100% stop JavaScript being rendered on the page?