J2EE Bad Practices: JSP Expressions

Overview
JSP 2.0 introduced a new capability allowing one to use JSP Expressions directly within the template text (i.e. outside of tag libraries or tag files) of a web page. However, improper use of the expressions will leave an application open to XSS Attacks.

Consequences

 * Failing to use JSP Expressions properly will leave an application open to XSS Attacks.

Exposure period
This vulnerability has existed since servlet containers and application servers began implementing the [| JSP 2.0 standard]

Platform

 * Languages: Java/JSP

Severity
High

Likelihood of exploit
If a developer uses JSP Expressions to write unsanitized, user-entered data to a page, the likelihood of exploit is very high.

Discussion
JSP 2.0 expressions allow developers to expose data and objects stored in application, session, request, or page scope using an Ant-style syntax. It allows you to replace this

with this

As you can see, the syntax in the second example is more succinct. However, it may also expose a Cross Site Scripting XSS vulnerability. The problem that few tutorials (including | Sun's Java EE 5 Tutorial) mention is that the expression syntax does not escape HTML characters. Therefore, any web application using JSP Expressions to output unsanitized, user-entered data will be vulnerable to Cross Site Scripting (XSS) attacks.

Avoidance and mitigation
The safest cure for this XSS vulnerability leads one right back to the first example. As section 2.2.2 of the JSP 2.0 Specification reads, “In cases where escaping is desired (for example, to help prevent cross-site scripting attacks), the JSTL core tag c:out can be used.”

To be sure your code is not vulnerable to the potential XSS vulnerability described herein, use JSP Expressions only as tag library attribute values and stick to using JSTL‘s c:out tag for writing text to a page. Deciding which instances of template text expression usage are safe and which are not is error prone and the consequences of a mistake is dangerous.

Examples
See above.