XSS

Feature Value
Type Detection/Protection
Risk OWASP A7
Covered by Agent/Library

Cross-site Scripting (XSS) attacks are a type of injection in which malicious scripts are injected into otherwise benign and trusted websites.

XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser-side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user within the output it generates without validating or encoding it. An attacker can use XSS to send a malicious script to an unsuspecting user. The end user's browser has no way of knowing that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and used with that site. These scripts can even rewrite the content of the HTML page.

More information

How to solve it

XSS can be prevented in JSP by using JSTL <c:out> tag or fn:escapeXml() EL function when (re)displaying user-controlled input. This includes request headers, cookies, URL, body, parameters, etc. or even the whole request. Also, the user-controlled input which is stored in a database needs to be escaped during redisplaying.

Examples:

JSTL Tag: <c:out value="${bean.userControlledValue}"/>

Spring Form Tags: <form:hidden path="${bean.userControlledValue}" htmlEscape="true" />

Spring Eval: <spring:eval expression="bean.userControlledValue" htmlEscape="true"/>

Struts 1: <bean:write name="bean" property="userControlledValue" filter="true"/>

Struts 2: <s:property value="#request.bean.userControlledValue" escapeHtml='true'/>

Spring MVC can be configured to enable the 'htmlEscape' by default.

Page level property:
<spring:htmlEscape defaultHtmlEscape="true"/>

Or application level configuration in web.xml file:
<context-param> <param-name>defaultHtmlEscape </param-name> <param-value>true </param-value> </context-param>