Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape values from HTML attribute inside jsp to avoid XSS attack?

Tags:

jsp

xss

Inside a jsp page, I have a input value attribute which is filled this way:

value="${param.name}"

It is vulnerable to a XSS attack if someone manage to put something

"><script>doEvil();</script>

How do I properly escape the value of param.name to fix the vulnerability ?

like image 975
Samuel Rossille Avatar asked Sep 27 '13 17:09

Samuel Rossille


People also ask

Does escaping prevent XSS?

Escaping from XSSEscaping is the primary means to avoid cross-site scripting attacks. When escaping, you are effectively telling the web browser that the data you are sending should be treated as data and should not be interpreted in any other way.

Does HTML encoding prevent XSS?

No. Putting aside the subject of allowing some tags (not really the point of the question), HtmlEncode simply does NOT cover all XSS attacks.

Is escaping enough for XSS?

The short answer is no, it's not enough. The long answer is it depends on the context of where the user data goes. In an attribute it definitely will not be safe. In the body of certain tags, etc...

How can XSS be prevented?

In general, effectively preventing XSS vulnerabilities is likely to involve a combination of the following measures: Filter input on arrival. At the point where user input is received, filter as strictly as possible based on what is expected or valid input. Encode data on output.


1 Answers

Use JSTL fn:escapeXml() function.

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input value="${fn:escapeXml(param.name)}" />

An alternative is using a decent MVC framework offering taglibs to represent HTML input elements which already implicitly escape XML/HTML,such as JSF and Spring MVC, so that you don't need to repeat the same over all place and worry about accidently overlooking one.

See also:

  • XSS prevention in JSP/Servlet web application
like image 175
BalusC Avatar answered Oct 05 '22 02:10

BalusC