Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
private String login(HttpServletRequest request, HttpServletResponse response) {
        List<String>List<String> errors = new ArrayList<String>();
         request.setAttribute("errors", errors);
        
        StringString username = request.getParameter("username");
        StringString password = request.getParameter("password");
        
        // Basic input validation
        ifif(username.matches("[\\w]*")) errors.add("Incorrect user name format.");
        ifif(password.matches("[\\w]*")) errors.add("Incorrect password format.");
        
        ifif(errors.size() > 0) return "error.jsp";
        
        UserBeanUserBean dbUser = this.userDAO.lookup(username);
        ifif(!dbUser.checkPassword(password)) {
             errorserrors.add("Passwords do not match.");
             returnreturn "error.jsp";
        }
        
        CookieCookie userCookie = new Cookie("user", username); // Create a cookie that contains the username
        responseresponse.addCookie(userCookie); // Send the cookie information to the client
        
        returnreturn "welcome.jsp";
}

Note that the above non compliant code example stores the user name within the cookie for authentication purposes. This particular code example is insecure because an attacker could possibly perform a cross-site scripting attack or sniff packets to find the user name within the cookie. If an attacker had the user name of a particular individual, they could forget their own cookie containing the user name and easily gain access to their account within the web application assuming that the application uses the cookie to identify a user.

...

Code Block
bgColor#ccccff
private String login(HttpServletRequest request, HttpServletResponse response) {
    	List<String> errors = new ArrayList<String>();
        request.setAttribute("errors", errors);

    	String username = request.getParameter("username");
    	String password = request.getParameter("password");

        // Basic input validation
    	if(username.matches("[\\w]*")) errors.add("Incorrect username format.");
    	if(password.matches("[\\w]*")) errors.add("Incorrect password format.");

    	if(errors.size() > 0) return "error.jsp";

    	UserBean dbUser = this.userDAO.lookup(username);
    	if(!dbUser.checkPassword(password)) {
    		errors.add("Passwords do not match.");
    		return "error.jsp";
    	}

    	HttpSession session = request.getSession();
    	session.invalidate(); // Invalidate old session id
    	session = request.getSession(true); // Generate new session id
    	session.setMaxInactiveInterval(2*60*60); // Set session timeout to two hours
    	session.setAttribute("user", dbUser); // Store user bean within the session

    	return "welcome.jsp";
}

In the above solution, we have switched from a cookie to a session to store user information. Additionally, the current session is invalidated and a new session is created in order to avoid session fixation attacks as noted by The Open Web Application Security Project (OWASP 2009). The timeout of the session has also been set to two hours to minimize the window that an attacker has to perform any a session hijacking attack.

...