Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid apps from XSS attacks?

How to safe guard our web applications from XSS attacks? One app is vulnearable to attack, if it does not do any conversion of a special charecters.

like image 326
Ravi Avatar asked Apr 24 '11 09:04

Ravi


2 Answers

You should HTML escape any input before outputting it back to the user. Some references:

  • OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet
  • Consider using StringEscapeUtils.escapeHtml() from Apache Commons Lang
  • Or use HtmlUtils.htmlEscape() from Spring
  • XSS attack prevention
  • XSS prevention in JSP/Servlet web application
like image 177
WhiteFang34 Avatar answered Sep 23 '22 12:09

WhiteFang34


HTML escaping inputs works very well. But in some cases business rules might require you NOT to escape the HTML. Using REGEX is not fit for the task and it is too hard to come up with a good solution using it.

The best solution I found was to use: http://jsoup.org/cookbook/cleaning-html/whitelist-sanitizer

It builds a DOM tree with the provided input and filters any element not previosly allowed by a Whitelist. The API also has other functions for cleaning up html.

like image 26
eduardohl Avatar answered Sep 22 '22 12:09

eduardohl