|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Feature How to Secure a Web Application
How to Secure a Web Application
By: Neil Smithline
Nov. 6, 2003 12:00 AM
While security is a concern throughout an application, it is especially important for Web application components. An insecure Web application leaves a Web site vulnerable to many attacks, some that require nothing more than an Internet browser and a small amount of knowledge. The global and anonymous nature of the Internet means that a Web application can be attacked from anywhere in the world by individuals who frequently cannot be traced. Even if they are found, international law often makes prosecution all but impossible. Java 2 Enterprise Edition (J2EE) provides many security features. Understanding when and how to use these features is complex and error prone. Furthermore, J2EE security falls short of satisfying many of the needs of Web application developers. BEA WebLogic Server implements all of the J2EE Web application security features and extends them to help complete some tasks that are impossible using J2EE APIs alone. Web Application Security Sample Web Application The application has been tested on BEA WebLogic Server 8.1. Besides deploying the Web application, two things must be done to enable it to run: Why these actions are required will be explained as they arise. Encryption (SSL) Unfortunately, as is the case with many security technologies, using encryption adversely affects performance. For this reason, SSL is typically enabled only for sensitive pages. A sensitive page is any page that contains confidential data, either outbound from the server or inbound from the user's browser via a form. There are a few exceptions to this rule. Non-sensitive pages that are in the middle of an otherwise secure conversation should generally be encrypted (e.g., an advertisement in the middle of a purchasing transaction). Besides being convenient for the developer, many browsers notify users with a prompt when the browser changes from an encrypted conversation to a clear-text conversation. To avoid these potentially confusing prompts, use encryption for these nonsecure pages. Also, pages that contain forms should be encrypted if the data to be filled in by the user will contain sensitive information. For example, consider a form that requires you to enter your name and credit card number. When the form is submitted it must use encryption to protect the confidential information, but the form itself, even if it does not contain confidential information, should be encrypted as well. This is needed because browsers generally display a lock in their bottom margin when a page has been encrypted. Some users will not enter secure data into a form if they don't see the lock. Note that this lock is irrelevant to whether the form data, the information the user actually wishes to secure, will be encrypted. The lock only signifies that the form has been encrypted. So, despite the lock usually being irrelevant, encryption should be used to ease users' concerns. You can require a page to be encrypted by specifying a transport guarantee of either "INTEGRAL" or "CONFIDENTIAL" (see for details). For a concrete example, consider a very simple Web application that allows online shopping. It contains the following files: Figure 1 shows the flow for this site as well as the required use of encryption. Only the orderstatus.jsp page requires the use of encryption, as the post of the form from billinginfo.jsp contains sensitive data. That being said, it is recommended that billinginfo. jsp be encrypted as well so that users see the lock on their browser's toolbar. Listing 1 shows a fragment of a web.xml that would be used to establish these encryption requirements.
![]() Figure 1: Site flow requiring encryption When you specify a <transport-guarantee> that requires encryption, BEA WebLogic Server will not allow a non-SSL connection to occur. If a non-SSL connection is attempted, WebLogic Server will send a redirection to the browser to cause it to access the page via SSL. This provides a convenient means of converting to SSL. That is, shoppingcart.jsp does not need to specify that SSL should be used for billinginfo. jsp. The application server will automatically ensure this. For example, in our example Web application, the link in shoppingcart. jsp to go to billinginfo.jsp is: <a href="billinginfo.jsp"> Checkout </a> It does not specify HTTPS. WebLogic Server will ensure that HTTPS is being used. Two issues that arise from this behavior are: <a Role-Based Security Role-based security should be used in one of two circumstances. First, when you have parts of the site that you wish to restrict to a subset of all users. In our example we will show how to restrict part of the site to the "Customer" role, but other roles are possible. For example, one can imagine adding more functionality, such as the ability to search orders, to the site to support customer support employees. The second circumstance where rolebased security should be used is when you need to know the identity of the user. The username might be needed for complex tasks such as maintaining statistics about which pages a user visits but also for simple tasks such as having a welcome message that includes the user name. The username can be obtained by calling request.getRemoteUser() This method only returns the username after the user has logged in. Prior to logging in, the user is anonymous and getRemoteUser() returns null. The simplest way to force a user to log in is to have them access a protected page. This forces the container to execute the login process, after which getRemoteUser() will return a valid username. In our example, shoppingcart.jsp requires login so that we can use the customer's username to look up their shopping cart in the database. While our sample does not actually access a database, it does print the username out in the header. To add role-based security to the above sample, we will add the ability to maintain a permanent record of all customers' shopping carts and purchases. To accomplish this, the Web application must ensure that only authenticated users are allowed to access the shopping cart. Once the users are authenticated, the shopping cart and order information can be stored in a database associated with the user. Figure 2 illustrates the new Web application and the use of role-based security in it. Listing 2 is a fragment of the web.xml used to configure this security. One feature to note is that there is no need to add an explicit link to the login.jsp page from catalog.jsp. Simply link to shoppingcart.jsp and, due to the <security- constraint> and <login-config> tags, the servlet container presents the login.jsp page for you. Precisely, it will interpose the login.jsp form whenever shoppingcart.jsp is referenced (or any protected page for that matter) and the current user has not logged in. Furthermore, if the current user turns out not to be a Customer, then they will receive an access-denied error page (i.e., HTTP error code 403).
![]() Figure 2: New Web application using role-based security Declarative vs Programmatic Security Online User Registration
![]() Figure 3: New user registration One thing to keep in mind as we extend this sample further is that while Figure 3 shows the login.jsp (and hence the newuser.jsp) page only being executed between catalog.jsp and shoppingcart.jsp, it is possible that it will appear in other places. Any time an unauthenticated user accesses a secure page, the login.jsp page can appear. Even if there are no links to the other pages (e.g., billinginfo.jsp), a user can type the URLs into a browser or they can bookmark the pages. After starting out with some argument processing, userstatus.jsp then creates a user. J2EE provides no standard mechanism for user creation but BEA WebLogic Server does via the UserEditorMBean interface. Once a UserEditorMBean is acquired, all that is needed is to call userEditorMBean.createUser(username, password, ""); (The third argument is descriptive text about the user and not used in this example.) This operation is protected and is available only to users in the Admin role. To allow userstatus.jsp to execute correctly, it needs to have a run-as tag to cause it to be run as an Admin user. The deployment descriptor code is: <servlet> Remember to be sure to have created the "weblogic" user and put it in the "Administrators" group as described earlier to ensure this works. Once the user is created, the servlet logs the new user in. Programmatically logging in a user is not supported by J2EE either, but WebLogic Server allows this via the following lines: SimpleCallbackHandler callbackHandler = Finally, userstatus.jsp displays a success message and a link to continue. The difficulty here is that the destination of the link could be any protected page and is not limited to shoppingcart. jsp. This is because the authentication process can begin any time a protected page is accessed. While J2EE provides no mechanism for discovering the destination URL, WebLogic Server gives access to this via the following call: ServletAuthentication.getTargetURLForFormAuthentication One thing to note here is that to make this work, the new user must be put into the Customer role. This is accomplished in a two-step process. First, the Customer role is mapped to the Customers group (a group is a collection of users – be sure this has been created as described above). This mapping is done in the weblogic.xml file with the following lines: <security-role-assignment> Next, the Web application utilizes the WebLogic extension to put the new user into the Customers group in userstatus.jsp immediately after it is created by calling: groupEditorMBean.addMemberToGroup("Customers", username); Programmatic Security <% if (request.isUserInRole("CatalogAdmin")) { %> This code assumes that updateentry.jsp takes a productid argument and that a getProductID() method exists to return the ID of the product currently being displayed. Bringing It All Together
![]() Figure 4: Completed application's flow BEA webLogic Server's security extensions, such as the UserEditorMBean and the GroupEditorMBean, can be used for user management. User management features are not included in the J2EE standard. In our example, we used these user management features to implement online user registration, a feature common to many Web sites. BEA WebLogic Server also extends programmatic security, enabling a programmatic mechanism of logging into the server. By using ServletAuthentication.authenticate() and ServletAuthentication. getTargetURLForFormAuthentication(), our sample demonstrated how a user could be programmatically logged in and redirected to their original destination. These WebLogic Server extensions are designed to dovetail with the security features available for Web applications in J2EE. They fill gaps in the standard J2EE security functionality, allowing the implementation of richer and more functional Web applications. REPRODUCED WITH PERMISSION FROM BEA SYSTEMS. BEA WEBLOGIC LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||