I have an existing web application to which i need to add protection against script attacks. For this i need to escape string before displaying on pages. One approach is a servlet filter. Other one which i am exploring is, can i write a ELResolver which escapes output of any other ELresolvers (array, map etc) if the return is of type string? Thanks,
How about using JSTL core out tag? Core out tag escapes the passed value by default.
There are two ways to achieve this with help of JSTL (if not installed yet, just drop jstl-1.2.jar in /WEB-INF/lib
folder).
Use
<c:out>
tag of the JSTL core taglib. It by default escapes XML (and thus also HTML) special characters.<c:out value="${param.foo}" />
Use
fn:escapeXml()
function of the JSTL functions taglib. This is been used "under the hoods" by the<c:out>
tag. More useful if you want more XML well formed output and/or don't want to pollute your JSP with tags nested in attributes of other tags:<input name="foo" value="${fn:escapeXml(param.foo)}">
See also:
Update: as per your update/comment which you incorrectly posted as an answer:
c:out and fn:escapeXml are the obvious options. But I am just being lazy trying to avoid changes to multiple jsps, if i can achieve the same at one place.
No, you can't. Let this be a lesson for the next time to properly sanitize the user controlled inputs from the beginning on.