Web applications are exposed to a number of threats all the time by their very nature of serving content to the public. There are intruders, hackers, impersonators, and eavesdroppers out there who try to wreck the contents you publish. This may harm your business in a serious way and therefore, it is important that you secure the contents of your Web application from such elements.
In this blog, I am going to explore some of the basic steps that can be taken to secure any Java-based Web application.
Placing the Files in the Right Directory
Java Web applications contain static HTML pages, JSP files, Servlet classes, Framework related classes (such as Action classes in case of Struts), Helper or Utility classes, image files, and any third-party jar files required by the application. Placing these files in the correct directories is the first step towards securing the contents of the application. The typical directory structure of any Java-based Web application deployed on a Tomcat Web server looks like this:
Only some of the parts of the application can be accessed directly using the URL. For example, in our case, the name of the application is TestApp which is also called a Webapp root directory. Anything that is placed directly inside the TestApp directory is accessible to the outside world, except the contents of the WEB-INF and META-INF directories. This stands true for any other directories we choose to create directly inside the TestApp directory to arrange our Web applications contents.
In our example, HTML files and JSP pages are placed directly inside the TestApp directory. Assume that a login.jsp file is also placed directly inside the TestApp directory. This file can be accessed directly from outside the application using the URL http://www.mywebserver.com/TestApp/login.jsp. Further, assume that we choose to create a new directory called ‘auth’ inside the TestApp directory and place the login.jsp file in that directory. Will this new directory still be accessible from outside the application? Yes. In this case, the user just needs to point the browser to http://www.mywebserver.com/TestApp/auth/login.jsp.
This is not true for the resources placed inside WEB-INF and META-INF directories. Anything inside these two directories cannot be accessed from outside the Web server. The Sun Java Specification restricts any Web Server that supports Java based application deployment from directly serving the contents inside the WEB-INF and META-INF directories. If an attempt is made to access the resources inside these two directories, the Web Server in this case must respond with a “404 NOT FOUND” error to the user.
In our example above, if we move login.jsp from TestApp to WEB-INF and try to access it through the URL http://www.mywebserver.com/TestApp/WEB-INF/login.jsp, we will get a “404 NOT FOUND” error from the Web Server. Try this with the META-INF directory as well and you will get the same error.
So, the basic rule of thumb with any Java-based Web application is, place the resources you want to restrict access to in either the WEB-INF directory or META-INF directory. In other words, never put your confidential and valuable resources outside these two directories!
Disabling the Directory Listing Functionality
A Web Server is simply a place to publish the content that you want other people to access. You use the standard directory structure given above to arrange your application resources in the required and desired manner. You have also restricted the access to confidential resources by placing them in the appropriate directories as explained above. What will happen if someone is able to navigate through these directories and hack in? A situation like this is known as a directory traversal attack.
The directory listing feature is enabled by default when you install the Tomcat Web Server for the first time. This feature is useful when you are developing and debugging the Web application to quickly traverse through the application directories and the contents inside. You may not like to use this feature on the production server though. The main reason behind directory listing restriction is to discourage users from navigating through proprietary information.
You can disable the directory listing feature in two ways:
- Modify the global web.xml file to disable the feature for all Web applications.
- Modify the web.xml file specific to the application to disable the feature for that single application.
The first way is to open and modify the $CATALINA_HOME/conf/web.xml file. Make sure it contains the following settings for org.apache.catalina.servlets.DefaultServlet.
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.
DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
The second way is to redefine the above servlet mappings, with a different servlet name, inside the web.xml file of a particular Web application. For example, if you want to disable the directory listing feature only for TestApp, then you need to edit the TestApp/WEB-INF/web.xml file. Make sure it contains the following servlet mapping for org.apache.catalina.servlets.DefaultServlet.
<servlet>
<servlet-name>defaultServlet</servlet-name>
<servlet-class>org.apache.catalina.servlets.
DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>defaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Note that we have changed the value of the <servlet-name> element in the second listing above. This is required to avoid a conflict for two different servlet mappings for the same servlet.
The two techniques I have described here are the basic ways to start with security implementations for Web applications. At the advanced level, several additional checks such as authentication, authorization, confidentiality, data integrity, encryption, Secure Socket Layer (SSL) protocol, and data conversion techniques are needed to strengthen the security of the application.



